text stringlengths 0 897 |
|---|
t.forward 100 * scale |
// draw sub-branches |
t.left 70 |
drawBranch t, scale * 0.4 |
t.right 70 * 2 |
drawBranch t, scale * 0.4 |
t.left 70 |
drawBranch t, scale * 0.6 |
end function |
drawBranch new Turtle |
``` |
This is a pretty direct translation of the pseudocode. The only "trick" here is on line 7, where we take the Turtle object that was passed in to our `drawBranch` function and create a `new` turtle derived from it. That allows us to do whatever we like with this new turtle — move it, rotate it, change its pen size or ... |
So type that in, if you haven't already, and give it a try. The result should look like this: |
{width: "25%"} |
 |
{gap:30} |
Looks pretty good! And now we can make an entire snowflake by just drawing it six times, turning 60 degrees each time. Just replace line 20 of the program above with: |
{number-from: 20} |
```miniscript |
t = new Turtle |
for i in range(1, 6) |
drawBranch t |
t.left 60 |
end for |
``` |
And now you have a beautiful snowflake! |
## Introducing Variation |
This code, of course, will draw the same snowflake every time you run it. But there are lots of magic numbers in it, which could be varied to produce different results: the length and width of the main branch, the angle of the side branches, and the scale of the side and forward branches. We could also tweak the colo... |
For our *Snowflake Evolution* program, we're going to wrap up all those adjustable bits into a little class called `Step`. And we'll go a little further: instead of strictly drawing the exact same branch at every level of the recursion, we'll keep a list of four different step variations, and switch to one of those wh... |
This will all likely be clearer in MiniScript than in English, so `reset` your program, and dive right in. |
{i:"`/sys/lib`,`turtle`;`/sys/lib`,`mathUtil`"} |
{caption: "Listing 1 (Start of Snowflake Evolution program)."} |
```miniscript |
import "turtle" |
import "mathUtil" |
clamp = @mathUtil.clamp // (we'll be using this a lot) |
clear |
// randColor: return a random (bright) color, 155-255 in RGB. |
randColor = function() |
return color.fromList([155+99*rnd, 155+99*rnd, 155+99*rnd]) |
end function |
// Step class. Draws one branch of a snowflake |
// (using recursion to draw sub-branches). |
Step = {} |
Step.newRandom = function() |
result = new Step |
result.penSize = round(5 + 10*rnd) |
result.penColor = randColor |
result.fwd = 25 + 50*rnd |
result.sideAngle = 20 + 60*rnd |
result.sideScale = 0.25 + 0.5*rnd |
result.sideStepNum = floor(rnd * 4) |
result.fwdScale = 0.25 + 0.5*rnd |
result.fwdStepNum = floor(rnd * 4) |
return result |
end function |
Step.draw = function(turtle, steps, scale=1) |
if scale < 0.1 then return |
t = new turtle |
t.penSize = self.penSize * scale |
t.color = self.penColor |
t.forward self.fwd * scale |
newScale = scale * self.sideScale |
if self.sideAngle > 0 and steps.hasIndex(self.sideStepNum) then |
t.left self.sideAngle |
steps[self.sideStepNum].draw t, steps, newScale |
t.right self.sideAngle * 2 |
steps[self.sideStepNum].draw t, steps, newScale |
t.left self.sideAngle |
end if |
newScale = scale * self.fwdScale |
if newScale > 0 and steps.hasIndex(self.fwdStepNum) then |
steps[self.fwdStepNum].draw t, steps, newScale |
end if |
end function |
// Snowflake class: keeps four Steps, and uses them to |
// draw a complete snowflake. |
Snowflake = {} |
Snowflake.newRandom = function() |
result = new Snowflake |
result.steps = [] |
for i in range(1, 4) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.