繁体   English   中英

C# 使用递归创建分形

[英]C# Creating fractals using recursion

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

表格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