简体   繁体   English

C# 使用递归创建分形

[英]C# Creating fractals using recursion

tried searching online, but could only find bits and bobs of outdated info on this.尝试在线搜索,但只能找到有关此的过时信息的点点滴滴。 Essentially I'm trying to write a program that draws a square (and circle) fractals using recursion.本质上,我正在尝试编写一个使用递归绘制正方形(和圆形)分形的程序。 For the square part I'm trying to draw a square within a field, then using recursion it would reduce its size by 50% (so half) and rotate it 90 degrees, and repeat depending on the recursion depth that's entered by the user.对于正方形部分,我试图在字段内绘制一个正方形,然后使用递归将其大小减小 50%(即一半)并将其旋转 90 度,并根据用户输入的递归深度重复。

Admittedly I haven't made that much progress but was hoping someone could point me in the right direction as I'm struggling with understanding how to go about this.不可否认,我没有取得太大进展,但希望有人能指出我正确的方向,因为我正在努力理解如何去做。

Specifically with how I would create a recursive function that would reduce the size of the square and then rotate it by 90 degrees and draw it.具体来说,我将如何创建一个递归函数来减小正方形的大小,然后将其旋转 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
        }
    }
}

Apologies for it being too long, but I wasnt sure if only parts of it would make sense.为它太长而道歉,但我不确定它是否只有部分有意义。

The end result for the square would be something like this:正方形的最终结果将是这样的: 在此处输入图片说明

and the end result for the circle part would look like this:圆形部分的最终结果如下所示: 在此处输入图片说明

Any pointers, criticisms or suggestions would be greatly appreciated.任何指示、批评或建议将不胜感激。

Thank you谢谢

The code below demonstrates how a recursive function can be used to draw the circles.下面的代码演示了如何使用递归函数来绘制圆。 You need to specify the initial state (center of the first circle, how much smaller the new circles should be after each iteration) and when the recursive method should terminate.您需要指定初始状态(第一个圆的中心,每次迭代后新圆应该小多少)以及递归方法何时终止。 If you forget that the program will crash with a stack overflow exception.如果您忘记程序会因堆栈溢出异常而崩溃。

Program.cs程序.cs

using System;
using System.Windows.Forms;

namespace WindowsFormsApp9
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }
}

Form1.cs表格1.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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM