Last active
August 18, 2020 15:25
-
-
Save johannvonissou/5a14eb119dfaf6f22c82af733b7e1a9d to your computer and use it in GitHub Desktop.
Fractal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
from time import time | |
import pygame | |
import math | |
c = lambda z: z*z - 1.476 | |
REFRESH_TIMES = 20 | |
COLOR = (255,255,0) | |
ITERATIONS = 100 | |
BORDER = 2.5 | |
ZOOM = 250 | |
DIM = 800 | |
pygame.init() | |
screen = pygame.display.set_mode((DIM, DIM)) | |
pygame.display.set_caption("Fractale") | |
def julia(x, y): | |
z = complex(x, y) | |
for i in range(ITERATIONS): | |
z = c(z) | |
dr = abs(x - z.real) | |
di = abs(y - z.imag) | |
if dr > BORDER or di > BORDER: | |
return (False, i) | |
return (True, ITERATIONS) | |
def mendelbrot(x, y): | |
tx = 0 | |
ty = 0 | |
for i in range(ITERATIONS): | |
tx = tx*tx - ty*ty + x | |
ty = 2*tx*ty + y | |
dr = abs(x - tx) | |
di = abs(y - ty) | |
if (tx*tx + ty*ty) < 4: | |
return (False, i) | |
return (True, ITERATIONS) | |
def grid(x, y): | |
return x == 0 or y == 0 | |
def display(a): | |
return (a - DIM/2) / ZOOM | |
def color_bw(i): | |
p = int(round((i / ITERATIONS) * 255, 0)) | |
return (p,p,p) | |
def color_from(color, i): | |
r = int(round((i / ITERATIONS) * color[0], 0)) | |
g = int(round((i / ITERATIONS) * color[1], 0)) | |
b = int(round((i / ITERATIONS) * color[2], 0)) | |
return (r,g,b) | |
#for x in range(HEIGH - int(round(HEIGH/2, 0)), HEIGH + int(round(HEIGH/2, 0))): | |
# for y in range(WITDH - int(round(WITDH/2, 0)), WITDH + int(round(WITDH/2, 0))): | |
t = time() | |
refresh = int(round(DIM/REFRESH_TIMES, 0)) | |
total = [] | |
for x in range(DIM): | |
for y in range(DIM): | |
scalex = display(x) | |
scaley = display(y) | |
r = julia(scalex, scaley) | |
if r[0]: | |
screen.set_at((x,y), COLOR) | |
total.append((x,y)) | |
else: | |
screen.set_at((x,y), color_from(COLOR, r[1])) | |
if x % refresh == 0: pygame.display.flip() | |
pygame.display.flip() | |
print("--- FIN ---") | |
#print("total:", total) | |
print("total_len:", len(total)) | |
print("time:", time() - t) | |
loop = True | |
while loop: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
loop = False | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment