-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractals.py
executable file
·58 lines (49 loc) · 1.61 KB
/
fractals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/python3
import numpy as np
import matplotlib.pyplot as plt
def getFractal(draw_julia: bool, constant=0.0):
x_res = 500
y_res = 500
if draw_julia:
x_linspace = np.linspace(-1.5, 1.5, y_res)
else:
x_linspace = np.linspace(-2.0, 1.0, x_res)
y_linspace = np.linspace(-1.5, 1.5, y_res)
X, Y = np.meshgrid(x_linspace, y_linspace)
# convert to Grid
Z = X + Y * 1j
# Starting Copy
C = np.copy(Z)
# keep count for color
count = np.zeros_like(Z, dtype=int)
# get max values
rmax = np.maximum(abs(C), 2)
k = 0
while k < 100:
# initial condition used for the meshgrids
cond = abs(Z) < rmax
# apply for each value that does not exceed/diverge
if draw_julia:
Z[cond] = Z[cond] ** 2 + constant
else:
Z[cond] = Z[cond] ** 2 + C[cond]
count[cond] += 1
k += 1
return C, count
if __name__ == "__main__":
fig, ax = plt.subplots(layout="constrained")
C, count = getFractal(draw_julia=False, constant=0)
ax.scatter(C.real, C.imag, s=1, c=count)
plt.plot()
plt.savefig("mandelbrot.png")
# fig, ax = plt.subplots(layout="constrained")
# C, count = getFractal(draw_julia=True, constant=0.5 - 0.5j)
# ax.scatter(C.real, C.imag, s=1, c=count)
# plt.plot()
# plt.savefig("julia0.5.png")
fig, ax = plt.subplots(layout="constrained")
# C, count = getFractal(draw_julia=True, constant=0.8 - 0.8j)
C, count = getFractal(draw_julia=True, constant=0.285+0.01j)
ax.scatter(C.real, C.imag, s=1, c=count)
plt.plot()
plt.savefig("julia.png")