我正在尝试使用Math::Geometry::Planar中的GpcClip()
函数来查找两个多边形的交集。我使用Math::Geometry::Planar->new();
构建了两个多边形,但在GpcClip()
中使用它时出现以下错误:
在gpc_polygon_clip的参数2中输入错误。预计_p_gpc_polygon位于c:/strawberry/perl/site/lib/math/geometry/planar.pm第2028行
如何将Math::Geometry::Planar->new()
返回的对象转换为GPC多边形?
答案 0 :(得分:1)
根据documentation,您可以使用convert2gpc
方法:
<强> $ polygon-&GT; convert2gpc; 强>
将多边形/轮廓转换为gpc结构并返回生成的gpc结构
示例:
use strict;
use warnings 'all';
use Math::Geometry::Planar;
my $outer = Math::Geometry::Planar->new;
my $inner = Math::Geometry::Planar->new;
$outer->points([[0, 0], [0, 3], [3, 3], [3, 0]]);
$inner->points([[1, 1], [1, 2], [2, 2], [2, 1]]);
my $diff = GpcClip('DIFFERENCE', $outer->convert2gpc, $inner->convert2gpc);
答案 1 :(得分:-1)
use strict;
use warnings 'all';
use Data::Dumper;
use Math::Geometry::Planar;
my $outer = Math::Geometry::Planar->new;
my $inner = Math::Geometry::Planar->new;
$outer->points([[0, 0], [0, 3], [3, 3], [3, 0],[0,0]]);
$inner->points([[2, 0], [2, 2], [4, 2], [4, 0],[2,0]]);
my $diff = GpcClip('INTERSECTION', $outer->convert2gpc, $inner->convert2gpc);
#first polygon rep the outer poly, the rest of them are holes
my @pgons = Gpc2Polygons($diff);
#since here we don't have holes, only the first one is a valid polygon
print Dumper($pgons[0]->polygons->[0]);
#convert the points into Planar Polygon
my $result = Math::Geometry::Planar->new;
$result->points($pgons[0]->polygons->[0]);
print Dumper($result);
感谢@ThisSuitlsBlackNot提供的帮助。顺便说一句,你是否有任何想法在多边形内找到一个随机点,这个多边形没有洞。 再次感谢