Permalink
Browse files

Add two versions of the mandelbrot script, one using Num internally a…

…nd the other Rat. Both are tuned to take about 2.5 minutes to execute on my MacBook Pro using today's Rakudo.
  • Loading branch information...
colomon committed Sep 1, 2010
1 parent fde9585 commit fbd6b1bd8390ec6345cbb211abc0666caa47b358
Showing with 514 additions and 0 deletions.
  1. +257 −0 mandelbrot-num.pl
  2. +257 −0 mandelbrot-rat.pl
View
@@ -0,0 +1,257 @@
+use v6;
+
+my $height = 71;
+$height = +$height;
+my $width = $height;
+my $max_iterations = 50;
+
+my $upper-right = -2.Num + (5/4.Num)i;
+my $lower-left = 1/2.Num - (5/4.Num)i;
+
+my @color_map = (
+ "0 0 0",
+ "0 0 252",
+ "64 0 252",
+ "124 0 252",
+ "188 0 252",
+ "252 0 252",
+ "252 0 188",
+ "252 0 124",
+ "252 0 64",
+ "252 0 0",
+ "252 64 0",
+ "252 124 0",
+ "252 188 0",
+ "252 252 0",
+ "188 252 0",
+ "124 252 0",
+ "64 252 0",
+ "0 252 0",
+ "0 252 64",
+ "0 252 124",
+ "0 252 188",
+ "0 252 252",
+ "0 188 252",
+ "0 124 252",
+ "0 64 252",
+ "124 124 252",
+ "156 124 252",
+ "188 124 252",
+ "220 124 252",
+ "252 124 252",
+ "252 124 220",
+ "252 124 188",
+ "252 124 156",
+ "252 124 124",
+ "252 156 124",
+ "252 188 124",
+ "252 220 124",
+ "252 252 124",
+ "220 252 124",
+ "188 252 124",
+ "156 252 124",
+ "124 252 124",
+ "124 252 156",
+ "124 252 188",
+ "124 252 220",
+ "124 252 252",
+ "124 220 252",
+ "124 188 252",
+ "124 156 252",
+ "180 180 252",
+ "196 180 252",
+ "216 180 252",
+ "232 180 252",
+ "252 180 252",
+ "252 180 232",
+ "252 180 216",
+ "252 180 196",
+ "252 180 180",
+ "252 196 180",
+ "252 216 180",
+ "252 232 180",
+ "252 252 180",
+ "232 252 180",
+ "216 252 180",
+ "196 252 180",
+ "180 252 180",
+ "180 252 196",
+ "180 252 216",
+ "180 252 232",
+ "180 252 252",
+ "180 232 252",
+ "180 216 252",
+ "180 196 252",
+ "0 0 112",
+ "28 0 112",
+ "56 0 112",
+ "84 0 112",
+ "112 0 112",
+ "112 0 84",
+ "112 0 56",
+ "112 0 28",
+ "112 0 0",
+ "112 28 0",
+ "112 56 0",
+ "112 84 0",
+ "112 112 0",
+ "84 112 0",
+ "56 112 0",
+ "28 112 0",
+ "0 112 0",
+ "0 112 28",
+ "0 112 56",
+ "0 112 84",
+ "0 112 112",
+ "0 84 112",
+ "0 56 112",
+ "0 28 112",
+ "56 56 112",
+ "68 56 112",
+ "84 56 112",
+ "96 56 112",
+ "112 56 112",
+ "112 56 96",
+ "112 56 84",
+ "112 56 68",
+ "112 56 56",
+ "112 68 56",
+ "112 84 56",
+ "112 96 56",
+ "112 112 56",
+ "96 112 56",
+ "84 112 56",
+ "68 112 56",
+ "56 112 56",
+ "56 112 68",
+ "56 112 84",
+ "56 112 96",
+ "56 112 112",
+ "56 96 112",
+ "56 84 112",
+ "56 68 112",
+ "80 80 112",
+ "88 80 112",
+ "96 80 112",
+ "104 80 112",
+ "112 80 112",
+ "112 80 104",
+ "112 80 96",
+ "112 80 88",
+ "112 80 80",
+ "112 88 80",
+ "112 96 80",
+ "112 104 80",
+ "112 112 80",
+ "104 112 80",
+ "96 112 80",
+ "88 112 80",
+ "80 112 80",
+ "80 112 88",
+ "80 112 96",
+ "80 112 104",
+ "80 112 112",
+ "80 104 112",
+ "80 96 112",
+ "80 88 112",
+ "0 0 64",
+ "16 0 64",
+ "32 0 64",
+ "48 0 64",
+ "64 0 64",
+ "64 0 48",
+ "64 0 32",
+ "64 0 16",
+ "64 0 0",
+ "64 16 0",
+ "64 32 0",
+ "64 48 0",
+ "64 64 0",
+ "48 64 0",
+ "32 64 0",
+ "16 64 0",
+ "0 64 0",
+ "0 64 16",
+ "0 64 32",
+ "0 64 48",
+ "0 64 64",
+ "0 48 64",
+ "0 32 64",
+ "0 16 64",
+ "32 32 64",
+ "40 32 64",
+ "48 32 64",
+ "56 32 64",
+ "64 32 64",
+ "64 32 56",
+ "64 32 48",
+ "64 32 40",
+ "64 32 32",
+ "64 40 32",
+ "64 48 32",
+ "64 56 32",
+ "64 64 32",
+ "56 64 32",
+ "48 64 32",
+ "40 64 32",
+ "32 64 32",
+ "32 64 40",
+ "32 64 48",
+ "32 64 56",
+ "32 64 64",
+ "32 56 64",
+ "32 48 64",
+ "32 40 64",
+ "44 44 64",
+ "48 44 64",
+ "52 44 64",
+ "60 44 64",
+ "64 44 64",
+ "64 44 60",
+ "64 44 52",
+ "64 44 48",
+ "64 44 44",
+ "64 48 44",
+ "64 52 44",
+ "64 60 44",
+ "64 64 44",
+ "60 64 44",
+ "52 64 44",
+ "48 64 44",
+ "44 64 44",
+ "44 64 48",
+ "44 64 52",
+ "44 64 60",
+ "44 64 64",
+ "44 60 64",
+ "44 52 64",
+ "44 48 64",
+);
+
+
+sub mandel(Complex $c) {
+ my $z = 0i;
+ my $i;
+ loop ($i = 0; $i < $max_iterations; $i++) {
+ if ($z.abs > 2) {
+ return $i + 1;
+ }
+ $z = $z * $z + $c;
+ }
+ return 0;
+}
+
+sub subdivide($low, $high, $count) {
+ (^$count).map({ $low + ($_.Num / ($count - 1)) * ($high - $low) });
+}
+
+say "P3";
+say "$width $height";
+say "255";
+
+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({ @color_map[$_] }).join(' ').say;
+}
+
Oops, something went wrong.

0 comments on commit fbd6b1b

Please sign in to comment.