Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 307 lines (288 sloc) 6.505 kb
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
1 use v6;
2
6e4bdd6 @colomon Clean up a tad.
authored
3 my $size = @*ARGS[0] // 321;
4 $size = +$size;
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
5 my $max_iterations = 50;
6
7 my $upper-right = -2 + (5/4)i;
8 my $lower-left = 1/2 - (5/4)i;
9
10 my @color_map = (
11 "0 0 0",
12 "0 0 252",
13 "64 0 252",
14 "124 0 252",
15 "188 0 252",
16 "252 0 252",
17 "252 0 188",
18 "252 0 124",
19 "252 0 64",
20 "252 0 0",
21 "252 64 0",
22 "252 124 0",
23 "252 188 0",
24 "252 252 0",
25 "188 252 0",
26 "124 252 0",
27 "64 252 0",
28 "0 252 0",
29 "0 252 64",
30 "0 252 124",
31 "0 252 188",
32 "0 252 252",
33 "0 188 252",
34 "0 124 252",
35 "0 64 252",
36 "124 124 252",
37 "156 124 252",
38 "188 124 252",
39 "220 124 252",
40 "252 124 252",
41 "252 124 220",
42 "252 124 188",
43 "252 124 156",
44 "252 124 124",
45 "252 156 124",
46 "252 188 124",
47 "252 220 124",
48 "252 252 124",
49 "220 252 124",
50 "188 252 124",
51 "156 252 124",
52 "124 252 124",
53 "124 252 156",
54 "124 252 188",
55 "124 252 220",
56 "124 252 252",
57 "124 220 252",
58 "124 188 252",
59 "124 156 252",
60 "180 180 252",
61 "196 180 252",
62 "216 180 252",
63 "232 180 252",
64 "252 180 252",
65 "252 180 232",
66 "252 180 216",
67 "252 180 196",
68 "252 180 180",
69 "252 196 180",
70 "252 216 180",
71 "252 232 180",
72 "252 252 180",
73 "232 252 180",
74 "216 252 180",
75 "196 252 180",
76 "180 252 180",
77 "180 252 196",
78 "180 252 216",
79 "180 252 232",
80 "180 252 252",
81 "180 232 252",
82 "180 216 252",
83 "180 196 252",
84 "0 0 112",
85 "28 0 112",
86 "56 0 112",
87 "84 0 112",
88 "112 0 112",
89 "112 0 84",
90 "112 0 56",
91 "112 0 28",
92 "112 0 0",
93 "112 28 0",
94 "112 56 0",
95 "112 84 0",
96 "112 112 0",
97 "84 112 0",
98 "56 112 0",
99 "28 112 0",
100 "0 112 0",
101 "0 112 28",
102 "0 112 56",
103 "0 112 84",
104 "0 112 112",
105 "0 84 112",
106 "0 56 112",
107 "0 28 112",
108 "56 56 112",
109 "68 56 112",
110 "84 56 112",
111 "96 56 112",
112 "112 56 112",
113 "112 56 96",
114 "112 56 84",
115 "112 56 68",
116 "112 56 56",
117 "112 68 56",
118 "112 84 56",
119 "112 96 56",
120 "112 112 56",
121 "96 112 56",
122 "84 112 56",
123 "68 112 56",
124 "56 112 56",
125 "56 112 68",
126 "56 112 84",
127 "56 112 96",
128 "56 112 112",
129 "56 96 112",
130 "56 84 112",
131 "56 68 112",
132 "80 80 112",
133 "88 80 112",
134 "96 80 112",
135 "104 80 112",
136 "112 80 112",
137 "112 80 104",
138 "112 80 96",
139 "112 80 88",
140 "112 80 80",
141 "112 88 80",
142 "112 96 80",
143 "112 104 80",
144 "112 112 80",
145 "104 112 80",
146 "96 112 80",
147 "88 112 80",
148 "80 112 80",
149 "80 112 88",
150 "80 112 96",
151 "80 112 104",
152 "80 112 112",
153 "80 104 112",
154 "80 96 112",
155 "80 88 112",
156 "0 0 64",
157 "16 0 64",
158 "32 0 64",
159 "48 0 64",
160 "64 0 64",
161 "64 0 48",
162 "64 0 32",
163 "64 0 16",
164 "64 0 0",
165 "64 16 0",
166 "64 32 0",
167 "64 48 0",
168 "64 64 0",
169 "48 64 0",
170 "32 64 0",
171 "16 64 0",
172 "0 64 0",
173 "0 64 16",
174 "0 64 32",
175 "0 64 48",
176 "0 64 64",
177 "0 48 64",
178 "0 32 64",
179 "0 16 64",
180 "32 32 64",
181 "40 32 64",
182 "48 32 64",
183 "56 32 64",
184 "64 32 64",
185 "64 32 56",
186 "64 32 48",
187 "64 32 40",
188 "64 32 32",
189 "64 40 32",
190 "64 48 32",
191 "64 56 32",
192 "64 64 32",
193 "56 64 32",
194 "48 64 32",
195 "40 64 32",
196 "32 64 32",
197 "32 64 40",
198 "32 64 48",
199 "32 64 56",
200 "32 64 64",
201 "32 56 64",
202 "32 48 64",
203 "32 40 64",
204 "44 44 64",
205 "48 44 64",
206 "52 44 64",
207 "60 44 64",
208 "64 44 64",
209 "64 44 60",
210 "64 44 52",
211 "64 44 48",
212 "64 44 44",
213 "64 48 44",
214 "64 52 44",
215 "64 60 44",
216 "64 64 44",
217 "60 64 44",
218 "52 64 44",
219 "48 64 44",
220 "44 64 44",
221 "44 64 48",
222 "44 64 52",
223 "44 64 60",
224 "44 64 64",
225 "44 60 64",
226 "44 52 64",
227 "44 48 64",
228 );
229
230 constant $GTK = "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
231 constant $GDK = "gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
232
233 constant Application = CLR::("Gtk.Application,$GTK");
234 constant Window = CLR::("Gtk.Window,$GTK");
235 constant GdkCairoHelper = CLR::("Gdk.CairoHelper,$GDK");
236 constant GdkGC = CLR::("Gdk.GC,$GDK");
237 constant GdkColor = CLR::("Gdk.Color,$GDK");
238 constant GtkDrawingArea = CLR::("Gtk.DrawingArea,$GTK");
239 constant SystemByte = CLR::("System.Byte");
240
241 my @red = @color_map.map({ SystemByte.Parse($_.comb(/\d+/)[0]) });
242 my @green = @color_map.map({ SystemByte.Parse($_.comb(/\d+/)[1]) });
243 my @blue = @color_map.map({ SystemByte.Parse($_.comb(/\d+/)[2]) });
244
245 Application.Init;
246 my $window = Window.new("mandelbrot");
6e4bdd6 @colomon Clean up a tad.
authored
247 my $windowSizeX = $size; my $windowSizeY = $size;
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
248 $window.Resize($windowSizeX, $windowSizeY); # TODO: resize at runtime NYI
249 my $drawingarea = GtkDrawingArea.new;
250 $drawingarea.add_ExposeEvent(&ExposeEvent);
251 $window.add_DeleteEvent(&DeleteEvent);
252 $window.Add($drawingarea);
253 $window.ShowAll;
254 Application.Run; # end of main program, it's all over when this returns
255
256 sub DeleteEvent($obj, $args) { #OK not used
257 Application.Quit;
258 };
259
260 sub ExposeEvent($obj, $args)
261 {
262 $args; # suppress "declared but not used" "Potential difficulties"
263
264 my $start-time = time;
265 my $windowX=0; my $windowY=0; my $windowWidth=0; my $windowHeight=0; my $windowDepth=0;
266 $obj.GdkWindow.GetGeometry($windowX, $windowY, $windowWidth, $windowHeight, $windowDepth);
267
6e4bdd6 @colomon Clean up a tad.
authored
268 my $delta-re = ($lower-left.re - $upper-right.re) / $windowWidth;
269 my $delta-im = ($upper-right.im - $lower-left.im) / $windowHeight;
f3fe5df @colomon Optimizations: only create a GC once for each color needed, and don't co...
authored
270
271 my %colors;
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
272
273 for 0 .. $windowHeight -> $y {
f3fe5df @colomon Optimizations: only create a GC once for each color needed, and don't co...
authored
274 my $c = $upper-right - $y * $delta-im * i;
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
275 for 0 .. $windowWidth -> $x {
276 my $value = mandel($c);
f3fe5df @colomon Optimizations: only create a GC once for each color needed, and don't co...
authored
277
278 unless %colors{$value}:exists {
279 my $gc = GdkGC.new($obj.GdkWindow);
280 my $color = GdkColor.new(@red[$value], @green[$value], @blue[$value]);
281 $obj.GdkWindow.Colormap.AllocColor($color, False, True);
282 $gc.Foreground = $color;
283 %colors{$value} = $gc;
284 }
285
286 $obj.GdkWindow.DrawPoint(%colors{$value}, $x, $y);
287
288 $c += $delta-re;
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
289 }
290 }
291
292 my $elapsed = time - $start-time;
293 say "$elapsed seconds";
294 };
295
296 sub mandel(Complex $c) {
297 my $z = 0i;
298 my $i;
299 loop ($i = 0; $i < $max_iterations; $i++) {
300 if ($z.abs > 2) {
301 return $i + 1;
302 }
303 $z = $z * $z + $c;
304 }
305 return 0;
306 }
Something went wrong with that request. Please try again.