text
stringlengths
0
897
The three algorithms in this chapter are pretty typical of most small, well-defined problems that you might need to solve. Now you've learned the secret every expert programmer knows: you don't have to think up a solution to every problem yourself. In many cases, a good solution (algorithm) is already known, and all ...
Of course, not *every* problem has already been solved by someone else. The number of things a computer can do is infinite, and you have so much creative freedom, you will certainly run into things that no one has done before, or if they have, they failed to publish a nice neat algorithm explaining how they did it. I...
A> **Chapter Review**
A> - You learned about algorithms, and how they relate to programming.
A> - You implemented algorithms for Greatest Common Divisor, Binary Search, and k-Means Clustering.
A> - You learned how to fix an algorithm that is incomplete or contains an error.
{chapterHead: "Day 28: Snowflakes Evolution", startingPageNum:347}
{width: "50%"}
![](Chapter28.svg)
Q> Programming isn't about what you know; it's about what you can figure out.
Q>— Chris Pine (programmer and author)
A> **Chapter Objectives**
A> - Build the *Snowflakes Evolution* app, which lets you guide the development of an endless variety of beautiful snowflakes.
A> - Learn how to design a custom algorithm to tackle a complex computing problem.
A> - Brush up on turtle graphics.
In the previous chapter, you learned about algorithms, and how to convert a pseudocode description of an algorithm into working MiniScript code. Today we're going to learn how to design your own algorithm from scratch!
The context of this task will be a fun little program called *Snowflake Evolution*. The program begins by generating six random snowflakes, as shown in the screen shot on the next page. Then, when the user clicks one of them, it generates several variations of the selected snowflake. This process can be repeated as ...
![Four screen shots from the *Snowflake Evolution* app.](Snowflakes.png)
But how do you write code to generate such complex shapes? And not only that, but do it in a way that lets us generate variations of a particular shape? That's not something for which you can easily find an off-the-shelf algorithm. So let's roll up our sleeves and figure it out!
## First Observations
Looking at the snowflakes in the figure, the first thing we should notice is the sixfold symmetry. Thinking in the step-by-step terms of algorithms, we should think of drawing the branches one at a time. But all six branches are the same, so we can just use a loop. In pseudocode, that would look something like this.
```pseudo
repeat 6 times
draw a branch
rotate 60°
```
This assumes that the *draw a branch* step does the same thing every time we do it, except pointed in a different direction.
Of the many ways Mini Micro has to put graphics on the screen, this assumption — that we can draw the same thing, but rotated differently — leads us directly to turtle graphics, first introduced in Chapter 18. To draw the same branch but rotated some number of degrees with only `gfx.line` and friends would take a *lot...
So we're already making progress on two levels: we have a very rough sketch of the algorithm, and we have made an important decision about the implementation.
## Anatomy of a Snowflake Branch
We still don't know how quite to do that *draw a branch* step. So let's look more closely. Consider the following snowflake.
{width: "50%"}
![](FlakeFull.png)
We've already dealt with the fact that there are six identical branches, so let's focus now on just one branch.
{width: "25%"}
![](FlakeBranch1.png)
{gap:30}
How can we simplify this? The first thing to notice is that there is some self-similarity going on. Each of the two side branches coming off the main trunk, when viewed in isolation, look a lot like the whole branch. And in fact if you were to break the main branch right where those two side branches come off, the s...
{width: "25%"}
![](FlakeBranch2.png)
And then if you were to focus on any one of those branches, you could break it down into smaller bits the exact same way: a trunk, two smaller side branches, and one smaller forward branch.
{width: "25%"}
![](FlakeBranch3.png)
## Our Custom Branch Algorithm
So we've uncovered a recursive structure here: a branch is composed of a straight line, followed by three smaller branches. In pseudocode, we might represent this as something like:
```pseudo
draw a branch = function(scale)
if scale is too small, return
draw a straight line proportional to scale
turn to the left
draw a branch (scale * 0.5)
turn to the right
draw a branch (scale * 0.5)
turn forward
draw a branch (scale * 0.6)
```
And now we have a detailed enough algorithm to convert this into MiniScript.
{i:"`/sys/lib`,`turtle`"}
{caption: "First stab at a program for drawing one branch of a snowflake."}
```miniscript
import "turtle"
clear
drawBranch = function(turtle, scale = 1)
if scale < 0.02 then return
// create a new turtle, so we don't move the one passed in
t = new turtle
// draw main branch
t.penSize = 20 * scale