text
stringlengths
0
897
result.steps.push Step.newRandom
end for
return result
end function
Snowflake.draw = function(x=480, y=320)
t = new Turtle
t.penDown = false
t.goTo x, y
t.penDown = true
for i in range(0,5)
self.steps[0].draw t, self.steps
t.left 60
end for
end function
flake = Snowflake.newRandom
flake.draw
```
The recursive drawing algorithm from the previous section is now implemented in the `Step.draw` method. It looks a little more complicated because the function takes a list of `Step` objects, each specifying a different set of properties for the branch. So when it is time to draw one of the angled side branches, or t...
A `Snowflake` is just a collection of four different `Step`s. It also has the `draw` method that kicks off the branch-drawing process six times in six different directions, making a complete snowflake.
{pageBreak}
D> Why four different `Step` objects? I knew I wanted more than one, and ten seemed excessive, given the recursion rarely goes more than a few levels deep anyway. Four seemed to work nicely, but you should feel free to try other numbers! Be sure to change the `4`s in both `Step.newRandom`, and in `Snowflake.newRando...
Once you've entered the listing above, run it several times. You should get a different snowflake each time!
## Mutant Clones
Our *Snowflake Evolution* app requires creating several variations of one snowflake. So we will need some code to both *clone* a snowflake, and to *mutate* it.
clone
: to create a copy of an object
mutate
: to introduce (usually small) changes to an object
Clear out the last couple of lines (test code) from your program, and continue with the code below.
{caption: "Listing 2 (Cloning and mutation).", number-from: 72}
```miniscript
Step.mutate = function()
i = floor(rnd * 8)
if i == 0 then
self.penSize = clamp(self.penSize + rnd*2-1, 1, 15)
else if i == 1 then
self.fwd = clamp(self.fwd + rnd*20-10, 10, 100)
else if i == 2 then
self.sideAngle = clamp(self.sideAngle + rnd*20-10, 0, 170)
else if i == 3 then
self.sideScale = clamp(self.sideScale + rnd/2-0.25, 0, 0.8)
else if i == 4 then
self.sideStepNum = floor(rnd * 4)
else if i == 5 then
self.fwdScale = clamp(self.fwdScale + rnd/2-0.25, 0, 0.9)
else if i == 6 then
self.penColor = randColor
else
self.fwdStepNum = floor(rnd * 4)
end if
end function
Step.clone = function()
return {} + self
end function
Snowflake.cloneAndMutate = function()
result = new Snowflake
result.steps = []
for step in self.steps
result.steps.push step.clone
result.steps[-1].mutate
end for
return result
end function
flake = Snowflake.newRandom
while true
clear
flake.draw
if key.get == char(27) then break
flake = flake.cloneAndMutate
end while
```
This listing includes somewhat longer test code, which generates a single mutated clone every time you press a key (until you press Escape, which is `char(27)`; then it breaks the loop).
Most of the work here is being done by the `Step.mutate` method, which picks a random attribute (`i`), and then randomly changes the corresponding property. The `clamp` method being used here (imported from the `mathUtil` module) simply limits a value to a valid range. This is especially important for the `sideScale`...
D> What if, say, `fwdScale` were *not* less than 1? Remember that this controls how big the next section of the branch is, relative to the last section. And also remember that the recursion stops when the branches get too small. So if the next part of the branch were actually *bigger* than the previous part, what wo...
## Main Program
We're just about done now. The rest of the program draws six flakes at a time, waits for the user to pick one with the mouse, and then generates five new ones — mostly mutant clones of the one selected.
Delete the test code starting on line 107, and then type in the following to finish the program.
{caption: "Listing 3 (Wrapping up *Snowflake Evolution*).", number-from: 107}
```miniscript