2016-05-11 28 views
1

我正在嘗試使用Math::Geometry::Planar中的GpcClip()函數來查找兩個多邊形的交點。我內置兩個多邊形使用Math::Geometry::Planar->new();但我得到以下錯誤,當我在GpcClip()使用他們:在gpc_polygon_clip的論點2如何創建一個用於Math :: Geometry :: Planar :: GpcClip的GPC多邊形?

類型的錯誤。位於C預計_p_gpc_polygon:/strawberry/perl/site/lib/math/geometry/planar.pm線2028

我如何可以轉換Math::Geometry::Planar->new()返回的對象爲GPC多邊形?

+2

我猜['$ polygon-> convert2gpc'(https://metacpan.org/pod/Math::Geometry::Planar#polygon-convert2gpc)...... – ThisSuitIsBlackNot

+0

哦,是的,謝謝您!它是如此明顯。我是這個模塊的新手.. –

回答

1

按照documentation,您可以使用convert2gpc方法:

$ polygon-> 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); 
+0

謝謝!我的項目嘗試在兩個多邊形的交點內獲得一個隨機點。我已經創建了平面多邊形,現在嘗試獲得交叉點聚合,並找到一個隨機點,如果交點可用。在GpcClip之後,$ diff是gpc poly,當我使用Gpc2Polygons時,我無法獲取點數據。你能幫我嗎? –

+0

首先,在調用'GpcClip'時將'DIFFERENCE'改爲'INTERSECTION'。 'Gpc2Polygons'返回一個多邊形數組,其中第一個元素表示外部形狀,其餘元素表示孔。所以你需要檢查你的點是否在第一個多邊形內,但不是在任何一個孔內。你可以使用['isinside'](https://metacpan.org/pod/Math::Geometry::Planar#polygon-isinside-point)方法。 – ThisSuitIsBlackNot

+0

@ThisSuitsBlack不是因爲我只需要交叉口,我的情況下肯定沒有洞。我只是修改了你的代碼,使用Gpc2Polygons後仍然沒有任何價值。我將在下面發佈代碼。 –

-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幫助。順便說一句,你有沒有想法在多邊形內找到一個隨機點,這個多邊形沒有洞。 再次感謝

+1

你發佈了這個答案,但我想你的意思是把它發佈爲[另一個問題](http://stackoverflow.com/questions/ask)。 – ThisSuitIsBlackNot

+0

@ ThisSuitIsBlackNot還有更多的問題,對於這個問題,它至少解決了.. :) –

相關問題