c# - C# 使用递归创建分形
问题描述
尝试在线搜索,但只能找到关于此的过时信息的点点滴滴。本质上,我正在尝试编写一个使用递归绘制正方形(和圆形)分形的程序。对于正方形部分,我试图在字段中绘制一个正方形,然后使用递归将其大小减小 50%(所以一半)并将其旋转 90 度,并根据用户输入的递归深度重复。
诚然,我没有取得太大进展,但希望有人能指出我正确的方向,因为我正在努力理解如何去做。
特别是我将如何创建一个递归函数来减小正方形的大小,然后将其旋转 90 度并绘制它。
namespace MD2
{
public partial class Form1 : Form
{
int recursionDepth;
bool drawCircle, drawSquare;
public Form1()
{
InitializeComponent();
}
private void drawButton_Click(object sender, EventArgs e)
{
recursionDepth = int.Parse(textBox1.Text);
if (drawSquare == true)
{
textBox3.Text = "Square is being drawn"; //used for testing pursposes
DrawRectangle();
}
if(drawCircle == true) //used later when something similar to squares will be drawn
{
textBox3.Text = "Circles are being drawn";
}
}
public void DrawRectangle()
{
/*/////////////////////////////////////
Graphics dc = pictureBox1.CreateGraphics();
Pen myPen = new Pen(Color.Black, 3);
Point[] points =
{
new Point(0, 0),
new Point(0, 400),
new Point(0, 400),
new Point(400,400),
new Point(400, 0),
new Point(0,0),
};
dc.DrawLines(myPen, points);
}*///////////////////////////////////
//Two options, either drawing a square or drawing lines that form a square.
Graphics dc = pictureBox1.CreateGraphics();
Pen myPen = new Pen(Color.Black, 3);
int width = 400;
int height = 400;
for (int i = 0; i <4; i++) //experimenting with for loops, but recursions would be necessary
{
Rectangle rect = new Rectangle(0, 0, width/2, height/2);
dc.DrawRectangle(myPen, rect);
}
}
private void circle_Click(object sender, EventArgs e)
{
drawSquare = false;
drawCircle = true;
textBox4.Text = "";
textBox4.Text = "Circle option selected"; //using for testing at this stage
}
private void square_Click(object sender, EventArgs e)
{
drawCircle = false;
drawSquare = true;
textBox4.Text = "";
textBox4.Text = "Square option selected"; //using for testing at this stage
}
}
}
抱歉它太长了,但我不确定它是否只有部分有意义。
任何指示、批评或建议将不胜感激。
谢谢
解决方案
下面的代码演示了如何使用递归函数来绘制圆圈。您需要指定初始状态(第一个圆的中心,每次迭代后新圆应该小多少)以及递归方法应该终止的时间。如果您忘记了程序将因堆栈溢出异常而崩溃。
程序.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsApp9
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}
Form1.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp9
{
public partial class Form1 : Form
{
const int initialRadius = 120;
const int centerX = 400;
const int centerY = 200;
const double factor = 0.45; // Factor to determine the size of the next smaller radius
const int recursionDepth = 5;
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawRecursionStage(centerX, centerY, initialRadius, e.Graphics);
}
private void DrawRecursionStage(int x, int y, int radius, Graphics g)
{
if (IsRecursionDepthReached(radius))
return;
DrawCircle(x, y, radius, g);
int newRadius = (int)(radius * factor);
DrawRecursionStage(x - radius, y, newRadius, g);
DrawRecursionStage(x, y - radius, newRadius, g);
DrawRecursionStage(x + radius, y, newRadius, g);
DrawRecursionStage(x, y + radius, newRadius, g);
}
private void DrawCircle(int x, int y, int radius, Graphics g)
{
g.DrawEllipse(Pens.Black, x - radius, y - radius, 2 * radius, 2 * radius);
}
private static bool IsRecursionDepthReached(int radius)
{
return radius < Math.Pow(factor, recursionDepth) * initialRadius;
}
}
}
推荐阅读
- javascript - 如何获取表格的所有行并将其显示在警报消息中?
- python - Python api 发布请求
- javascript - 正则表达式捕获位填充
- typescript - 找不到模块“C:\native\win32\x64\12.13.0\edge_nativeclr”
- javascript - 当 playlist.m3u8 文件不断更新时,如何播放 HLS 流?
- python-3.x - 根据行值和行值总和为零在熊猫中创建新列
- jquery - 如何在 ajax 中启用和禁用背景静态模式
- javascript - VUEJS 使用 comment.post_id 和 post.id 为发布社交网络循环评论 我的数据库是 mysql 循环的解决方案是什么?
- python - python中的Alpha-Beta修剪算法不修剪
- java - 创建一个新节点并将对象 bean 放在它下面 java magnolia