University of California, Berkeley
EECS Department - Computer Science Division
CS3 Lecture 22 : Fractals
Overview of today's lecture
Project Questions
- There is now a new home for all Project Questions - the Online Project FAQ! (organized
by project)
Review
Input / Output
- We saw strings (a new data type), display and write
(to output to screen) and read (to read input).
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
- A coastline
- Fern leaf
- Snowflake
- The explosion of a pyramid scheme
- We look at fractal definitions just like we look at recursive
function definitions:
Basic Graphics Primitives
- MacGambit
- Initializes (through any drawing command) a window which
goes from -200 to 200 in both the x and y directions.
- 0,0 is the center of the screen
- The window is scaled by a factor of 1/2, but you can undo
that by clicking the zoom button.
- You can use the basic commands (in online help) or more sophisticated
commands by calling Macintosh toolbox routines.
- Dr. Scheme
- Allows you to open a window of any size, as long as there's
enough memory.
- 0,0 is the upper-left corner of the screen
- Johnathon Jamison wrote some code which allows Dr. Scheme
to feel just like MacGambit, we'll have that online.
- The basic primitives built into MacGambit:
- (clear-graphics)
- (position-pen x y)
- Move the pen to position x,y
- (draw-line-to x y)
- Draw a line to position x,y
Your First Fractal : Sierpinski
square
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!
- Start with a straight line.
- 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
- Start with an equilateral triangle.
- 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.
Summary
- We saw six examples of fractals; what a wonderful, beautiful
world is mathematics!
Next Time
- We look at graphics with some randomness, Fibonacci, Tower
of Hanoi, and maybe some game theory!
Puzzle : Tower of Hanoi
- Transfer the tower of disks from peg A to peg C in the fewest
moves possible
- You are only allowed to move one disk at a time
- You may never place a disk on top of a smaller one
Game : Brussels Sprouts
- This is a game invented by John Conway and Michael Paterson
and played with a pencil and paper.
- You start wtih a number of 4-arm crosses.
- A move is to connect two arms (of the same cross or of two
different ones) and to make a new cross by drawing a crossbar
somewhere along the line, as below: