Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

How do I get "coords" of multiple polygons with the same "tag"

by smh (Acolyte)
on May 24, 2012 at 05:11 UTC ( #972165=perlquestion: print w/replies, xml ) Need Help??
smh has asked for the wisdom of the Perl Monks concerning the following question:

I have a canvas where I draw multiple polygons which have the same "tag". I re-scale the canvas and would like to get the coordinate of the polygons. WHen I use the "coords" method, I get the coordinates for one of the instances (as described by the documentation).

I was wondering is there a way to get "coords" method to get the other polygons also.

I do not have code for this since I do not know how to do it.

Thanks in advace  

  • Comment on How do I get "coords" of multiple polygons with the same "tag"

Replies are listed 'Best First'.
Re: How do I get "coords" of multiple polygons with the same "tag"
by zentara (Archbishop) on May 24, 2012 at 10:53 UTC
    Here is a simple example of using find with tags. Hope it sheds some light for you. Canvas tags are a very powerful tool, and the secret to using the Canvas successfully.
    #!/usr/bin/perl -w use strict; use Tk; my ($x, $y); my $iconSize = 20; my ($width, $height) = (10, 10); my ($canvasWidth, $canvasHeight) = ($iconSize+$width*$iconSize, $iconSize+$height*$iconSize); my $MW = MainWindow->new; my $MF = $MW->Frame->pack; my $c = $MF->Canvas( -width => $canvasWidth, -height => $canvasHeight )->pack; for ($y = $iconSize; $y < $canvasHeight; $y+=$iconSize) { for ($x = $iconSize; $x < $canvasWidth; $x+=$iconSize) { my $rect = $c->createRectangle ($x, $y, $x+$iconSize, $y+$iconSize, -fill => '#AFAFAF', -activefill => '#CFCFCF', -tags =>['rect',"row.$y", "col.$x"] ); } } $c->bind('rect', '<Enter>', \&enter ); $c->bind("rect", "<Leave>", \&leave ); MainLoop; sub enter { my ($canv) = @_; my $id = $canv->find('withtag', 'current'); my @tags = $canv->gettags($id); # print "@tags\n"; # row.20 col.140 current my ($row) = ( grep /^row\d*/, @tags ); my ($col) = ( grep /^col\d*/, @tags ); my(@rows) = $canv->find('withtag', $row); my(@cols) = $canv->find('withtag', $col); foreach my $r(@rows){ $canv->itemconfigure($r, -fill=>'#CFCFCF') } foreach my $c(@cols){ $canv->itemconfigure($c, -fill=>'#CFCFCF') } } sub leave{ my ($canv) = @_; # print "leave\n"; $canv->itemconfigure('all', -fill=>'#AFAFAF') }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thanks a lot. You are right on, great hint. smh

Re: How do I get "coords" of multiple polygons with the same "tag"
by kcott (Chancellor) on May 24, 2012 at 06:13 UTC

    From your description, it sounds like you want the find() method. Take a look at Tk::Canvas; in particular, ITEM IDS AND TAGS and WIDGET METHODS. You'd use this something like the following:

    for my $tag ($canvas->find(...)) { my @coords = $canvas->coords($tag); # do something with @coords }

    -- Ken

Re: How do I get "coords" of multiple polygons with the same "tag"
by stevieb (Abbot) on May 24, 2012 at 05:49 UTC

    You can describe how to get something "(as described by the documentation).", yet you can't provide any code samples at all?

    Showing both input data and at least basic code would be beneficial.

      I am sorry, at the time I was working on extracting a reasonable code segment. The full code is too long and in development so it will not be very useful. I will post the segment that I have problem with soon and hopefully get a resolution to me question.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://972165]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2018-05-25 23:27 GMT
Find Nodes?
    Voting Booth?