# Fractals

## Would you like to draw these figures?

Star grade 4: Koch Star grade 5: Partial Square Koch Star grade 5: Uniform Triangle Mass Center grade 5: Uniform Triangle Mass Center grade 7 (just an image): 3 Sides Regular Polygon grade 3: 4 Sides Regular Polygon grade 4: 5 Sides Regular Polygon grade 4: 7 Sides Regular Polygon grade 2: 10 Sides Regular Polygon grade 4: All these images are fractals...
Fractal is a "thing", structure or a shape that has at least a part that no matter how much zoom you do it, you will never rich the end. This is, you will continue to see changes. For example, this horrible painting I did: It's a guy with a painting in his hand, but if you look at the painting you will see that it contains itself, and so the picture repeats itself over and over. In order to get a fractal you don't necessary need the same shape to exactly repeat over and over, but there must be something that continues "growing".

Again here we can see a strong recursive nature! So to build a fractal we need to identify this pattern or structure that repeats. Of course since we want our program to finish before we die or before the poor computer runs out of resources, we need a base or stop case. Mathematically, fractals never end but we will define a number that keeps track of the amount of times the figure was generated, starting at zero to denote that it was generated once. This is called the grade of our fractal.

## Let's build our fractals!

I will give you some tips and ideas about how to draw all these fractals. You will see it's not difficult! Also at the bottom of this page there is a link with all the source code!
You will need some basic knowledge of geometry and some framework to draw lines (I'm using: Graphics2D). I'm assuming you already know that but if not, let's continue anyway and you can cheat in the sourcecode or somewhere else if you get stacked with something.

## Okay, now what?

First pick your structure, let's go with the star: So in this case, we need to find a way to draw this star and then call this algorithm as much as we want but each time drawing smaller stars! This can be done recursively or iteratively!
Take a look at the grade 1 star: The inner star is the same as the outer star but it is rotated 180º and each start is rotated 180º relative to the previous one. You will need to do some math (or take a look at the source code) to calculate the length of the inner star segments.

Let's analyze now the Koch star, here of grade 0 to 2:   By looking at these pictures, you can say that Koch 0 is just a triangle. Koch 1 is just two triangles but with the "inside lines removed". Koch 2 is 6 "Koch 1" with the "inside lines removed" And so on...
But thinking this way will make it hard...
The key here is to realize that the smaller recursive unit is not the triangle but each of it lines. So to create a full koch we well need 3 calls, one for each side of the triangle!
Let's call these lines, Koch lines. If we are in grade 0 each Koch line is a straight line. What if not? Then a Koch line grade n is composed by 4 Koch lines grade (n-1) (note that this is a recursive definition). Therefore Koch line grade 1 is made of 4 Koch lines grade 0, this is, 4 straight lines as shown below: As you know, the internal angles of a triangle according to Euclidian geometry always sum 180º. If we look at Koch grade 0 we have 1 equilateral triangle therefore, by symmetry, the angles must be equal to 180º/3, this is: 60º. This remains true for any Koch grade!
Also, note that here a non recursive solution is not trivial!

## Partial Square Koch Star

Before programming the Koch Star Fractal, I did this one for a simple reason: It was easer to work with 90º angles than with 60º. Was it? Nahh, is pretty much the same but why is it called "Partial"?
Take a look at it: mmmm something seems to be missing...
Similarly as in the Koch Star, here we have 4 lines or segments. In grade 0 these are straight lines. A line of grade n will be composed by 5 lines of grade (n-1) (again, note that this is a recursive definition) as follows: ... but see what happens if we generate our fractal following this definition:    We got another fractal! The only difference is that in the "Partial" the first and last segments are always straight lines! By doing this we are avoiding the overlapping. Also note that if we look at the black background we get a figure that is has the shape of previous levels of the fractal!! Amazing property!!

## Uniform Triangle Mass Center

This is my favorite fractal because I came up with this idea all by my own! Yes, I know it very likely someone else did it first but it's still my creation :P
So what we got here is a triangle for grade 0 with 3 lines from each of the 3 vertices to the mass center: By doing this we have divided the triangle in 3 more triangles and now we can recursively continue drawing lines from the new vertices to the new mass centers!  Imagine doing this non recursively!
Note that we are not drawing triangles in each recursive call, only lines from the each of the 3 vertices to the mass center. We only needed to draw a triangle for the 3 initial vertices or we could avoid drawing it and we will get this:    It's interesting to note that for a big grade both fractals tend to be the same!

source code and runnable 