Revision as of 15:26, 8 September 2013 by ErnieParke (talk | contribs) (Saving work.)

The droste effect is an example of Recursion.

Recursion is the process of repeating items in a self-similar way. Recursion can be implemented in Scratch by making a block that uses itself. This can be used to create fractals. A fractal is pattern that produces a picture, which can be zoomed into infinity and will still produce the same picture. Some common examples of fractals are the Mandelbrot set, the Sierpinski Triangle, and the Koch Snowflake.

Creating the Koch Curve

The Koch Curve is a fractal that can be created relatively easily in Scratch. The Koch Curve is part of a larger fractal, the Koch Snowflake.

Understanding Recursion in the Koch Curve

The Koch Curve.

The Koch Curve is made of four Koch Curves that are a third of the size of the original Koch Curve. They are they are arranged so that the first and fourth are flat and the middle two point up to make an equilateral that is triangle missing one side.

Recersion in Koch Curve.png

To make it easier to draw, the Koch Curve can be broken down into iterations, each one more complicated than the last. The first iteration is made up of four straight lines. The second iteration contains four copies of the first iteration. The third iteration contains four copies of the second iteration or sixteen copies of the first iteration. As iterations are added it gets more complicated and looks more and more like the real Koch Curve.

Iterations of Koch Curve.png

Implementation in Scratch

Basic Pen Path without Recursion

The triangle in the center is an equilateral triangle, therefore each of it's angles have a measure of 60°.

Equilateral Triangle in Koch Curve.png

Using basic geometry the angles of the rotations the sprite must make can be found.

Sprite Turns in Koch Curve.png

Using this these angles, a script can be created that draws the first iteration of Koch Curve. Since each line segment is 1/3 the total length of the Koch Curve, the sprite should move 1/3 of the length given each time.

when gf clicked
point in direction (90) //make sure the sprite is pointed right
go to x: (-240) y: (-179) //put the sprite in the lower left corner
clear //clear graphics from previous runs
pen down //put the pen down for drawing
make the first iteration of the Koch Curve with a length of (480)
pen up //put the pen up so movement afterwards is not recorded

define make the first iteration of the Koch Curve with a length of (length)
move ((length) / (3)) steps //draw a line segment
turn ccw (60) degrees //first turn
move ((length) / (3)) steps //draw a line segment
turn cw (120) degrees //second turn
move ((length) / (3)) steps //draw a line segment
turn ccw (60) degrees //third turn
move ((length) / (3)) steps //draw a line segment

Adding Recursion

To add recursion, instead of drawing a line, a smaller Koch Curve can be drawn. When each iteration above one is drawn, it contains four smaller Koch Curves that are one iteration less than it self. For example, when drawing the second iteration you must draw four copies that are 1/3 the size of the first iteration. When the program gets to the first iteration it must draw straight lines. The following code will make the fifth iteration of the Koch Curve

when gf clicked
point in direction (90) //make sure the sprite is pointed right
go to x: (-240) y: (-179) //put the sprite in the lower left corner
clear //clear graphics from previous runs
pen down //put the pen down for drawing
make the (5) iteration of the Koch Curve with a length of (480)
pen up //put the pen up so movement afterwards is not recorded

define make the (iteration) iteration of the Koch Curve with a length of (length)
if <(iteration) = [1]> then //is it the first iteration?
  move ((length) / (3)) steps //draw a line segment
else
  make the ((iteration) - (1)) iteration of the Koch Curve with a length of ((length) / (3)) //make a smaller Koch  Curve
end
turn ccw (60) degrees //first turn
if <(iteration) = [1]> then //is it the first iteration?
  move ((length) / (3)) steps //draw a line segment
else
  make the ((iteration) - (1)) iteration of the Koch Curve with a length of ((length) / (3)) //make a smaller Koch  Curve
end
turn cw (120) degrees //second turn
if <(iteration) = [1]> then //is it the first iteration?
  move ((length) / (3)) steps //draw a line segment
else
  make the ((iteration) - (1)) iteration of the Koch Curve with a length of ((length) / (3)) //make a smaller Koch  Curve
end
turn ccw (60) degrees //third turn
if <(iteration) = [1]> then //is it the first iteration?
  move ((length) / (3)) steps //draw a line segment
else
  make the ((iteration) - (1)) iteration of the Koch Curve with a length of ((length) / (3)) //make a smaller Koch  Curve
end

Creating the Mandelbrot Set

An animation of zooming in on the Mandelbrot set.

The Mandelbrot set is a mathematical fractal defined in the complex plane. It was named after its discoverer, Benoit Mandelbrot, and has many close relationships to the Julia Sets.

Understanding the Definition

The Mandelbrot set is defined as all c values in the complex plain which are bounded under iteration in the following equation:

Mandelbrot Equation.png

For example, letting c=1 yields the sequence 0, 1, 2, 5, 26, 677, ect... That sequence escapes to infinity and therefore c=1 is not part of the Mandelbrot set. Meanwhile, the sequence c=-1 gives 0, -1, 0, -1, 0, ect..., is bounded and so belongs to the Mandelbrot set.

To note, it has been proven that if any sequence contains a complex value that is outside a distance of 2 from the origin, it will escape to infinity.

Coloring

An animation of the Mandelbrot set under iteration.

In a basic Mandelbrot set, white is used for a c-values that escapes to infinity and black is used for all c-values that do not.

In most Mandelbrot sets, though, colors are used to help depict the Mandelbrot set or make it more art-orientated. Colors are not defined through an equation, but rather through the last iteration before escaping a distance of two from the origin. The iteration is then assigned a color of the creator's preference.

Implementation in Scratch

Creating the Variables

Due to the fact that Scratch does not directly support mathematics in the complex plain, a simple workaround has to be used. Each complex number will be defined as two variables, the real part, and the complex part. And since there will be operations based on the complex numbers, two complex numbers will be needed, or four variables. For the tutorial, these names will be used:

  • Real 1
  • Real 2
  • Complex 1
  • Complex 2

Along with that, a variable Best Fit will be used to figure out the color to be used when coloring a complex value:

  • Best Fit

Coding

See Also