Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Behold! This is a simple demo that plots a Julia set, the defining coordinate of which being taken from the mouse cursor position. This effect is achieved by defining 360×300 pieces of small rectangles on the canvas, then recalculating and changing the color of every rectangle on every motion notify event. This seems to be very slow (1-2 fps at best on my machine), hence the title.

The demo needs Gtk2 and Goo::Canvas to run.

#!/usr/bin/perl use strict; use warnings; use Gtk2 -init; use Gtk2::Ex::Dialogs; use Gtk2::Gdk::Keysyms; use Glib ':constants'; use Goo::Canvas; # 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 { '#'.sprintf("%02x", int(sqrt($_/$kmax)*256)). sprintf("%02x", int(($_/$kmax)**3*256)). sprintf("%02x", int(sin(3.14159*$_/$kmax)*256)) } +(0..$kmax-1); $palette[-1] = '#000000'; # 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); # Creating the canvas my $canvas = Goo::Canvas->new(); $canvas->set_size_request($xbox*$boxsize, $ybox*$boxsize); $canvas->set_bounds(0, 0, $xbox*$boxsize, $ybox*$boxsize); my $white = Gtk2::Gdk::Color->new (0xFFFF, 0xFFFF, 0xFFFF); $canvas->modify_base('normal', $white ); $vbox->pack_start($canvas, 1, 1, 0); my $root = $canvas->get_root_item(); my $g = Goo::Canvas::Group->new($root); my $rect = Goo::Canvas::Rect->new( $g, 0, 0, $xbox*$boxsize, $ybox*$boxsize, 'line-width' => 1, 'stroke-color' => 'white', #invisible on white bg 'fill-color' => 'white', # must be filled for mouse event sensit +ivity 'title' => 'bg_white_rect', ); # Creating the colored rectangles that make up the Julia set my @pixels; foreach my $x (0..$xbox-1) { foreach my $y (0..$ybox-1) { $pixels[$x][$y] = Goo::Canvas::Rect->new( $g, $x*$boxsize, $y*$boxsize, $boxsize, $boxsize, 'line-width' => 0, #'stroke-color' => 'white', 'fill-color' => 'white', #'title' => 'bg_white_rect', ); } } $g->signal_connect('motion-notify-event', \&on_background_motion_notif +y); $canvas->can_focus(TRUE); $canvas->grab_focus($root); $win->show_all; main Gtk2; # replotting on mouse movement sub on_background_motion_notify { my ($group, $item, $ev) = @_; my $cy = $C*int(($ev->y)/$boxsize) + $qmin; #/// my $cx = $A*int(($ev->x)/$boxsize) + $pmin; #print $ev->x," ,",$ev->y," ,$cx ,$cy\n"; my ($k, $x, $y, $x2, $y2); foreach my $p (0..$xbox-1) { foreach my $q (0..$ybox-1) { $k = 0; $x = $A*$p + $pmin; $y = $C*$q + $qmin; $x2 = $x*$x; $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++; } $pixels[$p][$q]->set( 'fill-color' => $palette[$k], ); } } return TRUE; }


In reply to The horribly slow Julia set viewer with the extremely inefficient plotting method by kikuchiyo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-20 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found