Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Tk - points in a circle

by tybalt89 (Monsignor)
on Apr 02, 2018 at 20:18 UTC ( [id://1212200]=note: print w/replies, xml ) Need Help??


in reply to Tk - points in a circle

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; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1212200]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found