Created
June 8, 2022 03:04
-
-
Save brenhinkeller/ca2246ab0928e109e281a4d540010b2d to your computer and use it in GitHub Desktop.
Compile an executable that will print an ascii image of the Mandelbrot Set to the terminal
This file contains 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
# Looks best when run in a terminal with the tab stop width set to four spaces. | |
using StaticTools, StaticCompiler | |
function mandelbrot(zᵣ, zᵢ) | |
cᵣ, cᵢ = zᵣ, zᵢ | |
maxiter = 999 | |
for n = 1:maxiter | |
zᵣ², zᵢ² = zᵣ^2, zᵢ^2 | |
if zᵣ² + zᵢ² > 4 | |
return n-1 | |
end | |
zᵣ, zᵢ = zᵣ²-zᵢ²+cᵣ, 2*zᵣ*zᵢ+cᵢ | |
end | |
return maxiter | |
end | |
function printmandel(argc::Int, argv::Ptr{Ptr{UInt8}}) | |
argc == 3 || (printf(stderrp(), c"Incorrect number of command-line arguments\n"); return) | |
rows = parse(Int64, argv, 2) # First command-line argument | |
cols = parse(Int64, argv, 3) # Second command-line argument | |
M = MallocArray{Int64}(undef, rows, cols) | |
i½, j½ = rows/2, cols/2 | |
i₀, j₀ = 0.131469775, -1.2223015 | |
@inbounds for n = 1:2000 | |
zoom = 1 + 0.01n | |
for i=1:rows | |
for j=1:cols | |
M[i,j] = mandelbrot(j₀ + (j-j½)*2/cols/zoom, i₀ + (i-i½)*2/rows/zoom) | |
end | |
end | |
printf(c"\n\n\n\n\n") | |
printf(M) | |
usleep(1000) | |
end | |
end | |
compile_executable(printmandel, (Int64, Ptr{Ptr{UInt8}}), "./") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment