Hello again nuns and monks,

yes i'm still here figthing against math: now I have a trigonometric question to ask.

In a Tk canvas I drow a circle, then I want to place a variable number of dots in the circumference, equally spaced in radiants (well.. I lost 3 hours before looking at sin to discover it accepts in radiants and not in degrees..).

In the below example I work with a canvas 610x610 and I create a circle with radius 300 to have same space around it.

Then I create some fat_dot aka little circles but I must have some offset misplaced and after some radiants my fat_dots are a bit outside of the circumference.

Where I'm failing?

Is my radiants computation right?

Notice that I need to store coordinates of all fat_dot and their ids to achieve other operations later on.

Where I need your precious advice is in the draw subroutine.

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+$offse +t); 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(300*sin($cur)); my $y = 10 + 300 - int (300*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; } }

Thanks in advance for looking.

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to 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.