master
E. Almqvist 3 years ago
parent daf9e375b8
commit f687a4bcd5
  1. 49
      mas/fractals/mandel.py

@ -2,8 +2,45 @@
import pygame import pygame
W, H = 500, 500
ITER = 5
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def square(self):
self.x = self.x**2 - self.y**2
self.y = 2*self.x*self.y
def add(self, p2):
self.x += p2.x
self.y += p2.y
def mandel(n: int, c: Point):
if n == 0:
return Point(0, 0)
else:
newp = mandel(n-1, c)
newp.square()
newp.add(c)
return newp
print("Plotting...")
plots = {}
for x in range(0, W):
for y in range(0, H):
print(f"{x=} {y=}", end="\r")
z = mandel(ITER, Point(x - W/2, y - H/2))
plots[(x,y)] = (0, 0, 0) if z.x < 2 else (255, 255, 255)
print("Init render")
pygame.init() pygame.init()
window = pygame.display.set_mode( (1600, 900) ) window = pygame.display.set_mode( (W, H) )
run = True run = True
while run: while run:
@ -13,12 +50,12 @@ while run:
window.fill(0) window.fill(0)
space = pygame.Rect(window.get_rect().center, (0, 0)).inflate(*([min(window.get_size())//2]*2))
for x in range(space.width): for coord in plots:
y = 2*x x, y = coord
col = (255, 255, 255) col = plots[coord]
window.set_at((space.left + x, space.bottom - y), col) window.set_at(coord, col)
pygame.display.flip() pygame.display.flip()

Loading…
Cancel
Save