File: 003boolops.t

package info (click to toggle)
libmath-clipper-perl 1.23-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 484 kB
  • ctags: 1,094
  • sloc: cpp: 3,448; perl: 131; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 1,876 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use Math::Clipper ':all';
use Test::More tests=>7;

#my $ai = [
#[-900359890780731,536870912000000],
#[0,-1073741824000000],
#[900359890780731,536870912000000]
#];
#my $bi = [
#[-900359890780731,-536870912000000],
#[900359890780731,-536870912000000],
#[0,1073741824000000]
#];

my $ai = [
[-15 ,  20],
[  0 , -40],
[ 15 ,  20]
];
my $bi = [
[-15 , -20],
[ 15 , -20],
[  0 ,  40]
];

my $triarea=100;

my $clipper = Math::Clipper->new;
$clipper->use_full_coordinate_range(1);
$clipper->add_subject_polygon($ai);
$clipper->add_clip_polygon($bi);
my $result = $clipper->execute(CT_DIFFERENCE);
ok(
  scalar(@{$result})==3,
  'DIFFERENCE should give three polygons'
  );
ok(
  3*$triarea == area_sum($result) ,
  'DIFFERENCE areas are reasonable'
  );

$clipper->clear();
$clipper->add_subject_polygon($ai);
$clipper->add_clip_polygon($bi);
$result = $clipper->execute(CT_UNION);
ok(
  scalar(@{$result})==1,
  'UNION should give one polygon'
  );
ok(
  12*$triarea == area_sum($result) ,
  'UNION area is reasonable'
  );

$clipper->clear();
$clipper->add_subject_polygon($ai);
$clipper->add_clip_polygon($bi);
$result = $clipper->execute(CT_XOR);
# xor of test gives two polygons, each with two shared points between triangles, but that might
# not be reliable or desired. It's a Clipper issue though, and might change with new versions
# so don't want to count result polygons for xor
ok(
  6*$triarea == area_sum($result) ,  
  'XOR area is reasonable'
  );

$clipper->clear();
$clipper->add_subject_polygon($ai);
$clipper->add_clip_polygon($bi);
$result = $clipper->execute(CT_INTERSECTION);
ok(
  scalar(@{$result})==1,
  'INTERSECTION should give one polygon'
  );
ok(
  6*$triarea == area_sum($result) ,
  'INTERSECTION area is reasonable'
  );

sub area_sum {
    my $polys=shift;
    my $ret = 0;
    map {$ret+=Math::Clipper::area($_)} @{$polys};
    return $ret;
    }