Thursday, 10 January 2013

PYGAME- “Sierpinski Triangle”

           
             The Sierpinski triangle fractal was first introduced in  1915 by Waclaw Sierpinski , who described some of the triangle’s interesting properties .  The Sierpinski triangle is a geometric pattern formed by connecting the  midpoints of the sides of a triangle. It is at the s most  interesting and  simplest fractal shapes in existence. Because one of  the neatest things about Sierpinski's triangle is how many different and easy  ways there are to generate it . This is one of the basic examples of self-similar  sets, that is  it is a mathematically generated pattern that can be reproducible at any  magnification or reduction.

Construction


  1)  Start with any triangle in a plane. The canonical Sierpinski triangle uses a
       equilateral triangle with a base parallel to the horizontal axis

  2)  Connect the midpoints of each side to form four separate triangles, and  cut
        out the triangle in the center

  3)  Each of the three  remaining triangles, perform this same act








Formation after each iteration
-->

 

Mathematical aspects



           The area of the Sierpinski Triangle approaches 0. This is because with every iteration 1/4 of the area is taken away. After an infinite number of iterations the remaining area is 0.

The number of triangles in the Sierpinski triangle can be calculated with the formula: N= 3k
Where n is the number of triangles and k is the number of iterations.

Dimensions of the triangles that remain after the Nth iteration are exactly 1/2^N of the original dimensions.

Triangle's side length=L*(1/2k)

where L is original and k is the number of iterations. 

Sierpinski Triangle

PROGRAM



#importing pygame
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()

# find midpoint of a line
def midline(x,y):
   p=((x[0]+y[0])*.5)
   q=((x[1]+y[1])*.5)
   n=(p,q)
   return n

# main loop
while True:
   n=0
   l1=[]
   l3=[]
   screen.fill(White)

# 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,(450,450),( 300,p),1)
   l=[(150,450),(450,450),(300,p)]
   pygame.display.update()
   l1.append((l))

# sub loop
   while n<7:
     l2=l1
     l1=l3
     for i in range(len(l2)):
       
#cut out the triangle in the center
         a=midline(l2[i][0],l2[i][1])
         b=midline(l2[i][0],l2[i][2])
         c=midline(l2[i][1],l2[i][2])
         pygame.draw.polygon(screen,Blue,(a,b,c))

#storing three  remaining triangles    
         l3=[l2[i][0],a,b]
         l1.append((l3))
       
         l3=[a,l2[i][1],c]
         l1.append((l3))
       
         l3=[b,c,l2[i][2]]
         l1.append((l3))
       
         l3=[]     

     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