Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Mandelbrot Set algorithm in Perl6
Generates the Mandelbrot Set fractal using Perl 6. To be submitted to "The Computer Language Benchmarks Game".
  • Loading branch information
rsiqueira committed Jan 2, 2016
1 parent f3a6365 commit b052f85
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions categories/shootout/mandelbrot.p6
@@ -0,0 +1,97 @@
use v6;

=begin pod
=TITLE Generates the Mandelbrot Set fractal
=AUTHOR Rodrigo Siqueira (@rsiqueira)
To be submitted to "The Computer Language Benchmarks Game", to test Perl6 speed / performance
in comparison with other programming languages.
L<https://benchmarksgame.alioth.debian.org/u32q/mandelbrot-description.html>
L<http://benchmarksgame.alioth.debian.org/u32/performance.php?test=mandelbrot>
L<http://benchmarksgame.alioth.debian.org/u64/performance.php?test=mandelbrot>
USAGE: perl6 mandelbrot.p6 200 > image.pbm
=end pod



constant $MAXITER = 50;
constant $xmin = -1.5;
constant $ymin = -1.0;

my $w = (@*ARGS[0] || 200);
my $h = $w;

my Rat $invN = 2/$w;

print "P4\n$w $h\n"; # PBM image header

my Int $bit_num = 0;

my Int $is_set=1;

my Int $byte = 0;

for (0..$h-1) -> $y {

my Rat $y_coord = $y * $invN + $ymin;

for 0..$w-1 -> $x {

my Rat $x_coord = $x * $invN + $xmin;

my Complex $C = $x_coord + $y_coord\i;

my Complex $z=0+0i;

for (0..$MAXITER) { # Iterate

$z = $z * $z + $C;

if ($z.abs > 4) { # Outer area of the Mandelbrot Set
$is_set = 0;
last;
}

}

# if ($is_set) { # Inner area of the Mandelbrot Set
# print "o";
# } else { # Outer area
# print ".";
# }

$bit_num++;

$byte = $byte +< 1;
if ($is_set) {
$byte = $byte +| 1;
}

if ($bit_num == 8) {

$bit_num = 0;
my $buf = Buf.new( $byte );
$*OUT.write($buf);
$byte = 0;

} elsif ($x == $w-1) { # (8 - $w%8) bits to fill out the last byte in the row

$byte = $byte +< (8 - $w%8);
my $buf = Buf.new( $byte );
$*OUT.write($buf);
$byte = 0;
$bit_num = 0;
}

$is_set=1;

} # Next x

# print "\n";

} # Next y

0 comments on commit b052f85

Please sign in to comment.