Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: The horribly slow Julia set viewer with the extremely inefficient plotting method

by kikuchiyo (Monk)
on Dec 17, 2009 at 17:01 UTC ( #813212=note: print w/ replies, xml ) Need Help??


in reply to Re^2: The horribly slow Julia set viewer with the extremely inefficient plotting method
in thread The horribly slow Julia set viewer with the extremely inefficient plotting method

To illustrate the point, here is a version that does not use Goo::Canvas and rectangles at all, it builds up a string of rgb pixels instead and uses that to create a pixbuf, which in turn gets displayed on a DrawingArea.

This is actually fast enough to be usable, as the animation is quite smooth on my machine.

#!/usr/bin/perl use strict; use warnings; use Gtk2 -init; use Gtk2::Ex::Dialogs; use Gtk2::Gdk::Keysyms; use Glib ':constants'; #use Goo::Canvas; use Inline C => Config => CCFLAGS => '-O3 -msse3 -mfpmath=sse -march=c +ore2 -ffast-math'; use Inline C => 'DATA'; # setting up default sizes my $xbox = 360; my $ybox = 300; my $boxsize = 2; my $centerx = 0; my $centery = 0; my $wsize = 4; my $pmax = $centerx + $wsize / 2; my $pmin = $centerx - $wsize / 2; my $qmax = $centery + $wsize / 2 * $ybox/$xbox; my $qmin = $centery - $wsize / 2 * $ybox/$xbox; my $A = ($pmax-$pmin)/$xbox; my $C = ($qmax-$qmin)/$ybox; #print "$pmax, $pmin, $qmax, $qmin, $A, $C\n"; my $kmax=100; # setting up palette my @palette = map { pack("WWW", int(sqrt($_/$kmax)*256), int(($_/$kmax)**3*256), int(sin(3.14159*$_/$kmax)*256) ) } (0..$kmax); $palette[-1] = "\0\0\0"; # Create the main window my $win = new Gtk2::Window ( "toplevel" ); $win->signal_connect ("delete_event", sub { Gtk2->main_quit; }); #$win->signal_connect ("configure_event", \&win_expose); $win->set_title( "Julia set demo" ); $win->set_border_width (6); #$win->maximize; $win->set_resizable (0); $win->resize(700, 500); my $vbox = Gtk2::VBox->new (0, 6); $win->add ($vbox); my $da = Gtk2::DrawingArea->new; $da->set_size_request($xbox*$boxsize, $ybox*$boxsize); $vbox->pack_start($da, 1, 1, 0); $da->signal_connect (motion_notify_event => \&on_background_motion_not +ify); $da->set_events ([ @{ $da->get_events }, 'leave-notify-mask', 'pointer-motion-mask', 'pointer-motion-hint-mask', ]); #my $gc1 = Gtk2::Gdk::GC->new ($win); $win->show_all; main Gtk2; # replotting on mouse movement sub on_background_motion_notify { my ($da, $event) = @_; my (undef, $ex, $ey, $state) = $event->window->get_pointer; my $cy = $C*int(($ey)/$boxsize) + $qmin; my $cx = $A*int(($ex)/$boxsize) + $pmin; #print $ex," ,",$ey," ,$cx ,$cy"; my $data = ''; my $row = ''; foreach my $q (0..$ybox-1) { foreach my $p (0..$xbox-1) { $k = julia_iter($p, $q, $A, $C, $pmin, $qmin, $kmax, $cx, +$cy); $row .= $palette[$k] x $boxsize; } $data .= $row x $boxsize; $row = ''; } my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_data ( $data, 'rgb', FALSE, 8, $xbox*$boxsize, $ybox*$boxsize, 3*$xbox*$boxsize ); my $gc1 = Gtk2::Gdk::GC->new ($da->window); $da->window->draw_pixbuf($gc1, $pixbuf, 0, 0, 0, 0, $xbox*$boxsize, $ybox*$boxsize, 'none', 0, 0, ); return TRUE; } __DATA__ __C__ int julia_iter (int p, int q, double A, double C, double pmin, double +qmin, int kmax, double cx, double cy) { int k = 0; double x = A*p + pmin; double y = C*q + qmin; double x2 = x*x; double y2 = y*y; while (x2+y2<4 && k<kmax) { y=2*x*y+cy; x=x2-y2+cx; x2=x*x; y2=y*y; k++; } return k; }


Comment on Re^3: The horribly slow Julia set viewer with the extremely inefficient plotting method
Download Code
Re^4: The horribly slow Julia set viewer with the extremely inefficient plotting method
by zentara (Archbishop) on Dec 17, 2009 at 17:37 UTC
    To illustrate the point

    .....now you're taiking..... nice example


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://813212]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-11-23 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (132 votes), past polls