Skip to content

Instantly share code, notes, and snippets.

@grondilu
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grondilu/a5d200996b2d6f09dbaa to your computer and use it in GitHub Desktop.
Save grondilu/a5d200996b2d6f09dbaa to your computer and use it in GitHub Desktop.
Mandelbrot with separation between IO and computations
constant MAX_ITERATIONS = 50;
my $height = +@*ARGS[0] // 31;
my $width = $height div 2;
my $re = [ -2, 1/2 ];
my $im = [ 0, 5/4];
sub linspace(@a, $size) {
@a.min, * + (@a.max - @a.min) / ($size - 1) ... @a.max
}
my @re = linspace $re, $height;
my @im = 1i «*« linspace $im, $width;
my @z = @re X+ @im;
my @color_map = map { Blob.new: .comb(/../).map({:16($_)}) }, <
0000fc 4000fc 7c00fc bc00fc fc00fc fc00bc fc007c fc0040 fc0000 fc4000 fc7c00
fcbc00 fcfc00 bcfc00 7cfc00 40fc00 00fc00 00fc40 00fc7c 00fcbc 00fcfc 00bcfc
007cfc 0040fc 7c7cfc 9c7cfc bc7cfc dc7cfc fc7cfc fc7cdc fc7cbc fc7c9c fc7c7c
fc9c7c fcbc7c fcdc7c fcfc7c dcfc7c bcfc7c 9cfc7c 7cfc7c 7cfc9c 7cfcbc 7cfcdc
7cfcfc 7cdcfc 7cbcfc 7c9cfc b4b4fc c4b4fc d8b4fc e8b4fc fcb4fc fcb4e8 fcb4d8
fcb4c4 fcb4b4 fcc4b4 fcd8b4 fce8b4 fcfcb4 e8fcb4 d8fcb4 c4fcb4 b4fcb4 b4fcc4
b4fcd8 b4fce8 b4fcfc b4e8fc b4d8fc b4c4fc 000070 1c0070 380070 540070 700070
700054 700038 70001c 700000 701c00 703800 705400 707000 547000 387000 1c7000
007000 00701c 007038 007054 007070 005470 003870 001c70 383870 443870 543870
603870 703870 703860 703854 703844 703838 704438 705438 706038 707038 607038
547038 447038 387038 387044 387054 387060 387070 386070 385470 384470 505070
585070 605070 685070 705070 705068 705060 705058 705050 705850 706050 706850
707050 687050 607050 587050 507050 507058 507060 507068 507070 506870 506070
505870 000040 100040 200040 300040 400040 400030 400020 400010 400000 401000
402000 403000 404000 304000 204000 104000 004000 004010 004020 004030 004040
003040 002040 001040 202040 282040 302040 382040 402040 402038 402030 402028
402020 402820 403020 403820 404020 384020 304020 284020 204020 204028 204030
204038 204040 203840 203040 202840 2c2c40 302c40 342c40 3c2c40 402c40 402c3c
402c34 402c30 402c2c 40302c 40342c 403c2c 40402c 3c402c 34402c 30402c 2c402c
2c4030 2c4034 2c403c 2c4040 2c3c40 2c3440 2c3040
>[^MAX_ITERATIONS];
constant black = Blob.new(0, 0, 0);
my $file = open "mandelbrot.ppm", :w, :bin;
$file.write: "P6\n$width $height\n255\n".encode('ascii');
my $writer = Supply.new;
$writer.tap(-> Blob $color { $file.write: $color });
await start {
z: for @z -> $z {
my Complex $u = 0i;
for @color_map -> $color {
$u = $u**2 + $z;
if $u.abs > 2 {
$writer.emit($color);
next z;
}
}
$writer.emit(black);
}
}
$file.close;
@zhuomingliang
Copy link

Hello, I got a exception after a long time run

jimmy@ubuntu:~$ perl6 test.p6 1024
Unhandled exception: setcodeobj needs a code ref
   at src/gen/m-BOOTSTRAP.nqp:1491  (/home/jimmy/OpenSource/MoarVM/install/languages/nqp/lib/Perl6/BOOTSTRAP.moarvm::18)
 from src/gen/m-CORE.setting:19507  (/home/jimmy/OpenSource/rakudo/../MoarVM/install/languages/perl6/runtime/CORE.setting.moarvm::74)
 from src/gen/m-CORE.setting:19490  (/home/jimmy/OpenSource/rakudo/../MoarVM/install/languages/perl6/runtime/CORE.setting.moarvm::20)
 from src/gen/m-CORE.setting:19288  (/home/jimmy/OpenSource/rakudo/../MoarVM/install/languages/perl6/runtime/CORE.setting.moarvm::48)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment