Last active
February 9, 2020 23:00
-
-
Save MattOates/8d4349171eacbf1e0a7358fd83da4a7a to your computer and use it in GitHub Desktop.
Dirty script to render the Mandelbrot set to the terminal
This file contains hidden or 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
sub terminal-width(:$default=100) { | |
my $width = run('tput', 'cols', :out).out.slurp-rest.trim.Int; | |
return $width < $default ?? $default !! $width; | |
} | |
sub is-mandelbrot(Complex $z0, int $max=100) { | |
my Complex $z = $z0; | |
for ^$max -> $n { | |
return $n if ($z.abs() > 2e0); | |
$z = $z**2 + $z0; | |
} | |
return $max; | |
} | |
my @shades = [" ","░","▒","▓","█"]; | |
sub MAIN(num :$mid-x=-0.4e0, num :$mid-y=0e0, num :$zoom=2e0, int :$width = terminal-width(:default<100>), int :$height=50) { | |
for ^$height -> $j { | |
my $row = ""; | |
for ^$width -> $i { | |
my num $x = $mid-x - $zoom/2e0 + $zoom*$i/$width; | |
my num $y = $mid-y - $zoom/2e0 + $zoom*$j/$height; | |
my Complex $z0 = Complex.new($x, $y); | |
if $zoom == 2e0 { | |
$row ~= @shades[floor(is-mandelbrot($z0,10) / 2.5e0)]; | |
} else { | |
$row ~= @shades[floor(is-mandelbrot($z0) / 25e0)]; | |
} | |
} | |
say $row; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment