C# creating fractals using recursion

  • Thread starter Thread starter sox123
  • Start date Start date
S

sox123

Guest
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.

For the circle, I've managed to get something together, but Im struggling on getting it to draw only when a value is entered and a button is pressed - right now it just draws as soon as the form starts.

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 and the circle part.

namespace MD2
{
public partial class Form1 : Form
{
int rekursijaDzilums;
bool circleBool, squareBool;
int xc = 10;
int yc = 10;

const int initialRadius = 100;
const int centerX = 242;
const int centerY = 267;
const double factor = 0.45;//factor for reducing circle size
int recursionDepth;


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)
{
recursionDepth = Convert.ToInt32(textBox1.Text);

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);
}


public void DrawCircle(int x, int y, int radius, Graphics g)
{

g.DrawEllipse(Pens.Black, x - radius, y - radius, 2 * radius, 2 * radius);
}


private bool IsRecursionDepthReached(int radius)
{
return radius < Math.Pow(factor, recursionDepth) * initialRadius;
}


private void drawButton_Click(object sender, EventArgs e)
{
rekursijaDzilums = int.Parse(textBox1.Text); //This is

if (squareBool == true)
{
textBox3.Text = "Square being drawn";
DrawRectangle();
}



if (circleBool == true)
{
textBox3.Text = "Circle being drawn";
//Nothing here yet
}
}


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);
*/

Graphics dc = pictureBox1.CreateGraphics();
Pen myPen = new Pen(Color.Black, 3);
int width = 380;
int height = 380;
dc.DrawRectangle(myPen, xc, yc, width, height);

for (int i = rekursijaDzilums; i >= rekursijaDzilums; i--)
{
width = width / 2;
height = height / 2;
dc.DrawRectangle(myPen, xc, yc, width, height);
}

}






private void circle_Click(object sender, EventArgs e)
{
squareBool = false;
circleBool = true;
textBox4.Text = "";
textBox4.Text = "Circle was pressed"; //using for testing at this stage
}



private void square_Click(object sender, EventArgs e)
{
circleBool = false;
squareBool = true;
textBox4.Text = "";
textBox4.Text = "Square was pressed"; //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 results should look like this when a 'recursionDepth' and button 'Draw' is clicked, it would draw this:

1507391.png1507392.png

Any pointers, criticisms or suggestions greatly appreciated.

Thank you

Continue reading...
 
Back
Top