Last active
November 24, 2024 16:01
-
-
Save vicradon/c5c1f29adb9905f31966e7edb75e361a to your computer and use it in GitHub Desktop.
A Python program to generate and visualize a Mandelbrot set
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
import numpy as np | |
import matplotlib.pyplot as plt | |
''' | |
z₀ = 0 | |
zₙ₊₁ = zₙ² + c | |
''' | |
def is_mandelbrot(complex_num, max_iter=100): | |
z = 0 | |
for n in range(max_iter): | |
z = z**2 + complex_num | |
if abs(z) > 2: | |
return False | |
return True | |
def generate_mandelbrot_points(resolution=100): | |
real = np.linspace(-2, 0.8, resolution) | |
imag = np.linspace(-1.4, 1.4, resolution) | |
points = [] | |
for r in real: | |
for i in imag: | |
c = complex(r, i) | |
if is_mandelbrot(c): | |
points.append(c) | |
return points | |
def plot_mandelbrot(points): | |
real_parts = [p.real for p in points] | |
imag_parts = [p.imag for p in points] | |
plt.figure(figsize=(10,10)) | |
plt.scatter(real_parts, imag_parts,s=1,c="black") | |
plt.axis("equal") | |
plt.show() | |
if __name__ == "__main__": | |
mandelbrot_points = generate_mandelbrot_points(resolution=100) | |
print(len(mandelbrot_points), "points in the set") | |
plot_mandelbrot(mandelbrot_points) | |
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
from PIL import Image | |
mandelbrot = Image.effect_mandelbrot((1000, 1000), (-3, -2.5, 2, 2.5), 100) | |
mandelbrot.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
100-point resolution