Introduction

Happy New Year!

This is not a practical program, it's only a simple program to have some fun with mathematics. It uses Sin() and Cos() functions with recursion to make some fractal shapes and Trees. It has a very simple algorithm.

Background

I initially wrote this code in August 2006 (VB6 format) to show recursive functions in action to a friend. Unfortunately his email had some problems and didn't accept my zip file, so I put it on PSC for him. It was amazing when I found that it was useful for some other guys, and now it's here in C# and VB.NET.

Using the Code

The zip files contain source code that was compiled for Visual Studio .NET 2003 and Visual Studio .NET 2005 in both C# and VB.NET. Just open the solution file and compile it. The executable created is called RealTree.exe.

How It Works

1. You need a loop to make branches:

When each branch divides to 2 new branches, you have 2 ^ Step branches in each step from base (step 0) to top, and when divisions are more, the formula is numberOfDivision ^ Step.

For this part, I used a recursive procedure, each time it runs itself with new position of branch and angle:

How did we find these formulas; see the following figures. Our starting point is P and we want to draw some lines with same lengths; when we draw more lines, it becomes more similar to a math shape, what is it like?

It's like a circle, our first point is the center of the circle and our lines are its radii, so for a sample PS line, we have to find S position on the surface of the circle. We draw a circle in the center of coordinates with one unit radius size to find position of S(X,Y).

If we draw a vertical line from S to X axis, it crosses it at H point, PH is X's dimension of S point. Now if we draw a horizontal line from S to Y axis, it crosses it at K point, PK is Y's dimension of S. When we look carefully at our points, we can find that KS and PH have the same size, and also PK and HS sizes are the same, So our S(X,Y) point can be as S(PH,HS) . We have a right triangle as PHS, as you know about triangles, Sin(P-angle)=HS / PS and so HS=Sin(angle) * PS, Also Cos(P-angle)=PH / PS and so PH = Cos(angle) * PS We know PS size because it is the radius of the circle, so our S point position would be as follows:

Now we return to our project, we draw a tree and try to find the position of its branches.

We are in position P (0, 0) and know that all branches have 10 unit sizes, for PQ line we need position of Q point. It has 90 degrees angle with X axis, its X position is Cos(90) * 10 and its Y position is Sin(90) * 10, so it is as Q(0,10). What about R and S? They have 60 degrees angle with each other, but this is not their angles with X axis, see the next figure for a better view:

When we continue the PQ line, our QR and QS lines make 30 degrees angles with it, and we know that it has 90 degrees angle with X axis, so angles of our lines are 90 + 30 = 120 for QR and 90 – 30 = 60 for QS. Now we calculate the position of R, its X position is Cos(120) * 10= -5 and its Y position is Sin(120) * 10 = 8.6 , right ?!

Wrong! Our (-5, 8.6) position is a point like Z in our shape not R, because when we moved from P to Q, our first position changed and when we want to calculate R position, we are in Q position not in P, so we have to enter these changes in our formula, it means we must add our new calculations to starting positions, so our X value must be -5 + Qx= -5 + 0= -5 and our Y value must be 8.6 + Qy = 8.6 +10 = 18.6 , so the true position is R(-5, 18.6) , and we found our main formulas:

Sx= 0 + Cos(60) * 10 = 5 , and Sy= 10 + Sin(60) * 10 = 18.6, so it is as S(5, 18.6).T and U make a 60 degrees angle with each other, it means each of them has 30 degrees difference with their starting angle and we know their starting angle is 120, so RT makes a 120 + 30 = 150 degrees angle and RU makes a 120 -30 = 90 degrees angle, so their positions are as follows:

What about V and W? Now you know that each of them has a 30 degrees difference from starting point angle, so SV has 60+30 = 90 degrees and SW has 60-30 = 30 degrees angles, position of these points are as follows:

D.P.S Scale (50 ± 50): (Division per Step Scale) When you lead to right, you have more DPS in higher branches than base DPS, and when you lead to left, you have less DPS in higher branches than base DPS.

Leaf Level (50 ± 50): It determines that leaves should be on the higher branches or lower.

Width Scale (50 ± 50): Amount of changes in width of branches from base to leaves.

Wind (50 ± 50): Amount and direction of wind effect.

Widening (500 ± 500): This is a special effect; you must see it in action, I have to make some changes in its values and results.

Note

When you use high values for "Total Steps" or "Division per Step", maybe it takes a long time for rendering, so using of "Broken Branches" and "Division per Step Scale" make it better.

If you use values over 2 for "Division per Step", you have to use higher values for "Broken Branches", it makes a wild Tree. [e.g. with "Total Steps"=30 use following values:( DPS=3, BB=52), (DPS=4, BB=65), (DPS=5, BB=72), (DPS=6, BB=77), (DPS=7, BB=81), (DPS=8, BB=83)...] If you want to deactivate "Broken Branches", you must use lower values for "Total Steps" (~ 15, depends on speed of your computer).

Points of Interest

You can make different funny fractals by changing or adding new variables.

History

Jan 2007: Uploaded on The Code Project

Apr 2007: Version 1.5: Added widening effect and some new samples

Mar 2009: Version 1.6: Added Timer, some new samples and updated to Visual Studio 2005

I would say that including the Swastika was in poor taste, I would not have done that. Hitler really messed up its meaning by using it as the symbol of a bunch of sadist. If it was just a word instead a graphic symbol, then it could be used in a context that made it clear what it actually means. But history is full of changed meanings of words and things, based on the current culture. Gay used to mean happy (in US) and Fag used to mean a bundle of sticks (in US), now everyone thinks of those words as meaning homosexual, of course in England a fag is a cigarette (makes since).

Anyway; just don’t do that again - ok.

If you have any other pieces of code like this laying around and can explain it at this level, please write another article on it.

John R. Shaw

INTP"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

You did a great job in explaining the mathematics behind your fractals program. I remember the days when I was reading Robert Lafore's "Windows Programming" book, it contained a fractal program but to understand the mathematics behind it was a boon for me at least. So, great job.

We don't talk politics here, and the article has absolutely nothing to do with you or your ethnic background nor it represents any person/group in real world. Just because your imagination can give it different meanings doesn't mean it represents anything. We've grown up drawing those fractals ever since we were kids and when we were programming back in Pascal times!!!

As a matter of fact that's exactly what it is. Your mind sees what you've been conditioned to see. I'm not saying that's a bad thing, simply pointing it out. That's how our brains work.

When I look at the symbol, I see something the Nazis used, but I also see a symbol the Greeks revered in addition to dozens of ancient cultures. Finally, I see a shape with interesting mathematical properties that makes it ideal in a discussion of fractals.