Skip to content

Instantly share code, notes, and snippets.

@pstaender
Created May 9, 2026 06:02
Show Gist options
  • Select an option

  • Save pstaender/8b9ee60e8bc0b4114263d50b72f26ca0 to your computer and use it in GitHub Desktop.

Select an option

Save pstaender/8b9ee60e8bc0b4114263d50b72f26ca0 to your computer and use it in GitHub Desktop.
# from https://gist.github.com/denilsonsa/5076dab06158eb97e55c5fa72b454cc7
def float_range(start, end, steps):
"""Similar to numpy.linspace(), but written in pure Python.
Extreme cases:
>>> list(float_range(-5, 5, 0))
[]
>>> list(float_range(-5, 5, 1))
[-5.0]
More useful cases:
>>> list(float_range(-5, 5, 2))
[-5.0, 5.0]
>>> list(float_range(-5, 5, 3))
[-5.0, 0.0, 5.0]
>>> list(float_range(-5, 5, 5))
[-5.0, -2.5, 0.0, 2.5, 5.0]
>>> list(float_range(0, 2, 5))
[0.0, 0.5, 1.0, 1.5, 2.0]
>>> list(float_range(0, 3, 4))
[0.0, 1.0, 2.0, 3.0]
Also in reverse order:
>>> list(float_range(5, -5, 5))
[5.0, 2.5, 0.0, -2.5, -5.0]
"""
if steps <= 0:
return
if steps == 1:
yield start * 1.0
return
for i in range(steps):
yield start + (end - start) * (i / (steps - 1))
def mandelbrot_0(c, iters):
z = c
nv = 0
for i in range(iters):
if abs(z) > 2:
break
z = z * z + c
nv += 1
return nv
def calculate_mandelbrot(width, height, iters):
for y in float_range(-1, 1, height):
for x in float_range(-1.5, 0.5, width):
c = mandelbrot_0(complex(x, y), iters)
rgb = round(c / iters * (256 ** 3 - 1))
print('\033[48;2;{r};{g};{b}m '.format(
r=round((rgb >> 16) & 0xFF),
g=round((rgb >> 8) & 0xFF),
b=round((rgb >> 0) & 0xFF),
), end='')
print('\033[m')
calculate_mandelbrot(80, 25, 5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment