|
| 1 | +import pygame |
| 2 | +import numpy as np |
| 3 | +import sys |
| 4 | +import pmma |
| 5 | + |
| 6 | +pmma.init() |
| 7 | + |
| 8 | +# Initialize Pygame |
| 9 | +pygame.init() |
| 10 | + |
| 11 | +# Screen dimensions |
| 12 | +WIDTH, HEIGHT = 1920, 1080 |
| 13 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 14 | +pygame.display.set_caption("Randomized Gradient Snowflake") |
| 15 | + |
| 16 | +# Colors |
| 17 | +BLACK = (0, 0, 0) |
| 18 | + |
| 19 | +# Clock for controlling frame rate |
| 20 | +clock = pygame.time.Clock() |
| 21 | + |
| 22 | +color_one = pmma.ColorConverter() |
| 23 | +color_two = pmma.ColorConverter() |
| 24 | +ang = pmma.Perlin() |
| 25 | + |
| 26 | +def random_angle(): |
| 27 | + """Generate a small random angle variation.""" |
| 28 | + return ang.generate_1D_perlin_noise(new_range=[-np.pi / 6, np.pi / 6]) |
| 29 | + |
| 30 | +def interpolate_color(start_color, end_color, t): |
| 31 | + """Interpolate between two colors based on t (0 <= t <= 1).""" |
| 32 | + return [ |
| 33 | + int(start_color[i] + (end_color[i] - start_color[i]) * t) for i in range(3) |
| 34 | + ] |
| 35 | + |
| 36 | + |
| 37 | +def draw_gradient_line(surface, start_pos, end_pos, start_color, end_color): |
| 38 | + """Draw a line with a gradient between two colors.""" |
| 39 | + steps = int(np.linalg.norm(np.array(end_pos) - np.array(start_pos))) |
| 40 | + for i in range(steps): |
| 41 | + t = i / steps |
| 42 | + color = interpolate_color(start_color, end_color, t) |
| 43 | + pos = ( |
| 44 | + start_pos[0] + (end_pos[0] - start_pos[0]) * t, |
| 45 | + start_pos[1] + (end_pos[1] - start_pos[1]) * t, |
| 46 | + ) |
| 47 | + pygame.draw.circle(surface, color, (int(pos[0]), int(pos[1])), 1) |
| 48 | + |
| 49 | + |
| 50 | +def randomized_koch_snowflake(order, p1, p2, start_color, end_color): |
| 51 | + """Draw a Koch snowflake edge with randomization and gradient colors.""" |
| 52 | + if order == 0: |
| 53 | + draw_gradient_line(screen, p1, p2, start_color, end_color) |
| 54 | + return |
| 55 | + |
| 56 | + # Divide the line into thirds |
| 57 | + p3 = p1 + (p2 - p1) / 3 |
| 58 | + p5 = p1 + 2 * (p2 - p1) / 3 |
| 59 | + |
| 60 | + # Compute the peak of the triangle with randomization |
| 61 | + angle = np.pi / 3 + random_angle() |
| 62 | + direction = p5 - p3 |
| 63 | + p4 = p3 + np.array([ |
| 64 | + direction[0] * np.cos(angle) - direction[1] * np.sin(angle), |
| 65 | + direction[0] * np.sin(angle) + direction[1] * np.cos(angle) |
| 66 | + ]) |
| 67 | + |
| 68 | + # Split colors for the gradient |
| 69 | + color1 = interpolate_color(start_color, end_color, 1/3) |
| 70 | + color2 = interpolate_color(start_color, end_color, 2/3) |
| 71 | + |
| 72 | + # Recursively draw the segments |
| 73 | + randomized_koch_snowflake(order - 1, p1, p3, start_color, color1) |
| 74 | + randomized_koch_snowflake(order - 1, p3, p4, color1, color2) |
| 75 | + randomized_koch_snowflake(order - 1, p4, p5, color2, end_color) |
| 76 | + randomized_koch_snowflake(order - 1, p5, p2, end_color, end_color) |
| 77 | + |
| 78 | + |
| 79 | +def draw_random_snowflake(order, center, size): |
| 80 | + """Draw a randomized Koch snowflake with gradient colors.""" |
| 81 | + # Initial equilateral triangle |
| 82 | + angle = np.pi / 3 |
| 83 | + p1 = center + np.array([-size / 2, size * np.sqrt(3) / 6]) |
| 84 | + p2 = center + np.array([size / 2, size * np.sqrt(3) / 6]) |
| 85 | + p3 = center + np.array([0, -size * np.sqrt(3) / 3]) |
| 86 | + |
| 87 | + # Generate random gradient colors |
| 88 | + start_color = color_one.generate_color_from_perlin_noise(format=pmma.Constants.RGB) |
| 89 | + end_color = color_two.generate_color_from_perlin_noise(format=pmma.Constants.RGB) |
| 90 | + |
| 91 | + # Draw the edges with gradients |
| 92 | + randomized_koch_snowflake(order, p1, p2, start_color, end_color) |
| 93 | + randomized_koch_snowflake(order, p2, p3, start_color, end_color) |
| 94 | + randomized_koch_snowflake(order, p3, p1, start_color, end_color) |
| 95 | + |
| 96 | + |
| 97 | +# Main loop |
| 98 | +def main(): |
| 99 | + order = 4 # Fixed recursion depth for performance |
| 100 | + size = 850 |
| 101 | + center = np.array([WIDTH // 2, HEIGHT // 2]) |
| 102 | + while True: |
| 103 | + # Event handling |
| 104 | + for event in pygame.event.get(): |
| 105 | + if event.type == pygame.QUIT: |
| 106 | + pygame.quit() |
| 107 | + sys.exit() |
| 108 | + |
| 109 | + # Clear the screen |
| 110 | + screen.fill(BLACK) |
| 111 | + |
| 112 | + # Draw the randomized snowflake with gradient |
| 113 | + draw_random_snowflake(order, center, size) |
| 114 | + |
| 115 | + pmma.compute() |
| 116 | + |
| 117 | + # Update the display |
| 118 | + pygame.display.flip() |
| 119 | + |
| 120 | + # Control frame rate |
| 121 | + clock.tick(60) # Run at 60 FPS |
| 122 | + |
| 123 | + |
| 124 | +# Run the program |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments