in reply to Tk Zooming Fractal Problems
For instance, tag all your lines with 'fractal', after you build the fractal, get a bbox of the tag fractal, take the midpoint values, and store it. Then on each new fractal build, just repeat, take the new bbox, then compute how much you need to move it, to center it on the mouse event point.
I'm not doing the computations here, but this shows the basics.
#!/usr/bin/perl use warnings; use strict; use Tk; my $c_size = 100; my $scale = 1; my $centerx; my $centery; my $mw = MainWindow -> new; my $canvas = $mw -> Canvas(-height=>$c_size, -width=>$c_size) -> pack; $canvas -> Tk::bind('all', '<ButtonPress-1>' => [\&transit,Ev('x'), Ev +('y') ]); &MakeMan(); MainLoop; sub transit{ my ($canv, $x, $y) = @_; print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n" +; my $dx = $x - $centerx; my $dy = $y - $centery; $canvas->move('fractal',$dx,$dy); } sub MakeMan(){ my ($e, $canv_x, $canv_y)=(0,$c_size/2,$c_size/2); if($scale > 1){ $e = $canvas -> XEvent; $canv_x = $e->x; $canv_y = $e->y; } my $icount = 1; #problem here... my $i = -2/$scale + (($canv_y-($c_size/2)) / ($c_size/$scale));# + $ +lasti; my $rgb; while($icount <= $c_size){ #... and here... my $r = -2/$scale + (($canv_x-($c_size/2)) / ($c_size/$scale));# + + $lastr; my $rcount = 1; while($rcount <= $c_size){ my $tr = $r; my $ti = $i; $rgb = '#000000'; for my $n(1..15){ ($tr, $ti) = (($tr ** 2) - ($ti ** 2) + ($r), (2 * $tr * $ti) ++ ($i)); if((($tr * $tr) + ($ti * $ti)) > 4){ $rgb = '#ffffff'; last; } } $canvas -> createLine($rcount, $icount, $rcount+1, $icount, -fill => $rgb, -width => 1, -tags => ['fractal'] ); $rcount ++; $r += (4/$scale)/$c_size; } $icount ++; $i += (4/$scale)/$c_size; } $scale = $scale * 2; my ($x1,$y1,$x2,$y2) = $canvas->bbox('fractal'); $centerx = ($x1+$x2)/2; $centery = ($y1+$y2)/2; print "center-> $centerx $centery\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tk Zooming Fractal Problems
by Melly (Chaplain) on Dec 07, 2006 at 17:37 UTC |