Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 320 lines (300 sloc) 7.076 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");
2528f02 @colomon Switch to DrawRgbImage to display, loop instead of for.
authored
237 constant GdkRgb = CLR::("Gdk.Rgb,$GDK");
238 constant GdkRgbDither = CLR::("Gdk.RgbDither,$GDK");
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
239 constant GdkColor = CLR::("Gdk.Color,$GDK");
240 constant GtkDrawingArea = CLR::("Gtk.DrawingArea,$GTK");
241 constant SystemByte = CLR::("System.Byte");
2528f02 @colomon Switch to DrawRgbImage to display, loop instead of for.
authored
242 constant ByteArray = CLR::("System.Byte[]");
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
243
244 my @red = @color_map.map({ SystemByte.Parse($_.comb(/\d+/)[0]) });
245 my @green = @color_map.map({ SystemByte.Parse($_.comb(/\d+/)[1]) });
246 my @blue = @color_map.map({ SystemByte.Parse($_.comb(/\d+/)[2]) });
247
4b036a1 @colomon Put my %bitmaps in a more sensible place.
authored
248 my %bitmaps;
249
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
250 Application.Init;
2528f02 @colomon Switch to DrawRgbImage to display, loop instead of for.
authored
251 GdkRgb.Init;
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
252 my $window = Window.new("mandelbrot");
6e4bdd6 @colomon Clean up a tad.
authored
253 my $windowSizeX = $size; my $windowSizeY = $size;
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
254 $window.Resize($windowSizeX, $windowSizeY); # TODO: resize at runtime NYI
255 my $drawingarea = GtkDrawingArea.new;
256 $drawingarea.add_ExposeEvent(&ExposeEvent);
257 $window.add_DeleteEvent(&DeleteEvent);
258 $window.Add($drawingarea);
259 $window.ShowAll;
260 Application.Run; # end of main program, it's all over when this returns
261
262 sub DeleteEvent($obj, $args) { #OK not used
263 Application.Quit;
264 };
265
266 sub ExposeEvent($obj, $args)
267 {
268 $args; # suppress "declared but not used" "Potential difficulties"
269
270 my $start-time = time;
271 my $windowX=0; my $windowY=0; my $windowWidth=0; my $windowHeight=0; my $windowDepth=0;
3c71b6e @colomon Replace repeated calls to $obj.GdkWindow with $window, for slight tim…
authored
272 my $window = $obj.GdkWindow;
273 $window.GetGeometry($windowX, $windowY, $windowWidth, $windowHeight, $windowDepth);
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
274
975571e @colomon Only calculate $delta-re and use it for both values, to make sure our…
authored
275 my $delta = ($lower-left.re - $upper-right.re) / $windowWidth;
feea17f @colomon Store mandelbrot bitmaps once they have been constructed, so they onl…
authored
276
277 my $hash-tag = HashTag($upper-right, $delta, $windowX, $windowY, $windowWidth, $windowHeight);
278 unless %bitmaps{$hash-tag}:exists {
279 my $bytes = ByteArray.new($windowWidth * 3 * $windowHeight);
f3fe5df @colomon Optimizations: only create a GC once for each color needed, and don't…
authored
280
feea17f @colomon Store mandelbrot bitmaps once they have been constructed, so they onl…
authored
281 my $counter = 0;
282 my ($x, $y);
283 loop ($y = 0; $y < $windowHeight; $y++) {
284 my $c = $upper-right - $y * $delta * i;
285 loop ($x = 0; $x < $windowWidth; $x++) {
286 my $value = mandel($c);
287 $bytes.Set($counter++, @red[$value]);
288 $bytes.Set($counter++, @green[$value]);
289 $bytes.Set($counter++, @blue[$value]);
290 $c += $delta;
291 }
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
292 }
feea17f @colomon Store mandelbrot bitmaps once they have been constructed, so they onl…
authored
293
294 %bitmaps{$hash-tag} = $bytes;
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
295 }
2528f02 @colomon Switch to DrawRgbImage to display, loop instead of for.
authored
296
297 my $gc = GdkGC.new($window);
298 $window.DrawRgbImage($gc, $windowX, $windowY, $windowWidth, $windowHeight,
feea17f @colomon Store mandelbrot bitmaps once they have been constructed, so they onl…
authored
299 GdkRgbDither.Normal, %bitmaps{$hash-tag}, $windowWidth * 3);
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
300
301 my $elapsed = time - $start-time;
302 say "$elapsed seconds";
303 };
304
feea17f @colomon Store mandelbrot bitmaps once they have been constructed, so they onl…
authored
305 sub HashTag($upper-right, $delta, $windowX, $windowY, $windowWidth, $windowHeight) {
306 ($upper-right, $delta, $windowX, $windowY, $windowWidth, $windowHeight).join("~");
307 }
308
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
309 sub mandel(Complex $c) {
310 my $z = 0i;
311 my $i;
312 loop ($i = 0; $i < $max_iterations; $i++) {
c750a25 @colomon Remove unneeded parens.
authored
313 if $z.abs > 2 {
13e94c1 @colomon Simple working Gtk-based graphic Mandelbrot code.
authored
314 return $i + 1;
315 }
316 $z = $z * $z + $c;
317 }
318 return 0;
319 }
Something went wrong with that request. Please try again.