-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sierpinski.py
57 lines (46 loc) · 1.68 KB
/
Sierpinski.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pygame
import sys
import time
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
width, height = 800, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Fractal Generation - Sierpinski Triangle')
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
# Initial points of the triangle
points = [(400, 50), (50, 750), (750, 750)]
# Function to draw the Sierpinski Triangle
def draw_sierpinski(points, depth, max_depth):
if depth == max_depth:
pygame.draw.polygon(screen, white, points, 1)
pygame.display.flip()
return
# Calculate midpoints of the triangle
midpoints = [
((points[0][0] + points[1][0]) // 2, (points[0][1] + points[1][1]) // 2),
((points[1][0] + points[2][0]) // 2, (points[1][1] + points[2][1]) // 2),
((points[2][0] + points[0][0]) // 2, (points[2][1] + points[0][1]) // 2)
]
# Recursive calls for the three smaller triangles
draw_sierpinski([points[0], midpoints[0], midpoints[2]], depth + 1, max_depth)
draw_sierpinski([points[1], midpoints[0], midpoints[1]], depth + 1, max_depth)
draw_sierpinski([points[2], midpoints[1], midpoints[2]], depth + 1, max_depth)
# Slow down the drawing to make it peaceful to watch
time.sleep(0.5)
# Main function
def main():
screen.fill(black)
max_depth = 5 # You can change this to make the fractal more or less detailed
draw_sierpinski(points, 0, max_depth)
# Keep the screen open to admire the fractal
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()