-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathfractal_code.py
95 lines (79 loc) · 3.27 KB
/
fractal_code.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pygame
import math
# Constants
SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1080
MAX_DEPTH = 6 # Maximum iterations for the L-system
MIN_DEPTH = 1 # Minimum iterations for the L-system
TREE_SPACING = 100 # Spacing between trees
TREE_COUNT = 1 # Number of trees
ANGLE = math.pi / 2 # 90 degrees
# Function to calculate the fractal dimension D2
def calculate_fractal_dimension(gamma):
return 2.0 - gamma
# Function to generate the L-system string
def generate_l_system(axiom, rules, iterations):
current_string = axiom
for _ in range(iterations):
next_string = ""
for char in current_string:
next_string += rules.get(char, char)
current_string = next_string
return current_string
# Function to draw the fractal based on the L-system string
def draw_fractal_l_system(surface, l_system_string, start_x, start_y, length, angle):
stack = []
x, y = start_x, start_y
for command in l_system_string:
if command == 'F':
# Calculate new branch endpoint
x_new = x + length * math.cos(angle)
y_new = y + length * math.sin(angle)
# Draw the branch
pygame.draw.line(surface, (255, 255, 255), (x, y), (x_new, y_new), 1)
x, y = x_new, y_new
elif command == '+':
angle += ANGLE # Turn right
elif command == '-':
angle -= ANGLE # Turn left
elif command == '[':
stack.append((x, y, angle)) # Save current position and angle
elif command == ']':
x, y, angle = stack.pop() # Restore last position and angle
def main():
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Fractal Trees with Pygame (L-System)")
clock = pygame.time.Clock()
axiom = "F+F+F+F"
rules = {'F': "F+F-F+F+F"} # Production rule
iterations = 4 # Initial number of iterations for L-system
length = 7.0 # Length of each branch
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Adjust depth with key events
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_k and iterations < MAX_DEPTH:
iterations += 1 # Increase iterations
if event.key == pygame.K_j and iterations > MIN_DEPTH:
iterations -= 1 # Decrease iterations
# Generate the L-system string based on the current number of iterations
l_system_string = generate_l_system(axiom, rules, iterations) # Generate L-system string
screen.fill((0, 0, 0)) # Set background color to black
# Draw the L-system fractal tree
start_x = SCREEN_WIDTH / 2
start_y = SCREEN_HEIGHT - 50 # Y position for tree base
draw_fractal_l_system(screen, l_system_string, start_x, start_y, length, -math.pi / 2)
# Display the current depth on screen
font = pygame.font.Font(None, 36)
text = font.render(f"Iterations (Press K/J to change): {iterations}", True, (255, 255, 255))
screen.blit(text, (10, 10))
pygame.display.flip()
clock.tick(120)
pygame.quit()
if __name__ == "__main__":
main()