This repository has been archived by the owner on Sep 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbrot7.p6
111 lines (84 loc) · 2.52 KB
/
mbrot7.p6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env perl6
use v6;
use SDL2::Raw;
my int $width = 320;
my int $height = 240;
my $hwidth = ($width /2).Int;
my $hheight = ($height/2).Int;
my int $wid = 4;
my (int $xcenter, int $ycenter) = (-1,0);
constant SDL_WINDOW_SHOWN = 0x00000004;
my $t0 = DateTime.now.Instant;
SDL_Init(VIDEO);
my $window = SDL_CreateWindow("Mandelbrot",
SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
$width, $height, SDL_WINDOW_SHOWN);
my $render = SDL_CreateRenderer($window, -1, 1);
SDL_SetRenderDrawColor($render, 0, 0,128, 0);
SDL_RenderClear($render);
SDL_RenderPresent($render);
#$PROCESS::SCHEDULER=ThreadPoolScheduler.new(initial_threads => 0, max_threads => 3, uncaught_handler => Callable);
my $c = Channel.new;
my $plotting = start {
my $closed = $c.closed;
loop {
if $c.poll -> $item {
my ($xcoord, $ycoord, $color) = $item;
plot($render, $xcoord, $ycoord, $color);
#say $color;
}
elsif $closed {
last;
}
}
}
my @promises;
push @promises: start {
for ( 0..$width) -> int $xcoord {
for ( 0..$height-1) -> int $ycoord {
my $ca = ($xcoord - $hwidth) / $width * $wid + $xcenter;
my $cb = ($ycoord - $hheight) / $width * 1 * $wid + $ycenter;
my ($res, $i) = mandelbrot($ca + $cb * i);
my $hcolor=128;
$hcolor=10*$i if $i;
my $color;
if !$res.defined {
$color = (0,0,0);
}
else {
if $i < 5 {
$color = (0, 0,128);
} elsif $i > 5 and $i < 7 {
$color = (0, 0, $hcolor);
} elsif $i > 7 and $i < 10 {
$color = (0, $hcolor, 0);
} else {
$color = ($hcolor,0, 0);
}
}
my $item = ($xcoord, $ycoord, $color);
# say "here: " ~ $item;
$c.send($item);
}
}
}
await @promises;
$c.close;
await $plotting;
SDL_RenderPresent($render);
say DateTime.now.Instant-$t0 ~ " sec(s)";
prompt("wait..") unless %*ENV{'MBROT_BATCH'};
SDL_Quit();
sub plot($render, $x,$y,$c) {
my ($c1, $c2, $c3) = $c; # XXX
SDL_SetRenderDrawColor($render,$c1,$c2,$c3,0);
SDL_RenderDrawPoint($render, $x, $y);
SDL_RenderPresent($render);
}
sub mandelbrot(Complex $c) {
my $z = $c;
for (1 .. 20) -> $i {
$z = $z * $z + $c;
return ($z,$i) if $z.abs> 2;
}
}