Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2d21a42041
Fetching contributors…

Cannot retrieve contributors at this time

executable file 530 lines (473 sloc) 12.749 kb
use v6;
use Threads;
my $size = @*ARGS[0] // 321;
$size = +$size;
my $max_iterations = 50;
my $upper-right = -2 + (5/4)i;
my $lower-left = 1/2 - (5/4)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",
);
constant $GTK = "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant $GDK = "gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant Application = CLR::("Gtk.Application,$GTK");
constant Window = CLR::("Gtk.Window,$GTK");
constant GdkCairoHelper = CLR::("Gdk.CairoHelper,$GDK");
constant GdkGC = CLR::("Gdk.GC,$GDK");
constant GdkRgb = CLR::("Gdk.Rgb,$GDK");
constant GdkRgbDither = CLR::("Gdk.RgbDither,$GDK");
constant GdkColor = CLR::("Gdk.Color,$GDK");
constant GtkDrawingArea = CLR::("Gtk.DrawingArea,$GTK");
constant GtkEventBox = CLR::("Gtk.EventBox,$GTK");
constant SystemByte = CLR::("System.Byte");
constant SystemIntPtr = CLR::("System.IntPtr");
constant ByteArray = CLR::("System.Byte[]");
my @red = @color_map.map({ SystemByte.Parse($_.comb(/\d+/)[0]) });
my @green = @color_map.map({ SystemByte.Parse($_.comb(/\d+/)[1]) });
my @blue = @color_map.map({ SystemByte.Parse($_.comb(/\d+/)[2]) });
my @windows;
class WorkQueue {
my $monitor = Monitor.new; # NIECZA no class attributes
method monitor() { $monitor }
my @queue;
method new() { self }
method push($item) {
$monitor.lock({
push @queue, $item;
$monitor.pulse;
});
}
method shift() {
$monitor.lock({
unless @queue {
say "Waiting for work at {times[0]}";
$monitor.wait until @queue;
say "Got work at {times[0]}";
}
shift @queue;
})
}
method run() {
loop {
my $item = self.shift;
next if $item.cancelled;
$item.run.();
$item.mark-done;
}
}
}
for ^(%*ENV<THREADS> // CLR::System::Environment.ProcessorCount) {
Thread.new({ WorkQueue.run });
}
class WorkItem {
has Bool $!done = False;
has Bool $!cancelled = False;
has Callable &.run;
has Callable &.done-cb;
method is-done() { WorkQueue.monitor.lock({ $!done }) }
method mark-done() {
&.done-cb.() unless WorkQueue.monitor.lock({ $!done++ })
}
method cancelled() { WorkQueue.monitor.lock({ $!cancelled }) }
method cancel() { WorkQueue.monitor.lock({ $!cancelled = True }) }
}
class FractalSet {
has Bool $.is-julia;
has Complex $.upper-right;
has Real $.delta;
has Int $.width;
has Int $.height;
has Complex $.c;
has @.rows;
has @.line-work-items;
has $.new-upper-right;
has $.draw-area;
method stop-work() {
for @.line-work-items { .cancel }
@.line-work-items = ();
}
method start-work() {
self.stop-work if @.line-work-items;
say "Upper: " ~ $.upper-right;
say "Delta: " ~ $.delta;
# make local copies of everything we need to avoid data races
my $julia-z0 = $.c;
my $is-julia = $.is-julia;
my $ur = $.upper-right;
my $width = $.width;
my $delta = $.delta;
my @rows = @.rows = ByteArray.new($width * 3) xx $.height;
my $y_;
loop ($y_ = 0; $y_ < $.height; $y_++) {
my $y = $y_;
sub row() {
my $row = @rows[$y];
my $counter = 0;
my $counter_end = $counter + 3 * $width;
my $c = $ur - $y * $delta * i;
while $counter < $counter_end {
my $value = $is-julia ?? julia($julia-z0, $c) !! mandel($c);
$row.Set($counter++, @red[$value]);
$row.Set($counter++, @green[$value]);
$row.Set($counter++, @blue[$value]);
$c += $delta;
}
}
sub done() {
Application.Invoke(-> $ , $ {
$.draw-area.QueueDrawArea(0, $y, $width, 1);
});
}
my $wi = WorkItem.new(run => &row, done-cb => &done);
WorkQueue.push($wi);
push @.line-work-items, $wi;
}
}
method resize($width, $height) {
self.stop-work;
$.delta *= $.height / $height;
$.width = $width;
$.height = $height;
self.start-work;
}
method xy-to-c($x, $y) {
$.upper-right + $x * $.delta - $y * $.delta * i;
}
method RememberNewUpperRight($x, $y) {
$.new-upper-right = self.xy-to-c($x, $y);
}
method ForgetNewUpperRight() {
$.new-upper-right = Complex;
}
method BuildWindow()
{
my $index = +@windows;
@windows.push(self);
self.start-work;
my $window = Window.new($.is-julia ?? "julia $index" !! "mandelbrot $index");
$window.Resize($.width, $.height); # TODO: resize at runtime NYI
my $event-box = GtkEventBox.new;
$event-box.SetData("Id", SystemIntPtr.new($index));
$event-box.add_ButtonPressEvent(&ButtonPressEvent);
$event-box.add_ButtonReleaseEvent(&ButtonReleaseEvent);
my $drawingarea = $.draw-area = GtkDrawingArea.new;
$drawingarea.SetData("Id", SystemIntPtr.new($index));
$drawingarea.add_ExposeEvent(&ExposeEvent);
$window.add_DeleteEvent(&DeleteEvent);
$event-box.Add($drawingarea);
$window.Add($event-box);
$window.ShowAll;
}
}
Application.Init;
GdkRgb.Init;
FractalSet.new(is-julia => False,
upper-right => $upper-right,
delta => ($lower-left.re - $upper-right.re) / $size,
width => $size,
height => $size).BuildWindow;
Application.Run; # end of main program, it's all over when this returns
sub ButtonPressEvent($obj, $args) { #OK not used
my $index = $obj.GetData("Id").ToInt32();
my $set = @windows[$index];
given $args.Event.Button {
when 1 {
$set.RememberNewUpperRight($args.Event.X, $args.Event.Y);
}
}
}
sub ButtonReleaseEvent($obj, $args) { #OK not used
my $index = $obj.GetData("Id").ToInt32();
my $set = @windows[$index];
given $args.Event.Button {
when 1 {
if $set.new-upper-right {
# my $upper-right = -2 + (5/4)i;
# my $lower-left = 1/2 - (5/4)i;
my $c1 = $set.new-upper-right;
my $c2 = $set.xy-to-c($args.Event.X, $args.Event.Y);
my $upper-right = ($c1.re min $c2.re) + ($c1.im max $c2.im)i;
my $lower-left = ($c1.re max $c2.re) + ($c1.im min $c2.im)i;
my $height-ratio = ($upper-right.im - $lower-left.im) / ($lower-left.re - $upper-right.re);
FractalSet.new(is-julia => False,
upper-right => $upper-right,
delta => ($lower-left.re - $upper-right.re) / $size,
width => ($size / $height-ratio).Int,
height => $size).BuildWindow;
}
}
when 3 {
FractalSet.new(is-julia => True,
upper-right => -5/4 + (5/4)i,
delta => (5 / 2) / $size,
width => $size,
height => $size,
c => $set.xy-to-c($args.Event.X, $args.Event.Y)).BuildWindow;
}
}
$set.ForgetNewUpperRight;
}
sub DeleteEvent($obj, $args) { #OK not used
Application.Quit;
};
sub ExposeEvent($obj, $args)
{
my $index = $obj.GetData("Id").ToInt32();
my $set = @windows[$index];
my $window = $obj.GdkWindow;
my $windowX=0; my $windowY=0; my $windowWidth=0; my $windowHeight=0; my $windowDepth=0;
$window.GetGeometry($windowX, $windowY, $windowWidth, $windowHeight, $windowDepth);
if $windowHeight != $set.height || $windowWidth != $set.width {
$set.resize($windowWidth, $windowHeight);
}
my $gc = GdkGC.new($window);
my $y0 = $args.Event.Area.Y - $windowY;
for $y0..^($y0+$args.Event.Area.Height) -> $y {
if $y < $set.line-work-items && $set.line-work-items[$y].is-done {
$window.DrawRgbImage($gc, $windowX, $windowY+$y, $windowWidth, 1,
GdkRgbDither.Normal, $set.rows[$y], $windowWidth * 3);
}
else {
$window.DrawRectangle($gc, True, $windowX, $windowY+$y,
$windowWidth, 1);
}
}
};
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 julia(Complex $c, Complex $z0) {
my $z = $z0;
my $i;
loop ($i = 0; $i < $max_iterations; $i++) {
if $z.abs > 2 {
return $i + 1;
}
$z = $z * $z + $c;
}
return 0;
}
Jump to Line
Something went wrong with that request. Please try again.