Programming Snazzy Fractals
Roshanak Roshandell
Department of Computer Science and So<ware Engineering
Sea$le University
Seattle Expanding Your Horizons
Euclidean Geometry
n Triangles n Circles n Squares n Rectangles n Trapezoids n Pentagons n Hexagons n Octagons n Cylinders
Can We Describe Nature Using Euclidean Geometry?
n Tree n cylinders
n Mountains n triangles
n Clouds n circles
n Leaves n Rocks
Well…
n We can describe man made structures using Euclidean geometry
n But nature is full of rough edges and non uniform shapes
n Geometry of irregular shapes, non-‐smooth edges, infinite details, and infinite length
n Fractal geometry is the formula of nature!
Let’s Watch!
n Fractal Zoom – Mandelbrot n AnimaWon n AnimaWon n Flying in Fractal Mountain
What is a Fractal?
n Geometric figures just like circles and rectangles, but they also have some special properWes
n Structural self-‐similarity n All over nature
n Flowers, trees, mountains, stalagmite, snow flakes, …
n Fractals are objects that look the same regardless of the magnificaWon
Why Should You Care about Fractals?
n Fractal Landscape n Movies (e.g., Star Trek) n Computer Graphics n Computer and video games
n Medicine n Fractal art and music n Where can you find fractals?
n Bacteria cultures n Galaxies n Your own body!
The Koch Snowflake
43
Length =
1Length =
2
43
Length =⎛ ⎞⎜ ⎟⎝ ⎠
First step
After 2 steps
A<er 3 Steps
A<er n Steps
43
n
Length =⎛ ⎞⎜ ⎟⎝ ⎠
Well, The Snowflake
n Koch Snowflake is 6 of these put together
Did You NoWce….
n The perimeter of the Koch snowflake is infinite ... n but that the area it bounds is finite (sure enough, it is contained in the white square).
Do You See a Paaern?
n We have to do something over and over and over again n It’s hard! n It’s tedious!
n Can we get help?
What Can Computers Do?
n Computers are really good at: 1. Following instrucWons 2. Processing data you provide (names, numbers, etc.) 3. Making decisions according to rules you specify 4. RepeaWng, repeaWng, repeaWng and repeaWng
n Let’s look at some examples from Python Programming Language
1) Following InstrucWons
n Load the so<ware (the Python interpreter) that will obey your commands!
n Find the shortcut to Python on your desktops. n You will see the interpreter's prompt: >>>
This prompt tells you that the interpreter is waiWng for another command.
Try the Following
>>> print "hello” >>> print "Emily"
>>> theName = "Emily”
>>> print "hello " + theName
2) Processing Data You Provide
n Let's use the Python interpreter as a calculator to see how it processes numbers.
>>> 3*8 + 4 >>> 2342*8973 + 345/3
CalculaWons….
>>> x = int(raw_input(‘Enter number: ')) >>> 56 >>> y = int(raw_input(‘Enter number: ')) >>> 12 >>> x * y >>> x + y
3) Making Decisions According to Rules You Specify
>>> theWord = raw_input(‘Enter a word:') >>> if (theWord is theName): ... print “Hello Master " + theName ... else: ... print “ Who are you?"
Tab
4) RepeaWng, RepeaWng and RepeaWng
>>> for i in range(1, 100): ... print theName >>> for i in range(1, 10): ... for j in range(1,5): ... print theName, ... print " is great!"
1 Tab 2 Tabs
Advanced Loops
>>> for i in range(1, 10): ... print theName + " is " ... for j in range(1,5): ... print “cool, ", ... print “cool!"
FuncWon
n Do we have to type everything all the Wme? Can we store some instrucWons to be used later on?
n >>> def WordCompare(someWord): ... if (someWord is myName): ... print "Hello, oh great master " +myName
n ... else: n ... print someWord + " is not recognized"
Now Let’s do Some Fractal Programming!
Resources
n Download Python Interpreter: n hap://www.python.org
n For this presentaWon go to: n hap://fac-‐staff.seaaleu.edu/roshanak/fractals
n QuesWons? Send me an email: n [email protected]
THANK You!
Hope you had an awesome Wme! J