The Sierpinski triangle is a fractal described by Sierpiński in 1915 and appearing in Italian art from the 13th century. It is also called the Sierpiński gasket. The Sierpinski triangle is a geometric pattern formed by connecting the midpoints of the sides of a triangle. It is at the is most interesting one and simplest one in fractals.
The Sierpinski triangle is given by Pascal's triangle (mod 2), giving the sequence 1; 1, 1; 1, 0, 1; 1, 1, 1, 1; 1, 0, 0, 0, 1; ... . In other words, coloring all odd numbers black and even numbers white in Pascal's triangle produces a Sierpiński triangle
Construction
An algorithm for obtaining arbitrarily close approximations to the Sierpinski triangle is as follows:
- Start with any triangle in a plane. The canonical Sierpinski triangle uses an equilateral triangle with a base parallel to the horizontal axis (image 1).
- Shrink the triangle to ½ height and ½ width, make three copies, and position the three shrunken triangles so that each triangle touches the two other triangles at a corner (image 2). Note the emergence of the central hole - because the three shrunken triangles can between them cover only 3/4 of the area of the original.
- Repeat step 2 with each of the smaller triangles (image 3 and so on).
Note that this infinite process is not dependent upon the starting shape being a triangle—it is just clearer that way.
Steps in coding
First off, you need to start off your game and load up your modules.
import pygame, sys
from pygame.locals import *
from pygame.locals import *
In the Sierpinski triangle, the function is defined for calculating the midpoint of the line.
Next, initialize clock and draw the screen
def midpoint(p1, p2):
return((p1[0]+p2[0])/2, (p1[1]+p2[1])/2)
return((p1[0]+p2[0])/2, (p1[1]+p2[1])/2)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Sierpinki triangle')
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Sierpinki triangle')
def main():
while 1:
while 1:
In while loop, fill the screen with black color and draw the initial equilateral triangle.store the points in array
screen.fill((0,0,0))
pygame.draw.polygon(screen, (255,0,0),((100,450),(500,450), (300,104)))
points = [[(100,450),(500,450),(300,104)]]
pygame.display.update()
clock.tick(1)
pygame.draw.polygon(screen, (255,0,0),((100,450),(500,450), (300,104)))
points = [[(100,450),(500,450),(300,104)]]
pygame.display.update()
clock.tick(1)
Then define the for loop from range 0 to 7 and check the event in loop, consider point in points and calculate the midpoint with the points of the triangle. Next, draw the line with mid points and store the points in list 'l'.Each list 'l' is stored in 'array'.this array points stored to previously created points.
for k in range(1,8,1):
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
array = []
for point in points:
l = []
for i in range(0,3,1):
first = midpoint(point[i-1],point[i])
second = midpoint(point[i],point[(i+1)%3])
pygame.draw.line(screen,(255,255,0),first,second,1)
l += [[first,point[i],second]]
array += l
points = array
pygame.display.update()
clock.tick(1)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
array = []
for point in points:
l = []
for i in range(0,3,1):
first = midpoint(point[i-1],point[i])
second = midpoint(point[i],point[(i+1)%3])
pygame.draw.line(screen,(255,255,0),first,second,1)
l += [[first,point[i],second]]
array += l
points = array
pygame.display.update()
clock.tick(1)
Complete code is Here.
No comments:
Post a Comment