Last active
August 13, 2018 19:02
-
-
Save MattOates/6b18dfbbf3cf771a75030753da10298b to your computer and use it in GitHub Desktop.
Quick and dirty script for plotting the Mandelbrot set in the terminal using unicode shading characters. An example invocation: perl6 mandelbrot.p6 --mid-x=-0.73e0 --mid-y=0.246e0 --zoom=0.03e0 --height=300
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
use Terminal::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