gpt4 book ai didi

C# 使用递归创建分形

转载 作者:行者123 更新时间:2023-12-05 05:07:32 24 4
gpt4 key购买 nike

尝试在网上搜索,但只能找到一些过时的信息。本质上,我正在尝试编写一个使用递归绘制正方形(和圆形)分形的程序。对于正方形部分,我试图在一个字段中绘制一个正方形,然后使用递归将其大小减小 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
}
}
}

很抱歉它太长了,但我不确定它是否只有部分内容有意义。

方形的最终结果是这样的: enter image description here

圆形部分的最终结果如下所示: enter image description here

如有任何指点、批评或建议,我们将不胜感激。

谢谢

最佳答案

下面的代码演示了如何使用递归函数来绘制圆圈。您需要指定初始状态(第一个圆的中心,每次迭代后新圆应该小多少)以及递归方法应该何时终止。如果您忘记了程序会因堆栈溢出异常而崩溃。

Program.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;
}
}
}

关于C# 使用递归创建分形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59008893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com