# CS3 Lecture 13 : Fractals

## Project Questions

• Are there any questions about the project?

## Review - Advanced Lists

• Association lists
• Writing procedures with an arbitrary number of arguments
• Deep recursion

## Fractals, or Self-similar geometric patterns

### Overview

• Fractals, like recursive procedures are defined in terms of themselves.
• Any part is similar to the whole
• The phrase "Fractals" was coined by Benoit Mandelbrot
• They describe naturally-occurring phenomena, e.g.,
• The path of a meandering river, coastline, fern leaf, snowflake, the explosion of a pyramid scheme
• We look at fractal definitions just like we look at recursive function definitions:
• Base case
• Recursive case

### Basic Graphics Primitives using gdraw.scm

• STk
• You need to call (init-graphics) to initialize the graphics window canvas. gamesman.scm does this for you so you need not worry about it when using graphics with your project.
• The window is initially 6 inches x 6 inches, but you should probably change the window size by using (set-canvas-size 800 800)
• This initializes the window which goes from 0,0 in the upper-left to 800,800 in the lower-right. (I don't like this, so see my adjustments below)
• You can use any of the commands listed in gdraw.scm on your canvas.
• The basic primitives built into STk by gdraw.scm:
• (init-graphics)
• Starts the drawing process by creating a window (gamesman.scm does this for you in your project)
• (clear-graphics!)
• Clear the screen
• (set-canvas-size 800 800)
• Changes the canvas size to 800 x 800
• (draw-rectangle x1 y1 x2 y2 ... xn yn stk-options)
• Move the pen to position x,y
• (draw-line x1 y1 x2 y2 ... xn yn stk-options)
• Draw a line to position x,y

### Basic Graphics Primitives using gdraw-helpers.scm

• What is this?
• Some pre-defined constants and supplemental utilities that makes writing fractals fun
• I provide y-flip to flip the y-coordinate for you (so origin is in SW, not NW)
• The most important new datatypes:
• (make-P x y), (x-P P), (y-P P)
• A point datatype; very handy. We call it P instead of point just because it's used so often.
• If you use draw-line-P, it'll flip the y-coordinate for you.
• (make-V x y), (x-V V), (y-V V)
• A vector datatype; used by coord-P below.
• (lerp x0 x1 t)
• Linear interpolation; return the point x0 (1-t) + x1
• (coord-P P0 P1 v)
• Return the point which is the result of thinking of v in the new coordinate system defined by P0 as the origin and P1 as the point (0,1)
• E.g., (avg-P P0 P1) == (coord P0 P1 (make-V 0 .5))
• (for-each-2 f L)
• Calls binary function f successively on all the pairs of L, moving ahead by 1 pair (not by two)

### Your First Fractal : Sierpinski square

• We have two cases
Base Case, just draw a square Recursive case,  place five sierpinski squares on the diagonals of a 3x3 board
• Let's look at the code which does this.

### Your Second Fractal : Sierpinski triangle

• Similar to the square, but a little more complicated due to geometry
• Start with a triangle and remove the inner inverted triangle.
• Base case: equilateral triangle
• Recursive case: A sierpinski triangle is three smaller (by 1/2) sierpinski triangles placed in the three corners of the original triangle

### Binary Tree : "And they tell two friends, and they tell two friends..."

• Want to see explosive growth of exponentials
• A binary tree is a fork up top and two binary trees on each arm

### Peano Curve : Space-filling!

• As n approaches infinity, completely fills space!
• A peano curve is a path. If the distance from the start to the finish is 3x, the peano curve is drawn as follows
• Start at the start.
• Go 2x of the way toward the destination. (E)
• Make a left turn, go x (N)
• Make a left turn, go x (W)
• Make a left turn, go 2x (S)
• Make a left turn, go x (E)
• Make a left turn, go x (N)
• Make a right turn, go x (E)

### Dragon & C-Curve : Two interesting variants!

• Find a point which makes an isosceles right triangle
• Draw a recursive version between the beginning point and the new point, there are now two options:
• C-curve
• Draw a recursive version between the new point and the end point
• Dragon
• Draw a recursive version between the end point and the new point
• Dragon is space-filling!

### Koch Curves : Snowflakes and Anti-snowflakes

• Divide each side into thirds, delete the middle third, and construct a point off that length from the deleted side that forms a smaller equilateral triangle.
• The anti-snowflake just means we push the point inside rather than outside.

# Animations!!!

 Sierpinski Square Zoom Implode C-Curve Hinge Bulge Dragon Curve Hinge Bulge Zoom

## Summary

• We saw six examples of fractals; what a wonderful, beautiful world is mathematics...

## In lab this week

• You'll continue to work on your project and you'll experiment with some fractals yourself

## In life this week

• Hillary for Secretary of State?
• The Big Game!! (and #9 Women face #5 Rutgers!)