Fractals are one example of
"algorithmic art"
. Here discussing about a particular kind
of fractal known as the Koch
Snowflake.
A fractal is an image that
keeps on repeating itself every new
time it gets smaller and
smaller.
The snowflake is
a fractal of the Koch curve.
Helge von
Koch who is responsible for the snowflake. He wrote about the fractal snowflake in a
paper written in 1906.
The most interesting part about a Koch
snowflake is that when you continue this creating
process the curve
is infinitely long but has an finite area. This means the the sides
of the
snowflake are never ending but at the same time the area it
encloses is only going to be 1.6
times bigger then the original area
of the original triangle.
Design Steps
step
by step process.
Step1
Start with an equilateral triangle.
Step2
Split each side into three equal
segments.
step3
Create an smaller equilateral triangle
in the middle of the three parts from step 2
step4
Remove the line segment that is the
base of the triangle from step 3
If you continue
repeating this
procedure, the curve will never self-intersect,
and in the limit you
get a shape known as the Koch snowflake.
Each time you repeat the
process
this is call an iteration. To find the number of sides all
you need to know is how many
iterations you have completed.
The Math Behind It
Number of sides (n)
For each iteration, one side of the
figure from the previous stage
becomes four sides in the following
stage. Since we begin with
three sides, the formula for the number
of sides in the Koch
Snowflake is
n = 3*4a
in the ath
iteration.
For iterations 0, 1, 2 and 3, the
number of sides are 3, 12, 48 and
192, respectively.
Length of side (length)
In every iteration, the length of a
side is 1/3 the length of a side from the preceding stage. If we begin
with an equilateral triangle with side length x, then the length of
a side in iteration a is
length = x*3-a
For iterations 0 to 3, length = a, a/3,
a/9 and a/27.
Line equations
Some basic line equations are used in the construction of a snowflake, that are
1) Midpoint of a line is :- consider a line with end points (x1,y1) and (x2,y2).
it's mid point is (X,Y)
X = (x1+x2)/2
Y = (y1+y2)/2
2) Distance between two points :- Consider two points (x1,y1) and (x2,y2)
distance = ( ( x1 − x 2 )2 + ( y1 −
y 2 )2 )1/2
3) Find point on a line :- consider a line having points (x1,y1) and (x2,y2).
Distance between these two point is D. To find an another point (X,Y) on the
line, L distance away from the point (x1,y1) is
X = x1 + k (x2 - x1)
Y = y1 + k (y2 - y1) where k = L/D
PROGRAM
import pygame,sys
from pygame.locals import *
#initialise screeen
pygame.init()
screen= pygame.display.set_mode((600, 600))
pygame.display.set_caption('koch snowflake')
White=(255,255,255)
Black=(0,0,0)
Blue= (0,0,255)
screen.fill(White)
clock=pygame.time.Clock()
#divide line in to three and remove the middle section
def cutline(x,y,n):
a=((x[0]+((y[0]-x[0])*.33333333333333333)))
b=((x[1]+((y[1]-x[1])*.33333333333333333)))
c=((x[0]+((y[0]-x[0])*.66666666666666666)))
d=((x[1]+((y[1]-x[1])*.66666666666666666)))
if n<=2:
pygame.draw.line(screen,White,( a,b),(c,d),4)
else:
pygame.draw.line(screen,White,( a,b),(c,d),1)
l=[(a,b),(c,d)]
return l
#
#find midpoint of a line and draw two sides of triangle(like this __/\__ )
def point(a,b,c):
p=((a[0]+b[0])*.5)
q=((a[1]+b[1])*.5)
x=((c[0]+((p-c[0])*1.3333333333333333333)))
y=((c[1]+((q-c[1])*1.3333333333333333333)))
n=(x,y)
pygame.draw.line(screen,Black,( b),(x,y),1)
pygame.draw.line(screen,Black,( a),(x,y),1)
return n
# divide a line and find a point 1/3 distance away from one point
def next(p,q):
a=((p[0]+((q[0]-p[0])*.33333333333333333)))
b=((p[1]+((q[1]-p[1])*.33333333333333333)))
l=(a,b)
return l
# main loop
while True:
n=0
l=[]
l1=[]
screen.fill(White)
pygame.display.update()
# draw a equilateral triangle
pygame.draw.line(screen,Black,( 150,450),(450, 450),1)
topy=((((300**2) - (150**2))**.5))
p=(450-topy)
pygame.draw.line(screen,Black,( 150,450),(300, p),1)
pygame.draw.line(screen,Black,( 300,p),(450, 450),1)
l1=[(150,450),(450,450),(300,p)]
pygame.display.update()
l.append((l1))
l1=[(150,450),(300,p),(450,450)]
l.append((l1))
l1=[(300,p),(450,450),(150,450)]
l.append((l1))
# sub loop
while n<=6:
l2=l
l=[]
# iteration process
for i in range(len(l2)):
a,b=cutline(l2[i][0],l2[i][1],n)
c=point(a,b,l2[i][2])
x=next(l2[i][0],l2[i][2])
y=next(l2[i][1],l2[i][2])
l1=[a,c,b]
l.append((l1))
l1=[b,c,a]
l.append((l1))
l1=[l2[i][0],a,x]
l.append((l1))
l1=[l2[i][1],b,y]
l.append((l1))
n+=1
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
clock.tick(1)
pygame.display.update()
No comments:
Post a Comment