HONR 300/CMSC 491: L-Systems Lab / Fractals Exploration AssignmentTue 2/16/16 (Fractals Exploration due Thu 2/25/16)

You may do the in-class lab individually or in pairs. However, the fractals exploration assignment should be turned in individually. As with all assignments, you may work in pairs or small groups to discuss the assignment, but the work that you turn in should be your own. If you talk to other students about how to do the “scavenger hunt,” that’s fine; but by turning in your answers, you are asserting that you fully understand the answers 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 -> L-System Fractals

·  Click the Swirl button, then click Go Once several times to create the Swirl fractal.

·  Experiment with restarting the Swirl model (click Swirl again, then Go Once again—don’t use the Setup button just yet, since that changes to the default model).

·  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 also click Go to let the model run indefinitely, or Go Once repeatedly to see how the fractal is constructed one step at a time. Click Go again to stop the model. (Be careful when using the Go command – these fractals can exhibit exponential growth and can really slow down your computer! Depending on how long you let the model run, it may take a few seconds for the model to stop after clicking the Go button. In the worst case, you may have to manually kill and restart NetLogo. Caution: you should always stop the model before trying to start a different model.)

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 fractal 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 of code 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, init-y) (these values can be set using 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 (“spawn”) 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.

·  Question: How many turtles will there be after k time clicks?

·  Question: How far (in total pixels traveled in any direction) will the initial turtle have traveled at the end of k time clicks?

·  Every turtle also changes its color slightly after spawning a child (you can see this in the spawn procedure), so you can see the age of each turtle visually in the display. New turtles are set to color “init-color,” and the color value is incremented by “color-inc.” (Colors in NetLogo are defined by a numeric value from 0 to 140; see http://ccl.northwestern.edu/netlogo/docs/programming.html#colors to learn more about colors if you’re interested.)

·  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.

·  Bring up the “turtle monitor” (choose “Turtle Monitor” under the “Tools” menu) and type “1” in the “who” field to watch the attributes of the first turtle as the fractal grows. (You’ll only be able to do this after you’ve clicked “Go Once” at least once, since the turtle doesn’t exist before then.)

·  Question: What is the display window in the turtle monitor showing? What does the slider do? What is the meaning of the “heading” attribute, and what units is it in?

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 (“rt 180” makes a U-turn; “skip 8” jumps forward 8 without leaving a trail; “rt 180” makes another U-turn), then spawning again—and in this case, killing the original turtle off at the end of the cycle. You can see this code in the Tree1 procedure in the Procedures tab.

·  Question: How many turtles will there be after k rounds of Tree1?

·  Look at the NetLogo model for Sierpinski’s Tree. It implements the Chaos Game, which we saw in class last week:
http://www.shodor.org/interactivate/activities/TheChaosGame/

·  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.

·  In this copied code, change setup-koch to setup-mykoch, and set rule-set “koch” to set rule-set “mykoch”.

·  Change to koch to to mykoch in the line set rule-set “mykoch” in the setup-mykoch procedure.

·  In your new mykoch procedure, delete the first part (ifelse and repeat) 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 drop-down 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 and mykoch in the Display name text box.

o  Close the dialog window by clicking OK.

o  Click your new mykoch button, then Go or Go Once. You should see a (vertical) Koch curve grow itself.

·  Congratulations—you’ve written your first NetLogo model and reached the end of the lab!

Fractals Exploration Assignment

Getting Started

You will probably want to use the NetLogo manual to help you with this assignment; it’s available on the NetLogo website and is linked under the User Manuals - Web menu in the NetLogo interface. I find the “NetLogo Dictionary” to be the single most useful piece of documentation.

To save the models you create or modify, 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. To get full credit, your code must replicate both the color and the scale of the fractals precisely. In all cases, init-colors is 9 and color-inc is 5.


HONR 300 students: You only need to write the code to generate 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. You should not use the Skip command for any of the fractals. The code for the Spiral fractal (#5) 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 tab ; init-y is still 0.)

Fractal #2: Dodecagon (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: Tree (Init-x is 0; init-y is -50. I applied “Go Once” ten times to generate the 10th level of this fractal.)

Fractal #5: Spiral

Fractal #6: Frond (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. You should also include the code itself from the Procedures tab (you can just cut and paste it). This report should not be longer than two pages (not counting the code), and should be printed and handed in as hardcopy.

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.