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.
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.
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
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With