Nagyitás egérrel
---------------------
import pygame
import numpy as np
# Ablak méretei
SZERESSE = 800
MAGASSAG = 600
# Maximális iteráció (a fraktál részletessége)
MAX_ITER = 100
def mandelbrot_grid(width, height, x_min, x_max, y_min, y_max, max_iter):
"""
Kiszámolja a Mandelbrot halmazt NumPy segítségével a maximális sebesség érdekében.
"""
x = np.linspace(x_min, x_max, width)
y = np.linspace(y_min, y_max, height)
C = x[np.newaxis, :] + 1j * y[:, np.newaxis]
Z = np.zeros_like(C, dtype=np.complex128)
diverge_time = np.zeros(C.shape, dtype=int)
# Iterációs ciklus
for i in range(max_iter):
mask = np.abs(Z) <= 2.0
Z[mask] = Z[mask]**2 + C[mask]
diverge_time[mask] = i
return diverge_time
def get_color(iteration, max_iter):
"""
Színskála generálása iterációs számok alapján.
"""
if iteration == max_iter:
return (0, 0, 0) # Belül fekete
# Szivárvány hatás a hullámhosszak alapján
norm = iteration / max_iter
r = int(9 * (1 - norm) * norm * norm * norm * 255)
g = int(15 * (1 - norm) * (1 - norm) * norm * norm * 255)
b = int(8.5 * (1 - norm) * (1 - norm) * (1 - norm) * norm * 255)
return (min(r, 255), min(g, 255), min(b, 255))
def main():
pygame.init()
screen = pygame.display.set_mode((SZERESSE, MAGASSAG))
pygame.display.set_caption("Mandelbrot Halmaz - Interaktív Fraktál")
# Kezdeti koordináták
x_min, x_max = -2.0, 0.5
y_min, y_max = -1.25, 1.25
running = True
clock = pygame.time.Clock()
needs_redraw = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_PLUS or event.key == pygame.K_EQUALS:
MAX_ITER += 20
needs_redraw = True
elif event.key == pygame.K_MINUS:
MAX_ITER = max(20, MAX_ITER - 20)
needs_redraw = True
elif event.type == pygame.MOUSEBUTTONDOWN:
# Nagyítás az egér pozíciójára
if event.button == 1: # Bal gomb: nagyítás
mouse_x, mouse_y = pygame.mouse.get_pos()
x_range = x_max - x_min
y_range = y_max - y_min
new_x_min = x_min + mouse_x / SZERESSE * x_range - x_range / 4
new_x_max = new_x_min + x_range / 2
new_y_min = y_min + mouse_y / MAGASSAG * y_range - y_range / 4
new_y_max = new_y_min + y_range / 2
x_min, x_max, y_min, y_max = new_x_min, new_x_max, new_y_min, new_y_max
needs_redraw = True
elif event.button == 3: # Jobb gomb: kicsinyítés
x_range = (x_max - x_min) * 2
y_range = (y_max - y_min) * 2
x_min -= x_range / 4
x_max += x_range / 4
y_min -= y_range / 4
y_max += y_range / 4
needs_redraw = True
if needs_redraw:
# Számítás és rajzolás
diverge_matrix = mandelbrot_grid(SZERESSE, MAGASSAG, x_min, x_max, y_min, y_max, MAX_ITER)
# Kép pixelek létrehozása tömbből
pixel_array = np.zeros((MAGASSAG, SZERESSE, 3), dtype=np.uint8)
for y in range(MAGASSAG):
for x in range(SZERESSE):
pixel_array[y, x] = get_color(diverge_matrix[y, x], MAX_ITER)
pygame.surfarray.blit_array(screen, pixel_array)
pygame.display.flip()
needs_redraw = False
clock.tick(30)
pygame.quit()
if __name__ == "__main__":
main()
-------------
Az alábbi program egy interaktív, színátmenetes Mandelbrot-halmazt generál, amely kihasználja a NumPy tömbök sebességét, és lehetővé teszi, hogy az egérrel nagyíts ("zoom") a fraktálba, valamint valós időben növeld a részletességet.
----------
Nincsenek megjegyzések:
Megjegyzés küldése