1

I want print out the intersection of two polygons. but when there is no intersection of two polygons, how can I get to know to avoid print it? Because if no intersection, I can't call $pgons[0]->polygons, it gives me an error.
(no holes in all polygons)
Thanks!

for my $x(0..$#polygon){
    for my $y(0..$#polygon){
        if ($x != $y){
            my $it = GpcClip('INTERSECTION', $polygon[0]->convert2gpc, $polygon[1]->convert2gpc);
            print FO "$x  ==  $y \n";
            my @pgons = Gpc2Polygons($it);  
            #since here we don't have holes, only the first one is a valid polygon 
            if(@pgons){
                print FO Dumper($pgons[0]->polygons->[0]);
                print "\n";
            }    
        }
    }
}
s.pan
  • 13
  • 4
  • I've no idea what you're talking about (not your fault, only my ignorance) but when you want to address ThisSuitIsBlackNot you need to omit the blank between the `@` and his name, like @ThisSuitIsBlackNot. Plus: He'll _only_ be noticed [if he already participates in the conversation](http://stackoverflow.com/editing-help#comment-formatting) (which he currently does not). – PerlDuck May 13 '16 at 19:59
  • @Perl Dog thanks. I mean how to determine whether there is real polygon in "@pgons". if there is no polygon, $pgons[0]->polygons will cause error. – s.pan May 13 '16 at 20:30
  • 3
    `if (@pgons && $pgons[0] && $pgons[0]->polygons) { ... }` – mob May 13 '16 at 20:53
  • I'm so sorry but you misunderstood me: _I_ cannot help you because I don't know anyhing about Math::Planar::GPC. 2nd: This is not like twitter. When you type @SomeUser, then SomeUser will _only_ be alerted if he already has answered/commented or somehow interacted with _this particular question_. ThisSuit did not (so far), so he will _not_ be notified. You cannot 'call' a particular person for help by this means. – PerlDuck May 13 '16 at 20:54
  • 2
    People may not be familiar with the module you are using, but if you give enough code to reproduce your problem i am sure people will help. you give no actual error. you dont show the input data so someone can reproduce your error and help you resolve it. – Chris Doyle May 13 '16 at 21:59
  • @Chris Doyle thank you for your advice. will do better next time. – s.pan May 16 '16 at 14:09
  • Thank @mob , it is a good one – s.pan May 16 '16 at 14:12

1 Answers1

1

It seems like Gpc2Polygons returns an empty array when no intersection was found. So to determine if the intersection is nonempty you could check if the length of the returned array is greater than zero. For example:

use feature qw(say);
use strict;
use warnings;

use Math::Geometry::Planar;

my $p1 = Math::Geometry::Planar->new;
my $p2 = Math::Geometry::Planar->new;

$p1->points([[0, 0], [0, 2], [2, 2], [2, 0]]);
for my $pos (1, 1.5, 2) {
    say "pos = $pos";
    $p2->points([[$pos, 0], [$pos, 2], [$pos + 2, 2], [$pos + 2, 0]]);
    my $intersect = GpcClip( 'INTERSECTION', $p1->convert2gpc, $p2->convert2gpc );
    my @pgons = Gpc2Polygons( $intersect );  
    if ( @pgons > 0 ) {
        say "  Found intersection";
    }
    else {
        say "  No intersection";
    }
}

The output is:

pos = 1
  Found intersection
pos = 1.5
  Found intersection
pos = 2
  No intersection
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • all right! Thank you Hakon. I just realized that the size of @pgons can be used for this purpose after I post this question. I use if $#pgons >= 0 . – s.pan May 16 '16 at 14:07