2011-10-04 20:27:45 +00:00
|
|
|
use Test::More;
|
2011-10-10 09:00:19 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2012-12-27 13:16:50 +00:00
|
|
|
plan tests => 3;
|
2011-10-04 20:27:45 +00:00
|
|
|
|
|
|
|
use Math::Clipper ':all';
|
|
|
|
|
|
|
|
|
2012-12-27 13:16:50 +00:00
|
|
|
{
|
|
|
|
my $square = [ # ccw
|
|
|
|
[10, 10],
|
|
|
|
[20, 10],
|
|
|
|
[20, 20],
|
|
|
|
[10, 20],
|
|
|
|
];
|
|
|
|
my $hole_in_square = [ # cw
|
|
|
|
[14, 14],
|
|
|
|
[14, 16],
|
|
|
|
[16, 16],
|
|
|
|
[16, 14],
|
|
|
|
];
|
|
|
|
my $square2 = [ # ccw
|
|
|
|
[5, 12],
|
|
|
|
[25, 12],
|
|
|
|
[25, 18],
|
|
|
|
[5, 18],
|
|
|
|
];
|
|
|
|
my $clipper = Math::Clipper->new;
|
|
|
|
$clipper->add_subject_polygons([ $square, $hole_in_square ]);
|
|
|
|
$clipper->add_clip_polygons([ $square2 ]);
|
|
|
|
my $intersection = $clipper->ex_execute(CT_INTERSECTION, PFT_NONZERO, PFT_NONZERO);
|
2013-05-11 07:19:23 +00:00
|
|
|
|
2012-12-27 13:16:50 +00:00
|
|
|
is_deeply $intersection, [
|
|
|
|
{
|
|
|
|
holes => [
|
|
|
|
[
|
2013-05-11 07:19:23 +00:00
|
|
|
[14, 14],
|
2012-12-27 13:16:50 +00:00
|
|
|
[14, 16],
|
|
|
|
[16, 16],
|
|
|
|
[16, 14],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
outer => [
|
|
|
|
[20, 12],
|
|
|
|
[20, 18],
|
2013-05-11 07:19:23 +00:00
|
|
|
[10, 18],
|
|
|
|
[10, 12],
|
2012-12-27 13:16:50 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
], 'hole is preserved after intersection';
|
|
|
|
}
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2012-12-27 13:16:50 +00:00
|
|
|
#==========================================================
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2012-12-27 13:16:50 +00:00
|
|
|
{
|
|
|
|
my $contour1 = [ [0,0], [40,0], [40,40], [0,40] ]; # ccw
|
|
|
|
my $contour2 = [ [10,10], [30,10], [30,30], [10,30] ]; # ccw
|
|
|
|
my $hole = [ [15,15], [15,25], [25,25], [25,15] ]; # cw
|
|
|
|
|
|
|
|
my $clipper = Math::Clipper->new;
|
|
|
|
$clipper->add_subject_polygons([ $contour1, $contour2, $hole ]);
|
|
|
|
my $union = $clipper->ex_execute(CT_UNION, PFT_NONZERO, PFT_NONZERO);
|
2013-05-11 07:19:23 +00:00
|
|
|
|
|
|
|
is_deeply $union, [{ holes => [], outer => [ [40,0], [40,40], [0,40], [0,0] ] }],
|
2012-12-27 13:16:50 +00:00
|
|
|
'union of two ccw and one cw is a contour with no holes';
|
|
|
|
|
|
|
|
$clipper->clear;
|
|
|
|
$clipper->add_subject_polygons([ $contour1, $contour2 ]);
|
|
|
|
$clipper->add_clip_polygons([ $hole ]);
|
|
|
|
my $diff = $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO);
|
2013-05-11 07:19:23 +00:00
|
|
|
is_deeply $diff, [{ holes => [[ [15,15], [15,25], [25,25], [25,15] ]], outer => [ [40,0], [40,40], [0,40], [0,0] ] }],
|
2012-12-27 13:16:50 +00:00
|
|
|
'difference of a cw from two ccw is a contour with one hole';
|
|
|
|
}
|
2011-10-04 20:27:45 +00:00
|
|
|
|