Permalink
Browse files

mandelbrot-color.pl, README

  • Loading branch information...
1 parent 7c182d3 commit 3067d0090e1698539f5b82911411f58308096a85 @moritz moritz committed Dec 13, 2009
Showing with 33 additions and 0 deletions.
  1. +2 −0 README
  2. +31 −0 mandelbrot-color.pl
View
2 README
@@ -0,0 +1,2 @@
+The Perl 6 sccripts mandelbrot.pl and mandelbrot-color.pl emit PBM and PMM
+files to STDOUT, showing a small Mandelbrot fractal.
View
@@ -0,0 +1,31 @@
+use v6;
+
+my $height = 35;
+my $width = $height;
+my $max_iterations = 50;
+
+my $upper-right = -2 + (5/4)i;
+my $lower-left = 1/2 - (5/4)i;
+
+sub mandel(Complex $c) {
+ my $z = 0i;
+ for ^$max_iterations {
+ return $_ if ($z.abs > 2);
+ $z = $z * $z + $c;
+ }
+ return $max_iterations;
+}
+
+sub subdivide($low, $high, $count) {
+ (^$count).map({ $low + ($_ / ($count - 1)) * ($high - $low) });
+}
+
+say "P3";
+say "$width $height";
+say $max_iterations;
+
+for subdivide($upper-right.re, $lower-left.re, $height) -> $re {
+ my @line = subdivide($re + ($upper-right.im)i, $re + 0i, ($width + 1) / 2).map({ mandel($_) });
+ my $middle = @line.pop;
+ (@line, $middle, @line.reverse).map({ $_ xx 3 }).join(' ').say;
+}

0 comments on commit 3067d00

Please sign in to comment.