Some things just a little bit off.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1212198 use warnings; use strict; use Tk; my $top = new MainWindow; my $canv; # offset use to decide the square around a given point my $offset = 4; my $num_of_points = 4; my @points; # ([x1,y1],[x2,y2]...) my @tk_points; # (tk-canvas,..) $top->Label(-text => "How many points?")->pack(-side => 'left'); $top->Entry(-width => 3,-borderwidth => 4, -textvariable => \$num_of_p +oints)->pack(-side => 'left'); $top->Button( -padx=> 5, -text => "draw", -borderwidth => 4, -command => sub{&draw($num_of_points)})->pack(-side => 'left') +; $top->Button( -padx=> 5, -text => "clear", -borderwidth => 4, -command => sub{@points = (); map{ $canv->delete($_) }@tk_points; })->pack(-side => 'left'); # new toplevel for the circle $canv=$top->Toplevel(-title=>'Points in a cirlce')->Canvas(-background +=>'gray', -width => 610, -height => 610)->pack; # the big cirlce my $circle = $canv->createOval(10,10,600,600, -fill => 'red', -outline => 'yellow', -width => 5, -tags =>['circle'], -stipple => 'gray12', ); # draw the center at 305 305 fat_dot(305,305); MainLoop; sub fat_dot { # center of the new fat dot my ($x,$y) = @_; # canvas and top left and bottom right coords of the # rectangle where the cirlce will be draw my ($x1,$y1,$x2,$y2) = ($x-$offset,$y-$offset,$x+$offset,$y+$offset) +; my $dot = $canv->createOval($x1,$y1,$x2,$y2,-fill => 'black'); return $dot; } sub draw{ my $num = shift; print "received $num as num of points\n"; # sin and cos they think in rad not in degrees!! my $ang = 3.141592653589793238462643383279 * 2 / $num; print "angle: $ang radiants\n"; my $cur = 0; for (1..$num){ my $x = 305 + int(295*sin($cur)); my $y = 5 + 300 - int (295*cos($cur)); print "at $cur radiants point at: $x $y\n"; my $dot = fat_dot($x,$y); $cur+=$ang; push @points,[$x,$y]; push @tk_points,$dot; } }

In reply to Re: Tk - points in a circle by tybalt89
in thread Tk - points in a circle by Discipulus

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.