Fractals Exploration/Lab

HONR 300/CMSC 491:
L-Systems Lab / Fractals Exploration Assignment
Wed 2/16/11 (Fractals Exploration due Mon 2/28/11)
You may do the lab individually or in pairs. As with all assignments, you may work in
small groups to discuss the fractals exploration assignment, but the final assignment
that you turn in should be your own. If you work with another student on the
“scavenger hunt,” that’s fine; but by turning in the answer, you are asserting that you
fully understand the answer yourself and are not just blindly submitting what another
student figured out.
In-Lab Activity:
Getting Started





Start NetLogo using the Windows Program menu.
Open the L-Systems model: File -> Models Library -> Mathematics -> Fractals -> LSystem Fractals
Click the Swirl button, then click Go to create the Swirl fractal. Click Go again
to stop the model. (Depending on how long you let the model run, it may
take a few seconds for the model to stop. Caution: you should always stop the
model before trying to start a different model.)
Experiment with restarting the Swirl model (click Swirl again, then Go again—
don’t use the Setup button just yet, since that changes to the default model).
Let the system run longer this time. You will probably notice that the system
slow down a lot, because the model is creating new turtles at an exponential
rate.
Try creating each of the built-in fractals listed at the bottom of the control
panel (Swirl, Ball, Tree1…). After selecting a fractal, you can click Go to let
the model run indefinitely, or Go Once repeatedly to see how the fractal is
constructed one step at a time.
Examining a Simple Fractal




Let’s look at how one of the L-systems is written more carefully.
Click on Setup, then Go Once a few times to see how this fractals grows.
Go to the Procedures tab and look at the line of code just below the commented
region that says “write your own rules here.” The line reads:
fd 3 rt 15 spawn fd 3 lt 30 fd 3
This is a simple “turtle program” that is applied on each iteration to every
turtle in the system. Initially there is just one turtle, at (init-x, inity) (which
defaults to (0, 0), as you can see in the Interface sliders). Breaking it down,
when you click Go Once, each turtle will:
o Move forward three pixels.
o Turn right 15 degrees.
o Create another turtle at the current location (which, on the next
iteration, will start running the same program from that location and
orientation).


o Move forward another three pixels.
o Turn left 30 degrees.
o Move forward three more pixels.
Every turtle also changes its color slightly after each “round,” so you can see
the age of each turtle visually in the display.
Go to the Interface tab, restart this model (using Setup), and see if you can get
an intuition for how this program builds a tree-like fractal. After the first
iteration, you’ll see a white vertical line (the turtle’s forward movement),
capped by a white dot (the spawned turtle), followed by a pink line that
bends right, then left, and is capped by a pink dot (the original turtle). Make
sure you understand this fractal thoroughly before moving on.
Examining Recursive Fractals


In the book, Flake creates recursion with bracketed grammar expansions, in
which each successive “generation” is decremented in size by a fixed ratio
(given as a switch to the commands).
In NetLogo, the recursive Tree1 fractal rule looks like this:
fd 4 rt 15 fd 8 spawn rt 180 skip 8 rt 180 lt 15 fd 4 lt 15



spawn fd 8 die
Note that the NetLogo model creates a recursive fractal by spawning,
backtracking over the turtle’s path, then spawning again—and in this case,
stopping the original turtle at the end of the cycle.
Look at the NetLogo model for Sierpinski’s Tree. It implements the Chaos
Game that I showed you in class on Monday. Work back and forth between
the code and the model interface to understand what’s happening as each
point generates a new midpoint between itself and a vertex.
Koch’s Snowflake builds the simplest snowflake first; on each subsequent
iteration, every edge of the snowflake becomes a new Koch curve with edges
that themselves become Koch curves. Again, spend some time tracing
through and understanding this more complex model.
Note that both the Sierpinski and Koch models leave behind “traces” of the
process that was used to build them (e.g., instead of peaks along the edges of
the Koch snowflake, there are filled triangles).
Building a New Fractal






Go back into the Procedures tab, and scroll to the bottom.
Copy the to setup-koch code block and the to koch code block below it.
Paste this copy below the existing code.
Change setup-koch to setup-mykoch, and set rule-set “koch” to set rule-set “mykoch”.
Change to koch to to mykoch.
In your new mykoch procedure, delete the first part of the ifelse ticks=0 block,
leaving only the last line (from which you need to remove the square
brackets):
spawn fd len lt 60 spawn fd len rt 120 spawn fd len lt 60 spawn

fd len rt 120
Return to the Interface window and create a button to run your new
procedure:

o Click the + ADD button at the top, then choose Button from the dropdown slider to the right of +ADD.
o Click where you want the new button to appear.
o You’ll see a popup dialog window; type setup-mykoch in the Commands
text box.
o Close the dialog window.
o Click your new setup-koch button, then Go or Go Once. You should see a
(vertical) Koch curve grow itself.
Congratulations—you’ve written your first NetLogo model!
Fractals Exploration Assignment
Getting Started
You will need to install NetLogo on your own machine. It’s very straightforward:
just visit http://ccl.northwestern.edu/netlogo/and download the appropriate version for
your machine. (NetLogo runs under Mac, Windows, and Linux.)
You may want to use the NetLogo manual to help you with this assignment; it’s
available on the NetLogo website and is linked under the Help menu in the NetLogo
interface.
To save models, use the File…Save As command. You can open models from the File
window, or by double-clicking on the saved model in a directory window.
To save a screen capture, use File… Export… View (to export just the display window
containing the fractal drawing), or File… Export… Interface (to export the entire
interface window, including the buttons and sliders). Be sure you name this file
with a name ending in “.png” – NetLogo creates a PNG file but won’t put the file
extension on automatically.
1. Fractal Scavenger Hunt (40 points)
Here are six fractals that I created by replacing the line of code in the apply-rules
procedure in the NetLogo L-Systems model. Use your understanding of fractals to
determine the code that will generate each of these fractals. (You should submit
your answers in hardcopy; your answer here is just the line or lines of code to
replace apply-rules with to generate the fractal. Feel free to add additional remarks or
observations if you want.)
You may want to look at the full-size pictures in color to see the details more clearly;
I’ve posted them on the course website.
HONR 300 students: You only need to identify the first four fractals. Each fractal
requires just a single line of code.
CMSC 491 students (and HONR 300 students who want the extra challenge): To
generate the last two fractals, I also modified the setup procedure to set the turtles’
initial len parameter, using this code:
ask turtles [ set len 1 ]
(len is set to 1 for Fractal 5, and to 5 for Fractal #6.) These fractals also require
multiple lines of code that modify the length on each iteration, as well as moving the
turtle. The code includes a stopping condition to cause the turtle to die when a
maximum length is exceeded. The fractal in the picture is the final view, and you
should include the stopping condition in your code.
Fractal #1: Circle (Note: I changed init-x to -50 in the interface ; init-y is still 0.)
Fractal #2: Dodecahedron (I changed init-x to -50; init-y is still 0.)
Fractal #3: Shrubbery (Here, init-x is 0; init-y is -50. I applied “Go Once” ten times to
generate the 10th level of this fractal.)
Fractal #4 (Init-x is 0; init-y is -50. I applied “Go Once” ten times to generate the 10th
level of this fractal.)
Fractal #5
Fractal #6 (Init-x is 0; init-y is -100. I applied “Go Once” 12 times to generate the 12th
level of this fractal.)
2. Create Your Own Fractal (60 points)
Create your own fractal program in NetLogo, and tell me about it.
For this assignment, it’s fine to start with the L-System program or one of the other
existing NetLogo programs.
 If you are in HONR 300, you can just experiment with the parameter settings
to explore the space of fractals that can be created with the program.
 If you are in CMSC 491, you should modify the program in some significant
way to create a family of fractals that wouldn’t have been possible with the
existing code. This new fractal family should involve at least one new
parameter, which you should add to the interface as a slider. (For example, a
“middle p% Koch curve” might build a peak from a fraction of each edge
other than 1/3 – for example, if p was .5, then you would get the “middle
half” Koch curve that we talked about in class.)
 You’re welcome to go beyond these requirements: HONR 300 students can
modify models to your heart’s content; and any student can create a new
model from scratch if you so choose.
After creating your new model, you should experiment with it to understand the
space of fractals that it can generate, and how the parameters affect the way the
fractal looks. You should write a short summary of how your model works,
including some screen captures of different variations of the fractal. This report
should not be longer than two pages.
You should submit the model itself by saving it as a “.nlogo” file in the “Save As”
dialog, and uploading this to the “Fractal Model” project in Blackboard. Your model
summary should be submitted as hardcopy, along with the answers to the fractal
identification exercise.