0

I am trying to use the GpcClip() function from Math::Geometry::Planar to find the intersection of two polygons. I built two polygons by using Math::Geometry::Planar->new(); but I got the following error when I used them in GpcClip():

Type error in argument 2 of gpc_polygon_clip. Expected _p_gpc_polygon at c:/strawberry/perl/site/lib/math/geometry/planar.pm line 2028

How can I convert the object returned by Math::Geometry::Planar->new() into a GPC polygon?

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
s.pan
  • 13
  • 4

2 Answers2

1

According to the documentation, you can use the convert2gpc method:

$polygon->convert2gpc;

Converts a polygon/contour to a gpc structure and returns the resulting gpc structure

Example:

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);
Community
  • 1
  • 1
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
  • Thank you! My project is try to get a random point inside the intersection of two polygons. I have created Planar Polygons, and now is try to get the intersection poly, and find a random point inside if intersection available. After GpcClip, $diff is a gpc poly, when I use Gpc2Polygons, I can't get the point data. Can you help? – s.pan May 12 '16 at 17:43
  • First, change `DIFFERENCE` to `INTERSECTION` in the call to `GpcClip`. `Gpc2Polygons` returns an array of polygons where the first element of the array represents the outer shape and the rest of the elements represent holes. So you need to check that your point is inside the first polygon, but not inside any of the holes. You can use the [`isinside`](https://metacpan.org/pod/Math::Geometry::Planar#polygon-isinside-point) method for that. – ThisSuitIsBlackNot May 12 '16 at 18:00
  • @ThisSuitsBlackNot Cause I just need the intersection and for sure no hole in my case. I just modified your code little bit, still no value after using Gpc2Polygons. I will post code below. – s.pan May 12 '16 at 18:18
  • 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([[0, 2], [2, 2], [4, 2], [0, 4]]); my $diff = GpcClip('INTERSECTION', $outer->convert2gpc, $inner->convert2gpc); my @pgons = Gpc2Polygons($diff); my $result = Math::Geometry::Planar->new; $result = $pgons[0]; print $result; my $points = $result->points(); print $points; – s.pan May 12 '16 at 18:27
  • @s.pan `$result` is a Math::Geometry::Planar object and `$points` is an array reference. You can't just print objects and references with `print`, that will just output their locations in memory. You can use [Data::Dumper](http://perldoc.perl.org/Data/Dumper.html) to debug complex data structures. If you still can't figure it out after using Data::Dumper, I would post a new question; comments aren't very good for long bits of code. – ThisSuitIsBlackNot May 12 '16 at 18:28
-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);

Appreciate the help from @ThisSuitlsBlackNot . By the way, do you have any idea to find a random point inside a polygon, this polygon doesn't have hole. Thanks again

s.pan
  • 13
  • 4