Fractal generator assignment (Maya/Python)

For my programming assignment here at the NCCA in Bournemouth), I chose to make a fractal generator.

You can generate various fractals through and L-System, and the Pythagorean tree through a turtle library I wrote.

I can’t upload the source code, since other students next year might run away with it. Sorry guys! :-). Feel free to contact me for some advice though. Always happy to help.

Click on the read more tag to.. well.. read (quite a bit) more.


 

Introduction

When the assignment was set and I had to make the choice about which project I was going to tackle, I had an easy time deciding. I had been using the Hilbert Curve as my render bucket order for a while, so I was very interested in how to program and visualize this.

Not knowing a single thing about fractals, I dove into making a fractal generator. Quickly became clear that I had two main routes I could take to reach my goal. The first one being using a Lindenmayer System and the second one being a so called Turtle.

After spending some time reading about both approaches, I decided to go for the L-System. Not only did I feel more intrigued by it, I also felt like I would learn more while writing it.

 

An L-system is a parallel rewriting system and a type of formal grammar. They were introduced and developed in 1968 by Aristid Lindenmayer, a Hungarian theoretical biologist and botanist at the University of Utrecht. (Wikipedia)

While a parallel rewriting system sounds pretty complicated, it is actually relatively simple. For example, you start with a string of characters named the axiom, e.g. “L”. You could easily compare this with the seed of a plant. Then you have one or several production rules, e.g. “L” -> “+RF-LFL-FR+” and “R” -> “-LF+RFR+FL-”. When you apply the production rules to the axiom the first time, you end up with “+-LF+RFR+FL-F-LFL-F-LF+RFR+FL-+”. The more times you apply these rules, the longer your string becomes, the higher your seed will grow.

Now you may think: “What are these strings?! What do they do?! This is looking like Chinese to me!” The answer is relatively simple. Each of the characters in the string represent an action, for example go forward, turn left, turn right, etc. But more on this later on in the Implementation section of this report! While the approach of rewriting and visualizing the strings worked excellent for almost all fractals, I failed to create the Pythagorean tree with it. This is why I also wrote my own separate Turtle module.

Because this is a lot simpler and uses exactly the same mathematical principles as the L-System I wrote, I will not dig into it.

 

Implementation

 

Overview – main flow
Before I dive into details, I feel like I should start off by explaining the main flow of the script. When the “Create Fractal” button is pressed, the createFractalButton function is called. The main purpose of this function is to pass some variables and decide whether to use the L-System or the Turtle system based on the preset name.

01
I also provided buttons for re-initializing the script and for adding the script to the current Maya shelf. I will not go in depth into these since they are so simple.


 

 

There are two big parts to the L-System I wrote, the rewriting algorithm and the visualisation algorithm. Since the rewriting algorithm is at the very core of the code and is executed first, I will
start with this.

Lindenmayer System

 

02
As I briefly mentioned in the introduction, central to L-systems is the notion of rewriting, where the basic idea is to define complex strings of characters by successively replacing parts of a simple string using a set of rewriting (production) rules. The rewriting can be carried out recursively.

The actual rewriting is very straightforward and can be done in many ways. The big keyword here is recursion. So… What is this? It is as simple as a program calling itself until some final point is reached. This is important, otherwise it would, in theory, go on forever.

The L-System I wrote is deterministic (D0L), as opposed to stochastic. This means that there is no randomization involved. Having a stochastic L-System is very important if the purpose of it is, for example, making believable vegetation. However, for drawing well known fractals, I did not see the need to implement it.

Visualisation

To explain how the visualisation works, I have to introduce Achilles. Achilles, believe it or not, is a turtle living on the lovely Bournemouth beach.

Now imagine that you can give Achilles directions. For example, go forward. Turn 90 degrees to the left! Go forward again! Achilles, turn 45 degrees to your right! Now go forward, but lift up your tail! You would quickly see some interesting shapes appearing in the sand.

After the recursive rewriting process is finished, we end up with a long string of characters. The key is to give these individual characters a meaning, and iterate through all of them exactly one time.

The different available commands are:

“F”: Moves the turtle forward by a specified distance in the direction the turtle is headed, and creates
a curve.

“f”: Moves the turtle forward by a specified distance in the direction the turtle is headed, without creating a curve.

“-“: Turns the turtle to the left by a specified angle

“+”: Turns the turtle to the right by a specified angle

“[“: Appends the coordinates and angle to a list, this is mainly used for branched fractals like tree structures.

“]”: Pops the coordinates and angle from the list, this is mainly used for branched fractals like tree structures.

“@”: Divides the length of the individual segments by a float number following the character, e.g. “@0.7”

“@Q”: Divides the length of the individual segments by the square root of the following character, e.g. “@Q2”

“@I”: Divides the length of the individual segments by the reciprocal of the following character, e.g. “@I3”

“@IQ”: Divides the length of the individual segments by the square root of the reciprocal of the following character, e.g. “@IQ2”

Results

As far as the actual visualization of my project goes, I opted for curves instead of geometry. Since this is less memory intensive it allowed me to create bigger fractals.

For presentation purposes I rendered it out with a render curves function of Solid Angle’s Arnold.

The fractal presets include:

  • Hilbert Curve
  • Terdragon Curve
  • Pythagorean Tree
  • Y-Tree
  • Hilbert II Curve
  • Moore Curve
  • Sierpinski Arrowhead Curve
  • Sierpinski Curve
  • Koch Curve
  • Quadratic Koch Curve
  • Koch Snowflake Curve
  • Koch Antisnowflake Curve
  • Cross-stitch Curve
  • Box Curve
  • Quadratic Koch Island Curve
  • Harter-Heighway Dragon Curve
  • Peano-Gosper Curve
  • Minkowski Sausage Curve
  • Levy Curve
  • Quadratic Gosper Curve
  • McWorter’s Pentigree Curve
  • Tree Structure

 

Hilbert Curve, 5 iterations, Axiom: L, Turn: 90
L +RF-LFL-FR+
R -LF+RFR+FL-

preset_Hilbert
Terdragon Curve – 6 iterations, Axiom: F, Turn: 120
F F+F-F

preset_Terdragon
Pythagorean Tree, 5 iterations, manually written action sequence.

preset_PythagoreanTree

3 Comments

Post a Comment

Powered by themekiller.com