Permalink
Browse files

Added a couple Clipper tests. #858

  • Loading branch information...
1 parent 0da71db commit 547e62d0a8b7c8717b9975f7b0eee3dd59d7c832 @alexrj committed Dec 27, 2012
Showing with 62 additions and 42 deletions.
  1. +62 −42 t/clipper.t
View
@@ -2,52 +2,72 @@ use Test::More;
use strict;
use warnings;
-plan tests => 1;
+plan tests => 3;
use Math::Clipper ':all';
-my $clipper = Math::Clipper->new;
-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 $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);
+ is_deeply $intersection, [
+ {
+ holes => [
+ [
+ [14, 16],
+ [16, 16],
+ [16, 14],
+ [14, 14],
+ ],
+ ],
+ outer => [
+ [10, 18],
+ [10, 12],
+ [20, 12],
+ [20, 18],
+ ],
+ },
+ ], 'hole is preserved after intersection';
+}
-my $square2 = [ # ccw
- [5, 12],
- [25, 12],
- [25, 18],
- [5, 18],
-];
+#==========================================================
-$clipper->add_subject_polygons([ $square, $hole_in_square ]);
-$clipper->add_clip_polygons([ $square2 ]);
-my $intersection = $clipper->ex_execute(CT_INTERSECTION, PFT_NONZERO, PFT_NONZERO);
+{
+ 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);
+ is_deeply $union, [{ holes => [], outer => [ [0,40], [0,0], [40,0], [40,40] ] }],
+ '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);
+ is_deeply $diff, [{ holes => [[ [15,25], [25,25], [25,15], [15,15] ]], outer => [ [0,40], [0,0], [40,0], [40,40] ] }],
+ 'difference of a cw from two ccw is a contour with one hole';
+}
-is_deeply $intersection, [
- {
- holes => [
- [
- [14, 16],
- [16, 16],
- [16, 14],
- [14, 14],
- ],
- ],
- outer => [
- [10, 18],
- [10, 12],
- [20, 12],
- [20, 18],
- ],
- },
-], 'hole is preserved after intersection';

0 comments on commit 547e62d

Please sign in to comment.