如何访问 Math::Planar::GPC 功能和特性?

how to access Math::Planar::GPC function and features?

我想打印出两个多边形的交集。但是当两个多边形没有交集时,我怎样才能知道避免打印它呢? 因为如果没有交点,我就不能调用 $pgons[0]->polygons,它会给我一个错误。
(所有多边形都没有洞)
谢谢!

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

似乎Gpc2Polygons returns 没有找到交集时是一个空数组。因此,要确定交集是否为空,您可以检查返回数组的长度是否大于零。例如:

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

输出为:

pos = 1
  Found intersection
pos = 1.5
  Found intersection
pos = 2
  No intersection