Wednesday, May 27, 2015

Exploring Dragon Curves in TurtleArt

Vi Hart's video about dragon curves helped me understand the math behind this wonderful fractal.

Once I understood the dragon curve to be a series of 90° turns that occur in an either/or logic, I adapted the math to a TurtleArt simulation.

In order to make as large a dragon curve as we can potentially fit in the TurtleArt screen, we will keep the turtle's forward movement small. Let's start with forward 5. It is such a small movement that the turtle's mark will be obscured by its body.

As Ms. Hart explains in her video, the turtle should then make the decision to turn left or right. There are turtle movement blocks to make the turtle turn.

However, stacking those blocks would make the turtle turn right, then left, effectively canceling the turn.

Instead, we will use the set heading (seth) block. This allows us to set the turtle's heading in a specific direction by specifying the degree to which the turtle faces. 

We still need to build in a basic logic in the turtle to decide whether to turn left or right relative to the position it is currently facing. We will use the one of block in conjunction with the set heading block to achieve our goal.

When this block runs, sometimes the turtle faces -90 degrees from its current position, while other times it faces 90 degrees from its current position. In the examples below I reset the turtle to 0° before running the procedure.

Finally, program the turtle to move forward 5, and name the procedure. I used Ms. Hart's naming convention.

The red mark is a little heavy. We can make the turtle's pen smaller.

A master procedure to erase the screen, set the pen size and color, and call the zigzag procedure produces a program to generate dragon curves.

Each time the procedures runs a different dragon curve is produced.

Once you have a working zigzag procedure that you can use to produce dragon curves, remix it. Make the turtle go a little farther forward to create a larger dragon curve.

Combine the original zigzag procedure with your new bigzig procedure in a new master procedure that draws both a small dragon curve and a large dragon curve.

Running a large and small dragon curve together produces interesting variations.

Using the method described in the 3D Printed TurtleArt Tiles chapter of my book, you can also produce 3D prints of dragon curves.

Dragon curves are a fractal that, as it turns out, is a fun programming challenge for TurtleArt. The beauty lies in the fact that the procedure creates a different dragon curve each time it runs, art as elusive as the dragons of myth. Save a copy of the designs you like before running the procedure again. 

Once the master procedure clears the screen, you will never see that dragon curve again!

No comments: