Skip to content

Commit 3ca5a41

Browse files
committed
Apply the low-level TR feedback
1 parent 56a57cd commit 3ca5a41

15 files changed

+45
-44
lines changed

mandelbrot-set-python/01_scatter_plot.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
def complex_matrix(xmin, xmax, ymin, ymax, pixel_density):
88
re = np.linspace(xmin, xmax, int((xmax - xmin) * pixel_density))
99
im = np.linspace(ymin, ymax, int((ymax - ymin) * pixel_density))
10-
return re[:, None] + im[None, :] * 1j
10+
return re[np.newaxis, :] + im[:, np.newaxis] * 1j
1111

1212

1313
def is_stable(c, num_iterations):
1414
z = 0
1515
for _ in range(num_iterations):
1616
z = z ** 2 + c
17-
return abs(z) < 2
17+
return abs(z) <= 2
1818

1919

2020
def get_members(c, num_iterations):
21-
return c[is_stable(c, num_iterations)]
21+
mask = is_stable(c, num_iterations)
22+
return c[mask]
2223

2324

2425
if __name__ == "__main__":

mandelbrot-set-python/02_bw_plot.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77
def complex_matrix(xmin, xmax, ymin, ymax, pixel_density):
88
re = np.linspace(xmin, xmax, int((xmax - xmin) * pixel_density))
99
im = np.linspace(ymin, ymax, int((ymax - ymin) * pixel_density))
10-
return re[:, None] + im[None, :] * 1j
10+
return re[np.newaxis, :] + im[:, np.newaxis] * 1j
1111

1212

1313
def is_stable(c, num_iterations):
1414
z = 0
1515
for _ in range(num_iterations):
1616
z = z ** 2 + c
17-
return abs(z) < 2
17+
return abs(z) <= 2
1818

1919

2020
def get_members(c, num_iterations):
21-
return c[is_stable(c, num_iterations)]
21+
mask = is_stable(c, num_iterations)
22+
return c[mask]
2223

2324

2425
if __name__ == "__main__":
2526
c = complex_matrix(-2, 0.5, -1.5, 1.5, pixel_density=512)
2627

27-
plt.imshow(is_stable(c, num_iterations=20).T, cmap="binary")
28+
plt.imshow(is_stable(c, num_iterations=20), cmap="binary")
2829
plt.gca().set_aspect("equal")
2930
plt.axis("off")
3031
plt.tight_layout()

mandelbrot-set-python/03_bw.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88

99
width, height = 512, 512
1010
scale = 0.0075
11-
black_and_white = "1"
11+
BLACK_AND_WHITE = "1"
1212

13-
image = Image.new(mode=black_and_white, size=(width, height))
13+
image = Image.new(mode=BLACK_AND_WHITE, size=(width, height))
1414
for y in range(height):
1515
for x in range(width):
16-
re = scale * (x - width / 2)
17-
im = scale * (height / 2 - y)
18-
image.putpixel((x, y), complex(re, im) not in mandelbrot_set)
16+
c = scale * complex(x - width / 2, height / 2 - y)
17+
image.putpixel((x, y), c not in mandelbrot_set)
1918

2019
image.show()

mandelbrot-set-python/04_grayscale.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88

99
width, height = 512, 512
1010
scale = 0.0075
11-
grayscale = "L"
11+
GRAYSCALE = "L"
1212

13-
image = Image.new(mode=grayscale, size=(width, height))
13+
image = Image.new(mode=GRAYSCALE, size=(width, height))
1414
for y in range(height):
1515
for x in range(width):
16-
re = scale * (x - width / 2)
17-
im = scale * (height / 2 - y)
18-
stability = 1 - mandelbrot_set.stability(complex(re, im))
19-
image.putpixel((x, y), int(stability * 255))
16+
c = scale * complex(x - width / 2, height / 2 - y)
17+
instability = 1 - mandelbrot_set.stability(c)
18+
image.putpixel((x, y), int(instability * 255))
2019

2120
image.show()

mandelbrot-set-python/05_grayscale_smooth.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
if __name__ == "__main__":
66

7-
mandelbrot_set = MandelbrotSet(max_iterations=20)
7+
mandelbrot_set = MandelbrotSet(max_iterations=20, escape_radius=1000)
88

99
width, height = 512, 512
1010
scale = 0.0075
11-
grayscale = "L"
11+
GRAYSCALE = "L"
1212

13-
image = Image.new(mode=grayscale, size=(width, height))
13+
image = Image.new(mode=GRAYSCALE, size=(width, height))
1414
for y in range(height):
1515
for x in range(width):
1616
re = scale * (x - width / 2)
1717
im = scale * (height / 2 - y)
1818
c = complex(re, im)
19-
stability = 1 - mandelbrot_set.stability(c, smooth=True)
20-
image.putpixel((x, y), int(stability * 255))
19+
instability = 1 - mandelbrot_set.stability(c, smooth=True)
20+
image.putpixel((x, y), int(instability * 255))
2121

2222
image.show()

mandelbrot-set-python/06_viewport_pixel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
if __name__ == "__main__":
88
print("This might take a while...")
99

10-
mandelbrot_set = MandelbrotSet(max_iterations=256)
10+
mandelbrot_set = MandelbrotSet(max_iterations=256, escape_radius=1000)
1111

1212
image = Image.new(mode="L", size=(512, 512))
1313
for pixel in Viewport(image, center=-0.7435 + 0.1314j, width=0.002):
1414
c = complex(pixel)
15-
stability = 1 - mandelbrot_set.stability(c, smooth=True)
16-
pixel.color = int(stability * 255)
15+
instability = 1 - mandelbrot_set.stability(c, smooth=True)
16+
pixel.color = int(instability * 255)
1717

1818
enhancer = ImageEnhance.Brightness(image)
1919
enhancer.enhance(1.25).show()

mandelbrot-set-python/07_color_palette_matplotlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def denormalize(palette):
2424
colormap = matplotlib.cm.get_cmap("twilight").colors
2525
palette = denormalize(colormap)
2626

27-
mandelbrot_set = MandelbrotSet(max_iterations=512)
27+
mandelbrot_set = MandelbrotSet(max_iterations=512, escape_radius=1000)
2828
image = Image.new(mode="RGB", size=(512, 512))
2929
viewport = Viewport(image, center=-0.7435 + 0.1314j, width=0.002)
3030
paint(mandelbrot_set, viewport, palette, smooth=True)

mandelbrot-set-python/08_color_palette_custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def denormalize(palette):
2525
gray_area = [(1 - i / 44,) * 3 for i in range(45)]
2626
palette = denormalize(exterior + gray_area + interior)
2727

28-
mandelbrot_set = MandelbrotSet(max_iterations=20)
28+
mandelbrot_set = MandelbrotSet(max_iterations=20, escape_radius=1000)
2929
image = Image.new(mode="RGB", size=(512, 512))
3030
viewport = Viewport(image, center=-0.75, width=3.5)
3131
paint(mandelbrot_set, viewport, palette, smooth=True)

mandelbrot-set-python/09_color_gradient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def make_gradient(colors, interpolation="linear"):
4343
[gradient(i / num_colors) for i in range(num_colors)]
4444
)
4545

46-
mandelbrot_set = MandelbrotSet(max_iterations=25)
46+
mandelbrot_set = MandelbrotSet(max_iterations=20, escape_radius=1000)
4747
image = Image.new(mode="RGB", size=(512, 512))
4848
viewport = Viewport(image, center=-0.75, width=3.5)
4949
paint(mandelbrot_set, viewport, palette, smooth=True)

mandelbrot-set-python/10_color_hsb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def hsb(hue_degrees: int, saturation: float, brightness: float):
1616
if __name__ == "__main__":
1717
print("This might take a while...")
1818

19-
mandelbrot_set = MandelbrotSet(max_iterations=25)
19+
mandelbrot_set = MandelbrotSet(max_iterations=20, escape_radius=1000)
2020
image = Image.new(mode="RGB", size=(512, 512))
2121
for pixel in Viewport(image, center=-0.75, width=3.5):
2222
stability = mandelbrot_set.stability(complex(pixel), smooth=True)

0 commit comments

Comments
 (0)