Tuesday, November 25, 2008

The Koch Curve

I know its slightly off topic but still, there is some connection to CSC236 -- Paterns. The Koch Curve is sort of a fractal, we can use the equations and change it to make interesting shapes. Here is the python code for a Koch Curve (got it from wikipedia) that will model a snowflake:

import turtle
set="F"
for i in range(5): set=set.replace("F","FLFRFLF")
set=set+"R"+set+"R"+set
turtle.down()
for move in set:
#Get rid of the underscores, replace by a sinlge tab as blogspot doesn't like leading blank spaces.
____if move is "F": turtle.forward(100.0/3**i)
____if move is "L": turtle.left(60)
____if move is "R": turtle.right(120)
input ()

And we get this image:




















Now what if we try and modify the equations?

Lets try:

import turtle
import math
set="F"
c = 2f
for i in range(5): set=set.replace("F","FLFRFLF")
set=set+"R"+set+"R"+set
turtle.down()
for move in set:
____if move is "F": turtle.forward((100/c**i))
____if move is "L": turtle.left(60/c**(c-i*i))
____if move is "R": turtle.right(math.cos(45*(i/i**2))*i**4)
input ()

And we get a more interesting pattern:




















Looks fun, now lets terribly screw it over and see what happens :)

Lets try:
import turtle
import math
set="F"
c = 2
for i in range(5): set=set.replace("F","FLFRFLF")
set=set+"R"+set+"R"+set
turtle.down()
for move in set:
____if move is "F": turtle.forward((100/c**i))
____if move is "L": turtle.left(60/c**(c-i*i))
____if move is "R": turtle.right(math.cos(45*(i/i**2))*i**4)
____set=set+"L"+set+"L"+set
____for move in set:
________if move is "F": turtle.forward((100/c**i))
________if move is "L": turtle.left(60/c**(c-i*i))
________if move is "R": turtle.right(math.cos(45*(i/i**2))*i**4) #set=set+"R"+set+"R"+set
input ()

After a couple of minutes of computation, we get this:



















Now if we let the computation continue for a while (7mins) we get this:



















And finally after 21 minutes, we get this beautiful image:

















God knows what it is but I like it. Now there's something worrying me a bit, its still drawing! I dont know when it will stop drawing but i think i'll let it draw till tomorrow morning and see what the final image is like.

1 comment:

Yashvin said...

ayooooooo!

lol, its not the singer "Ayo" but rather a word in creole(a language) which shows that a person is complaining :P