Skip to content

Commit

Permalink
Refactor xy to complex plane mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
IlIllII committed Feb 7, 2024
1 parent 8f701c6 commit feadbfc
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions mandelbrot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,33 @@ def julia(c, max_iter, z):
z = z * z + c
return max_iter


def xy_to_mandelbrot(x, y):
return complex(
3.5 * (x / (width / 2) - 0.5) / zoom + mandelbrot_shift_x,
2 * (y / height - 0.5) / zoom,
)


def xy_to_julia(x, y):
return complex(
3.5 * ((x - width / 2) / (width / 2) - 0.5) / julia_zoom
+ julia_shift_x,
3.5 * ((x - width / 2) / (width / 2) - 0.5) / julia_zoom + julia_shift_x,
2 * (y / height - 0.5) / julia_zoom + julia_shift_y,
)


def draw_mandelbrot():
for x in range(width // 2): # Left panel
for x in range(width // 2):
for y in range(height):
# c = complex(
# 3.5 * (x / (width / 2) - 0.5) / zoom + mandelbrot_shift_x,
# 2 * (y / height - 0.5) / zoom,
# )
c = xy_to_mandelbrot(x, y)
color = mandelbrot(c, max_iter)
green = int(255 * color / max_iter)
screen.set_at((x, y), (green, green, 255))


def draw_julia(c):
for x in range(width // 2, width): # Right panel
for x in range(width // 2, width):
for y in range(height):
# z = complex(
# 3.5 * ((x - width / 2) / (width / 2) - 0.5) / julia_zoom
# + julia_shift_x,
# 2 * (y / height - 0.5) / julia_zoom + julia_shift_y,
# )
z = xy_to_julia(x, y)
color = julia(c, max_iter, z)
green = int(255 * color / max_iter)
Expand All @@ -82,10 +75,7 @@ def main():
mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_on_mandelbrot = mouse_x < width // 2
if mouse_on_mandelbrot:
c = complex(
3.5 * (mouse_x / (width / 2) - 0.5) / zoom + mandelbrot_shift_x,
2 * (mouse_y / height - 0.5) / zoom,
)
c = xy_to_mandelbrot(mouse_x, mouse_y)
draw_julia(c)

pygame.display.flip()
Expand Down

0 comments on commit feadbfc

Please sign in to comment.