-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDragon_Curve.py
64 lines (51 loc) · 1.83 KB
/
Dragon_Curve.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
58
59
60
61
62
63
64
import pygame
import sys
import time
# Initialize Pygame
pygame.init()
# Screen dimensions
width, height = 800, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Fractal Generation - Colorful Dragon Curve')
# Colors
black = (0, 0, 0)
# Function to interpolate between two colors
def interpolate_color(color1, color2, factor):
return (
int(color1[0] + (color2[0] - color1[0]) * factor),
int(color1[1] + (color2[1] - color1[1]) * factor),
int(color1[2] + (color2[2] - color1[2]) * factor),
)
# Function to draw the Dragon Curve
def draw_dragon_curve(order, p1, p2, color1, color2):
if order == 0:
pygame.draw.line(screen, color1, p1, p2, 1)
pygame.display.flip()
time.sleep(0.001)
else:
# Calculate the midpoint and the new point to form the Dragon Curve
mid = ((p1[0] + p2[0]) // 2, (p1[1] + p2[1]) // 2)
new_point = (mid[0] + (p1[1] - p2[1]) // 2, mid[1] - (p1[0] - p2[0]) // 2)
# Interpolate colors
new_color = interpolate_color(color1, color2, 0.5)
draw_dragon_curve(order - 1, p1, new_point, color1, new_color)
draw_dragon_curve(order - 1, p2, new_point, color2, new_color)
# Main function
def main():
screen.fill(black)
order = 15 # You can change this to make the fractal more or less detailed
# Initial points for the Dragon Curve
p1 = (200, 400)
p2 = (600, 400)
# Initial colors for the gradient
color1 = (255, 0, 0) # Red
color2 = (0, 0, 255) # Blue
draw_dragon_curve(order, p1, p2, color1, color2)
# 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()