Skip to content

Instantly share code, notes, and snippets.

@adrhill
Last active May 21, 2021 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrhill/3d28e2a9df18603f0379e3457bb90cdd to your computer and use it in GitHub Desktop.
Save adrhill/3d28e2a9df18603f0379e3457bb90cdd to your computer and use it in GitHub Desktop.
Zoom in on the Mandelbrot set from the comfort of your terminal!
# ]add https://github.com/IanButterworth/VideoInTerminal.jl
# ]add ImageCore
using VideoInTerminal
using ImageCore
"""
Run n Mandelbrot iterations.
"""
function mb_iterations(c::AbstractArray{T}; n=50) where {T<:Complex}
z = zeros(T, size(c))
for i in 1:n
z .= z .^ 2 .+ c
end
return z
end
"""
Turn Mandelbrot fractal into gray-scale image.
"""
function mb2gray(mat; threshold=2)
mat = clamp.(abs.(mat), 0, threshold)
replace!(mat, NaN => threshold)
return Gray.(mat / threshold)
end
"""
Draw a frame of the Mandelbrot fractal around (cx, cy).
"""
function mb_fractal(width; cx=0, cy=0, h=200, w=200)
rowrange = range(-width / 2, width / 2; length=h) .+ cy
colrange = range(-width / 2, width / 2; length=w) .+ cx
cmat = [Complex(c, r) for r in rowrange, c in colrange]
frac = mb_iterations(cmat)
return mb2gray(frac)
end
"""
Zoom in on seahorse valley (https://mathworld.wolfram.com/SeaHorseValley.html).
"""
function seahorse_valley(; frames=200)
zoomrange = 2 .^ range(0, -10; length=frames)
return play(map(i -> mb_fractal(i; cx=-0.75, cy=-0.1), zoomrange))
end
seahorse_valley()
@adrhill
Copy link
Author

adrhill commented May 21, 2021

mandelbrot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment