Mathematical nostalgy

There is a time in every person?s life where one likes to recap about long lost projects. Little pieces of code that made you happy inside once it was completed. Well today, I had this moment.

The project I?m talking about is something I came up with after a school lesson on fractals. I won?t be getting much into that, as I am assuming everyone knows what that is. But once that session was done, I grabbed my laptop and went to work. The fractal that originated from that piece of free time is something that made me proud up until today.

Note: I completely made this fractal up from my imagination, if the same fractal already exists, and I?m sure it does, I don?t know about it but would like more info on it.

So anyhow, I was browsing through some old folders on my external disk and discovered that I had deleted that entire project to make room for other stuff. Disappointment went through my entire body and mind. But that didn?t take too long, as I decided to re-write it. Here?s  how I did it:

Basically, the top design of the fractal would be something like this

image

 

A tree-like structure that keeps getting smaller in a repetitive pattern. The tricky part is of course always defining your endpoint. The start point is never a problem, because that?s the endpoint of the previous branch. But the endpoints? they make it a little hard on my head :)

Now there are probably a lot more solutions available to this problem, but in my own case, I decided to go with good old trigonometry. Since the tree is built up using rectangular triangles, it?s pretty easy to come up with a formula that calculates the points needed.

image

x2 = x1 + (cos(alfa) * branchlength)
y2 = y1 - (sin(alfa) * branchlength)

Since I have a left part and a right part, I?m starting off with alfa = 135 and beta = 45. So I?m already done with the formula, on to the code translation!

First of all, it?s fairly logical that I declared a brush, a pen, and a graphics object, in my case called b, p, and g. Nothing fancy there :)

Next part is a littly cooler; in order to implement a configurable nesting level and draw everything with as little effort as possible, I chose to make a recursive method that takes a few parameters, namely a current X position, current Y position, current length, nesting level, an alfa angle and a beta angle, signatured as: private void DrawFrac(int xCur, int yCur, int lenCur, int nestCur, int alfa, int beta){}

So now the rest of the implementation is pretty easy (afterwards :) I busted my head on it for a while because the mathematical part had been a while since I used it)

if (_nestinglevel >= nestCur)
            {
                Point start = new Point(xCur, yCur);
                Point leftEnd = new Point((xCur + Convert.ToInt32(Math.Cos(alfa * (Math.PI / 180)) * lenCur)), (yCur - Convert.ToInt32(Math.Sin(alfa * (Math.PI / 180)) * lenCur)));
                g.DrawLine(p, start, leftEnd);

                Point rightEnd = new Point((xCur + Convert.ToInt32(Math.Cos(beta * (Math.PI / 180)) * lenCur)), (yCur - Convert.ToInt32(Math.Sin(beta * (Math.PI / 180)) * lenCur)));
                g.DrawLine(p, start, rightEnd);

                nestCur++;
                DrawFrac(leftEnd.X, leftEnd.Y, (lenCur / 4) * 3, nestCur, alfa+45, beta+45);
                DrawFrac(rightEnd.X, rightEnd.Y, (lenCur / 4) * 3, nestCur, alfa-45, beta-45);
            }

the result? a nice little fractal :)

 

frac

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Jan
Posted on: 7/2/2009 at 3:29 AM
Categories: C#
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (10) | Post RSSRSS comment feed

Comments