The Pray Fractal

The Pray Fractal (and the Dragon Curve)

Andrew Pray

What is a fractal?

A fractal is a geometric shape that is created from various rules, that self replicates itself, or patterns of the grand design.  Often very chaotic early levels come to form a much more defined image.  As one zooms into a fractal, similar patterns emerge at a smaller scale.

Early Decisions:

Very quickly I discovered that in order to create any sort of fractal, you have to understand what mathematical equations go on behind the scenes. What formulas are constantly being repeated?  What rules do I need to follow?

To learn what math I had to code I first turned to Desmos.

The functions on the left are what created the triangle on the right.  The important part is that I can move the base angles to any x or y coordinate and a right triangle will still be drawn.  The dragon fractal goes on to erase the base line, then repeat the pattern on the two legs.  If you keep doing this, eliminating the base line and repeating the pattern, the dragon fractal is born!

One interesting problem:

There are a few ways a code can create a fractal.  One uses array lists and creates new objects.  I created a recursive function that calls itself inside the function.

drawFract(ax, ay, bx, by, nlevel, difD);

Every frame I call for this function.  I made the parameters variables so I can later change them inside the program.

void drawFract(float x1, float y1, float x2, float y2, int level, float difD) {
float x3=((x2-x1)/2)+x1;
float y3=((y2-y1)/2)+y1;
float d=sqrt(sq(x3-x1)+sq(y3-y1))/difD;
float T= atan((y3-y1)/(x3-x1));
float x4=x3+d*(cos(T+HALF_PI));
float y4=y3+d*(sin(T+HALF_PI));
if (level==1) {
line(x1, y1, x4, y4);
line(x2, y2, x4, y4);
}
if (level > 1) {
level = level - 1;
drawFract(x1, y1, x4, y4, level, difD);
drawFract(x2, y2, x4, y4, level, difD);
}
}

This is the code I initially came up with.  However, something very unexpected came from this code.  This is the fractal, 14 levels deep.

  This is the fractal’s coordinates: A:(5,520) and B:(-10,0)

It reminds me of the Sierpinski Triangle.  I’ve never seen or heard of this fractal.  So, we called it the Pray Fractal.  This is the fractal at A:(5,10) and B:(-660,-5) Obviously, something has gone wrong.  I went through each interval one by one and compared it to the dragon curve’s equivalent.

  

So I figured it out:  It is a problem with negative values.

float T= atan((y3-y1)/(x3-x1));
float x4=x3+d*(cos(T+HALF_PI));
float y4=y3+d*(sin(T+HALF_PI));

In my code, when x3 is less than x1, T becomes negative, making the following two lines place the coordinate inverted.  Three lines of code fixed the problem:

if (x2>x3) {
x4=x3+d*(cos(T-HALF_PI));
y4=y3+d*(sin(T-HALF_PI));
}

Other features I added to my program were various key presses that allow the user to zoom in and out, mouse click to drag the image, and key presses that change the parameters.

Last thing:

I ended up saving multiple images of the fractal zooming in on itself in a data folder.  This way the images could be placed on top of each other in a 3D modeling program.  At that point, the fractal could be 3D printed.

Leave a Reply

Your email address will not be published. Required fields are marked *