Skip to content

Commit

Permalink
Store mandelbrot bitmaps once they have been constructed, so they onl…
Browse files Browse the repository at this point in the history
…y need be constructed once per setting.
  • Loading branch information
colomon committed Nov 25, 2011
1 parent 2528f02 commit feea17f
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions bin/gtk-mandelbrot.pl
Expand Up @@ -261,6 +261,8 @@ ($obj, $args)
Application.Quit;
};

my %bitmaps;

sub ExposeEvent($obj, $args)
{
$args; # suppress "declared but not used" "Potential difficulties"
Expand All @@ -271,30 +273,39 @@ ($obj, $args)
$window.GetGeometry($windowX, $windowY, $windowWidth, $windowHeight, $windowDepth);

my $delta = ($lower-left.re - $upper-right.re) / $windowWidth;

my $hash-tag = HashTag($upper-right, $delta, $windowX, $windowY, $windowWidth, $windowHeight);
unless %bitmaps{$hash-tag}:exists {
my $bytes = ByteArray.new($windowWidth * 3 * $windowHeight);

my $bytes = ByteArray.new($windowWidth * 3 * $windowHeight);

my $counter = 0;
my ($x, $y);
loop ($y = 0; $y < $windowHeight; $y++) {
my $c = $upper-right - $y * $delta * i;
loop ($x = 0; $x < $windowWidth; $x++) {
my $value = mandel($c);
$bytes.Set($counter++, @red[$value]);
$bytes.Set($counter++, @green[$value]);
$bytes.Set($counter++, @blue[$value]);
$c += $delta;
my $counter = 0;
my ($x, $y);
loop ($y = 0; $y < $windowHeight; $y++) {
my $c = $upper-right - $y * $delta * i;
loop ($x = 0; $x < $windowWidth; $x++) {
my $value = mandel($c);
$bytes.Set($counter++, @red[$value]);
$bytes.Set($counter++, @green[$value]);
$bytes.Set($counter++, @blue[$value]);
$c += $delta;
}
}

%bitmaps{$hash-tag} = $bytes;
}

my $gc = GdkGC.new($window);
$window.DrawRgbImage($gc, $windowX, $windowY, $windowWidth, $windowHeight,
GdkRgbDither.Normal, $bytes, $windowWidth * 3);
GdkRgbDither.Normal, %bitmaps{$hash-tag}, $windowWidth * 3);

my $elapsed = time - $start-time;
say "$elapsed seconds";
};

sub HashTag($upper-right, $delta, $windowX, $windowY, $windowWidth, $windowHeight) {
($upper-right, $delta, $windowX, $windowY, $windowWidth, $windowHeight).join("~");
}

sub mandel(Complex $c) {
my $z = 0i;
my $i;
Expand Down

0 comments on commit feea17f

Please sign in to comment.