L-System Playground
My Project Explosion Danger Mountain Fractal Tree Credits
play_arrow refresh camera
30 90 90 1 #006600 EQ AND TRUE 10 WHILE i 1 10 1 BREAK 123 ADD 1 1 ROOT 9 SIN 45 PI EVEN 0 ROUND 3.1 SUM 64 10 50 1 100 1 100 #ff0000 100 50 0 #ff0000 #3333ff 0.5 Click the menu button in the top right to see some examples. (as well as the all important credits!) *Try out some of the challenges, They're the notes inside *'s =P * 1 L-Systems are a way to create drawings by using rules you make up. The L-system is made up of a list of pieces, where each piece has a rule for replacing it and a rule for drawing it. Replace rules change the list by adding pieces. Draw rules tell the turtle how to draw that list. The L-system starts by going through the list of pieces and using the replace rules. *Try changing the "replace list 5 times" number on the first block to a different number. See what happens when you hit play.* Then it uses the draw rules to draw the final picture. *Click on the blue question mark circles (?) for more clues* This block controls the whole L-System. It creates the initial list, uses the replace rules to add to the list however many times you tell it to. And then uses the draw rules to draw the list. beam This block adds the first "beam" piece to the L-System. After you hit the play button the "replace" and "draw" rules will be applied to it. *Try adding more "beam"s (or other pieces!) to this list and see what happens* 5 beam This block creates a replace-rule. Which means that whenever the L-System comes to a "beam" piece it uses this rule to replace it. This rule says "replace the 'beam' piece with two 'beam' pieces". *Try adding more "beam"s (or other pieces!) to this list and see what happens* beam This block says add a "beam" piece to the list. beam This block says add a "beam" piece to the list. beam This block creates a draw-rule. Which means that whenever the L-System comes to a "beam" piece it uses this rule to draw it. This rule says: "Turn right a random number of degrees from 1 to 90. Then set the line color to red. Then set the line width to 3. Then move forward 100. Then turn 180 degrees so that we're facing where we came from. Then move forward 100, so we're back where we started." ***What happens if you change the numbers and colors on the blocks?*** If you don't like how it looks you can always change it back =) so go ahead and try things! *Try adding more "Turtle" blocks (the green ones) to the list* This block tells the turtle to turn right a random number of degrees between 1 and 90. *What happens if you remove the "random integer" block?* 90 This block picks a random number between 1 and 90. 1 90 This block sets the line color to red. *Try changing the color to your favorite color* #ff0000 This block sets the line width to 3. *Try changing the width to a different number* 3 This block tells the turtle to move forward 100 pixels. *What happens if you change this number, and the number on the next move forward block?* 100 This block tells the turtle to turn right 180 degrees. Which means the turtle is facing where it came from. *What happens if you change this to a different number?* *What happens if you change it to a different number and remove the "random integer" block?* 180 This block tells the turtle to move forward 100 pixels. *What happens if you change this number, but not th enumber on the previous move forward block?* 100 distance degrees By adding more to your L-System you can create these cool things called fractals. Fractals are like a pattern that when repeated makes a cool shape. Though to make them, we have to be a bit more tricky. *Try changing the "replace list 1 times" number and see what happens* In this example instead of just having 1 piece, we have 5, where all of them have a draw rule, and only 1 has a replace rule. That's a cool thing about L-systems, not ever piece needs both rules. *Can you think of a reason why you would want a piece to /only/ have a replace rule?* This example also includes variable blocks (that's those red ones inside the "start list" block). They are used to store a number or color that you can then use other places. *Try changing the "distance" and "degrees" variables and see what happens* You can put more than just "create piece" blocks inside this block. You can setup variables, as well as include loop and logic blocks. The one thing you can't do is include turtle blocks, that would be too easy! =P distance This sets the "distance" variable to a number. The distance variable is then used inside the "draw forward" rule to tell the turtle how far to move forward. *What happens if you change it to a bigger or smaller number?* 10 degrees This sets the "degrees" variable to a number. The degrees variable is then used inside the "draw left" and "draw right" rules to tell the turtle how much to turn. *What happens if you change it to a bigger or smaller number?* 60 save start This adds a "save start" piece to the piece list. Find this piece's draw rule to find out what it does. forward This adds a "forward" piece to the piece list. *What happens if you add more"forward" blocks?* back to start This adds a "back to start" piece to the piece list. Find this piece's draw rule to find out what it does. 1 forward This block creates a replace rule for "forward" piece. It says that for every forward piece, which looks like: __ Replace it with something that looks like: __/\__ A little mountain =) *Try adding and removing "forward", "left", and "right" blocks. You can create some cool fractals just by changing those blocks!* forward left forward right right forward left forward save start This block creates a draw rule for the "save start" piece. See the "add turtle state to stack" block to learn what it does. *What other blocks do you think could go here?* This tells the turtle to save it's "state" to a "stack". The state means the turtle's line color, line width, position, and rotation. The stack is just like a stack of legos or minecraft blocks, but it lives inside the computer and is made of states. This block keeps adding "states" to the stack, which makes it get taller and taller. Then later you can look back through the stack, and get information about your turtle at different times. back to start This block creates a draw rule for the "back to start" piece. See the "restore turtle state from stack" block to learn more about what it does. This tells the turtle to restore it's "state" from the "stack". The state means the turtle's line color, line width, position, and rotation. The stack is just like a stack of legos or minecraft blocks, but it lives inside the computer and is made of states. This block looks at the very top of the stack (which is the last saved state) and applies the state to the turtle. So the turtle teleports back to where it was, and the direction it was facing, with the same line width and line color. This add the "add turlte state to stack" block make it so the turtle always goes back to where it started when you first hit the play button. forward This creates a draw rule for the "forward" piece. It says: "Set the line width to 3, then move forward by the number stored in the 'distance' variable." *How would you set the line color here?* *Where else could you set the line color?* This block sets the width of the turtle's line. *Try setting the line width with a variable instead of a number.* 3 This tells the turtle to move forward by the number stored in the "distance" variable. 30 distance left This creates a draw rule for the "left" piece. It says: Turn left by the number of degrees stored in the "degrees" variable. *What happens if you remove the "degrees" block from the "turn left" block?* 90 degrees right This creates a draw rule for the "right" piece. It says: Turn right by the number of degrees stored in the "degrees variable. *What happens if you remove the "degrees" block from the "turn right" block?* 70 degrees length factor degrees The faint of heart should beware! For this example shows off the most advanced knock-your-socks-off, shock-your-friends, don't-blink-or-you'll-miss-it tool of L-Systems: Parameters! (...maybe I should have used a more exciting name...) Parameters are like variables, but way cooler. They can change while the L-system is running and are piece specific. Which means you can have a list that looks like this: forward (distance: 4) left forward (distance: 2) right forward (distance: 1) _ / Ahhh a Snake! _ _ _ _/ Can variables do that? Nope! Well they can change while it's running... but they aren't piece specific! *Try adding and removing some parameters from pieces by clicking on the gear icon on a "create piece" block. Simpy go to the menu -> #3 Fractal Tree to reset* This example also includes a "growth piece" which is a piece that has no draw rule, only a replace rule. It is simply used for growing the picture! length factor This variable changes how long older "forward" pieces are compared to newer "forward" pieces. *Try changing this variable and see what happens.* 1.4 degrees You've already seen this before! It tells the turtle how far to turn. 30 save This adds a "save" piece to the list. It saves the turtle's state. growth piece This adds a "growth piece" piece to the list. The growth piece is only used to make the list bigger, it never gets drawn =D *What happens if you add more "growth piece" blocks to the list?* restore This adds a "restore" block to the list. It returns the turtle to it's last saved state. 2 growth piece This piece has no draw rule, only a replace rule. It helps the list get bigger and bigger, so other pieces (like the forward piece) can have other replace rules. *Do you think there's a way to create the tree without using a growth piece?* (I don't know I haven't tried hehe) forward This creates a new forward piece with an initial "distance" value of 10. Then as the "start list" block replaces the list over and over again the forward piece's replace rule will make this piece's "distance" value bigger and bigger. *What happens if you change the "10" to a different number?* 20 save The save & restore blocks are what give the tree it's branching structure. If you give the "replace list # times" a big value (like 10) you can see how the system draws the left branch to the end first (saving it's state all the way) and then backtracks to draw the other branches. The list will look something like this: save, | forward, | save, | | left, | | forward, | restore, | save, | | right, | | forward, | restore restore With all of the extra growth functions removed of course =) left growth piece restore save right growth piece restore forward distance This draw rule says: "Set the line width to 3. Then move the turtle forward by the number stored in the distance parameter." *What else could you store in a parameter?* *What happens if you remove the "distance" block from the "move forward" block?* 3 10 distance forward,1 save See #2 Danger Mountain for more info about saving. restore See #2 Danger Mountain for more info about restoring. forward distance This replace rule uses the parameter to make the older "forward" pieces get longer and longer, because older "forward" pieces get this rule applied multiple times. *Try removing all of the blocks from the "replace growth piece" block, and replace the "growth piece" block in the start list with a "forward" block. Then change the "replace list # times" number and see what happens* forward This block says: "Take this piece's old distance, and multiply it by the length factor" So the distances gets larger and larger as the rule is applied more times. *What happens if you change the dropdown to plus, minus, or divide instead of multiply? (it might be easier to experiment if you make the number stored in "length factor" bigger)* MULTIPLY 1 distance forward,1 1 length factor left *What happens if you remove the "degrees" block from the "turn left" block?* 90 degrees right *What happens if you remove the "degrees" block from the "turn right" block?* 90 degrees