Tk Mandelbrot - click on image to center and zoom in... highly inefficient...

Takes a numeric parameter, which sets the width and height, in pixels, of the canvas. Defaults to 100 if no valid argument - personally, I wouldn't try going higher than, say, 500...

<update>Now you can zoom out by right-clicking, and I've changed the colour-scheme...</update>

use strict; use Tk; my $debug = 0; my $c_size = ($ARGV[0] =~ /^\d+$/ && $ARGV[0]>0) ? $ARGV[0] : 100; my $scale = my $oldscale = 1; my ($lasti, $lastr)=(0,-0.5); my $first = 1; my $mw = MainWindow -> new; my $canvas = $mw -> Canvas(-height=>$c_size, -width=>$c_size) -> pack; $canvas -> Tk::bind('all', '<ButtonPress-1>' => \&MakeMan0); $canvas -> Tk::bind('all', '<ButtonPress-3>' => \&MakeMan1); MakeMan(0); MainLoop; sub MakeMan0(){ print "here0\n" if $debug; MakeMan(0); } sub MakeMan1(){ print "here1\n" if $debug; MakeMan(1); } sub MakeMan(){ $canvas -> delete('all'); my ($e, $canv_x, $canv_y)=(0,$c_size/2,$c_size/2); if(!$first){ $e = $canvas -> XEvent; $canv_x = $e->x; $canv_y = $e->y; } print "$_[0] : $scale : $oldscale\n" if $debug; $oldscale = $scale; my $px_old = (4/$oldscale)/$c_size; $scale = $scale * ($_[0] ? 0.5 : 2); $scale = 1 if $first; my $px_new = (4/$scale)/$c_size; print "$_[0] : $scale : $oldscale\n" if $debug; $first = 0; my $i = $lasti = $lasti + ($canv_y - ($c_size/2))*$px_old; # centre $i = $i - (($c_size/2)*$px_new); # left edge my $rconst = $lastr = $lastr + ($canv_x - ($c_size/2))*$px_old; # ce +ntre $rconst = $rconst - (($c_size/2)*$px_new); # top edge my $icount = 1; while($icount <= $c_size){ my $r = $rconst; my $rcount = 1; while($rcount <= $c_size){ my $iter = ($c_size/10)*($scale**0.5); my $tr = $r; my $ti = $i; my $rgb_out = '#000000'; my $red = 20; my $green = 20; my $blue = 20; for my $n(1..$iter){ ($tr, $ti) = (($tr ** 2) - ($ti ** 2) + ($r), (2 * $tr * $ti) ++ ($i)); $red += int(130/$iter); $green += int(130/$iter); $blue +=int(235/$iter); if((($tr * $tr) + ($ti * $ti)) > 4){ $rgb_out = '#' . (sprintf("%2.2X",$red)) . sprintf("%2.2X",$ +green) . (sprintf("%2.2X",$blue)); last; } } $canvas -> createLine($rcount, $icount, $rcount+1, $icount, -fil +l => $rgb_out, -width => 1); $rcount ++; $r += ((4/$scale)/$c_size); } $icount ++; $i += ((4/$scale)/$c_size); } }

In reply to Tk Mandelbrot Fractal by Melly

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.