Tuesday, January 19, 2016

Midpoints and Sierpinski Triangles (programming class)

We started the new year with some exercises that refresh several of the concepts we've been covering:

  1. loops
  2. arrays
  3. coordinates
We haven't done very much with coordinates, so this is probably the part that feels the most "new." By this point, loops should feel reasonably familiar and, in the code today, we aren't doing any complicated or nested loops. The use of arrays is still difficult; these objects are a tricky beast for the kids to fully understand.

Midpoint jumps

Our first challenge is to get the turtle to jump halfway from where it is standing to a target point. For this, we recall the getxy() function and use the xy-coordinates for the screen (the home where the turtle starts is (0, 0) in this system). Here's an example:

Sierpinski Triangle

Using this basic code, we can reorganize things a bit to play the Chaos Game and generate an amazing picture. In each iteration, we randomly select one vertex, then jump halfway to that vertex, leave a dot, then continue:

This version already has a nice little twist with the random colors on each new dot. Nice!


Finish the challenges from the class (including writing explanations of the lines of code we highlighted) and then modify the last program to create your own picture. Here's an example of something I created:

A slightly different version is here: <a href="https://jgplay.pencilcode.net/edit/Math/Fractal3">more Fractals.</a>

No comments:

Post a Comment