- mandelbrot.git
- main.py
This file ( 1kB ) exceeds the allowed full mode (48 kb) size.
The editor full hight is disabled, only scrolling is allowed..
If you wish to edit a file, it is recommended to use the scroll mode as some users do not like the full height
mode, although some users like it.
"""
References:
1. http://linas.org/art-gallery/escape/escape.html
2. https://www.codingame.com/playgrounds/2358/how-to-plot-the-mandelbrot-set
"""
from math import log2
from PIL import Image, ImageDraw
from typing import Tuple
escape_radius = 20.0
max_iter = 20
image_size = (600, 600)
origin = (450, 300)
zoom = 200
def mandelbrot(c: complex) -> float:
z = complex(0)
iter_count = 0
while iter_count < max_iter and abs(z) <= escape_radius:
z = z * z + c
iter_count += 1
return iter_count if iter_count == max_iter else iter_count - log2(log2(abs(z)))
def colormap_lookup(mu) -> Tuple[int, int, int]:
hue = int(255 * mu / max_iter)
saturation = 255
value = 255 if mu < max_iter else 0
return hue, saturation, value
def main():
img = Image.new("HSV", image_size, (0, 0, 0))
draw = ImageDraw.Draw(img)
for x in range(0, image_size[0]):
for y in range(0, image_size[1]):
c = complex((x - origin[0]) / zoom, (y - origin[1]) / zoom)
mu = mandelbrot(c)
draw.point([x, y], colormap_lookup(mu))
img.show()
if __name__ == "__main__":
main()