Triangle Fractal

This shape is called a "Sierpinski (sher-PIN-ski) gasket."


triangle fractal animation levels 1 to 8

    "U7L2-TriangleFractal"Create a new project called U7L2-TriangleFractal
  1. First, write a block that takes a size as input and draws an equilateral triangle.
    triangle size: (40) triangle size: (40) drawing

Growing the Triangle into the Fractal

triangle is the base case. You can modify it, or create a new block with level and size inputs just like tree:
triangle fractal level: (level #) size: (size #)

If the level is 1, we should see only the triangle:
Triangle fractal level 1 drawing: basic triangle

Paul, do you not like my click-for-comment on principle? --bh

Otherwise, a half-size child buds at the end of each side of the parent. At level 2, it should look like this:
Triangle fractal level 2 drawing: triangle with 3 smaller triangles on its vertices

And at level 3, you should see this:
Triangle fractal level 3 drawing: triangle with 3 smaller triangles on its vertices each of which in turn have 3 smaller triangles on their vertices

  1. Talk with Your Partner Write the code for triangle fractal.
  2. Using a warp block (to save time) try a big level number, like 8 or 9.
  3. The warp block (in the yellow Control palette) makes the scripts inside it run faster by not letting any other scripts run or updating the display until it's finished.
    warp{triangle fractal level:(9) size:(100)}

In order to draw the parent triangle, the sprite must turn 120° between sides. For the fractal you just created, that turning happens after drawing the child, but you could turn before the recursive call, or split the turn, with part before and part after the recursive call.

  1. Try some modifications like these:
    repeat(3){move(size) steps; turn clockwise (120) degrees; triangle fractal level:(level-1) size:(size*0.5)} repeat(3){move(size) steps; turn clockwise (120) degrees; triangle fractal level:(level-1) size:(size*0.5); turn clockwise(60) degrees} repeat(3){move(size) steps; turn counter-clockwise (30) degrees; triangle fractal level:(level-1) size:(size*0.5); turn clockwise(150) degrees}
 
  1. Use for to make an animation that cycles through different turning angle arrangements (that is, 0° and 120°, then 1° and 119°, then 2° and 118°, etc.).
  2. Experiment with the scale factor for the size of the recursive calls. A couple of interesting values are \frac{1}{3} and \frac{1}{\sqrt{3}}.