line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyrights 2004-2017 by [Mark Overmeer]. |
2
|
|
|
|
|
|
|
# For other contributors see ChangeLog. |
3
|
|
|
|
|
|
|
# See the manual pages for details on the licensing terms. |
4
|
|
|
|
|
|
|
# Pod stripped from pm file by OODoc 2.02. |
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
62764
|
use strict; |
|
6
|
|
|
|
|
16
|
|
|
6
|
|
|
|
|
174
|
|
7
|
6
|
|
|
6
|
|
36
|
use warnings; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
263
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Math::Polygon::Clip; |
10
|
6
|
|
|
6
|
|
37
|
use vars '$VERSION'; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
341
|
|
11
|
|
|
|
|
|
|
$VERSION = '1.06'; |
12
|
|
|
|
|
|
|
|
13
|
6
|
|
|
6
|
|
35
|
use base 'Exporter'; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
625
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our @EXPORT = qw/ |
16
|
|
|
|
|
|
|
polygon_line_clip |
17
|
|
|
|
|
|
|
polygon_fill_clip1 |
18
|
|
|
|
|
|
|
/; |
19
|
|
|
|
|
|
|
|
20
|
6
|
|
|
6
|
|
1209
|
use Math::Polygon::Calc; |
|
6
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
555
|
|
21
|
6
|
|
|
6
|
|
40
|
use List::Util qw/min max/; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
16864
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _inside($$); |
24
|
|
|
|
|
|
|
sub _cross($$$); |
25
|
|
|
|
|
|
|
sub _cross_inside($$$); |
26
|
|
|
|
|
|
|
sub _cross_x($$$); |
27
|
|
|
|
|
|
|
sub _cross_y($$$); |
28
|
|
|
|
|
|
|
sub _remove_doubles(@); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub polygon_fill_clip1($@) |
32
|
2
|
|
|
2
|
1
|
14
|
{ my $bbox = shift; |
33
|
2
|
|
|
|
|
5
|
my ($xmin, $ymin, $xmax, $ymax) = @$bbox; |
34
|
2
|
50
|
|
|
|
7
|
@_ or return (); # empty list of points |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Collect all crosspoints with axes, plus the original points |
37
|
2
|
|
|
|
|
3
|
my $next = shift; |
38
|
2
|
|
|
|
|
4
|
my @poly = $next; |
39
|
2
|
|
|
|
|
7
|
while(@_) |
40
|
8
|
|
|
|
|
15
|
{ $next = shift; |
41
|
8
|
|
|
|
|
20
|
push @poly, _cross($bbox, $poly[-1], $next), $next; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# crop them to the borders: outside is projected on the sides |
45
|
2
|
|
|
|
|
5
|
my @cropped; |
46
|
2
|
|
|
|
|
5
|
foreach (@poly) |
47
|
14
|
|
|
|
|
21
|
{ my ($x,$y) = @$_; |
48
|
14
|
50
|
|
|
|
34
|
$x = $xmin if $x < $xmin; |
49
|
14
|
100
|
|
|
|
30
|
$x = $xmax if $x > $xmax; |
50
|
14
|
50
|
|
|
|
27
|
$y = $ymin if $y < $ymin; |
51
|
14
|
100
|
|
|
|
32
|
$y = $ymax if $y > $ymax; |
52
|
14
|
|
|
|
|
32
|
push @cropped, [$x, $y]; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
13
|
polygon_beautify {despike => 1}, @cropped; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub polygon_line_clip($@) |
60
|
11
|
|
|
11
|
1
|
56467
|
{ my $bbox = shift; |
61
|
11
|
|
|
|
|
32
|
my ($xmin, $ymin, $xmax, $ymax) = @$bbox; |
62
|
|
|
|
|
|
|
|
63
|
11
|
|
|
|
|
21
|
my @frags; |
64
|
11
|
|
|
|
|
19
|
my $from = shift; |
65
|
11
|
|
|
|
|
30
|
my $fromin = _inside $bbox, $from; |
66
|
11
|
100
|
|
|
|
41
|
push @frags, [ $from ] if $fromin; |
67
|
|
|
|
|
|
|
|
68
|
11
|
|
|
|
|
33
|
while(@_) |
69
|
65
|
|
|
|
|
118
|
{ my $next = shift; |
70
|
65
|
|
|
|
|
121
|
my $nextin = _inside $bbox, $next; |
71
|
|
|
|
|
|
|
|
72
|
65
|
100
|
100
|
|
|
316
|
if($fromin && $nextin) # stay within |
|
|
100
|
66
|
|
|
|
|
|
|
100
|
|
|
|
|
|
73
|
20
|
|
|
|
|
40
|
{ push @{$frags[-1]}, $next; |
|
20
|
|
|
|
|
37
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
elsif($fromin && !$nextin) # leaving |
76
|
12
|
|
|
|
|
19
|
{ push @{$frags[-1]}, _cross_inside $bbox, $from, $next; |
|
12
|
|
|
|
|
30
|
|
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
elsif($nextin) # entering |
79
|
12
|
|
|
|
|
39
|
{ my @cross = _cross_inside $bbox, $from, $next; |
80
|
12
|
|
|
|
|
36
|
push @frags, [ @cross, $next ]; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
else # pass thru bbox? |
83
|
21
|
|
|
|
|
44
|
{ my @cross = _cross_inside $bbox, $from, $next; |
84
|
21
|
100
|
|
|
|
51
|
push @frags, \@cross if @cross; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
65
|
|
|
|
|
217
|
($from, $fromin) = ($next, $nextin); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Glue last to first? |
91
|
11
|
100
|
100
|
|
|
64
|
if( @frags >= 2 |
|
|
|
66
|
|
|
|
|
92
|
|
|
|
|
|
|
&& $frags[0][0][0] == $frags[-1][-1][0] # X |
93
|
|
|
|
|
|
|
&& $frags[0][0][1] == $frags[-1][-1][1] # Y |
94
|
|
|
|
|
|
|
) |
95
|
4
|
|
|
|
|
11
|
{ my $last = pop @frags; |
96
|
4
|
|
|
|
|
8
|
pop @$last; |
97
|
4
|
|
|
|
|
7
|
unshift @{$frags[0]}, @$last; |
|
4
|
|
|
|
|
17
|
|
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
11
|
|
|
|
|
42
|
@frags; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# |
104
|
|
|
|
|
|
|
### Some helper functions |
105
|
|
|
|
|
|
|
# |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _inside($$) |
108
|
101
|
|
|
101
|
|
220
|
{ my ($bbox, $point) = @_; |
109
|
|
|
|
|
|
|
|
110
|
101
|
100
|
100
|
|
|
805
|
$bbox->[0] <= $point->[0]+0.00001 |
|
|
|
100
|
|
|
|
|
111
|
|
|
|
|
|
|
&& $point->[0] <= $bbox->[2]+0.00001 # X |
112
|
|
|
|
|
|
|
&& $bbox->[1] <= $point->[1]+0.00001 |
113
|
|
|
|
|
|
|
&& $point->[1] <= $bbox->[3]+0.00001; # Y |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _sector($$) # left-top 678,345,012 right-bottom |
117
|
0
|
|
|
0
|
|
0
|
{ my ($bbox, $point) = @_; |
118
|
0
|
0
|
|
|
|
0
|
my $xsector = $point->[0] < $bbox->[0] ? 0 |
|
|
0
|
|
|
|
|
|
119
|
|
|
|
|
|
|
: $point->[0] < $bbox->[2] ? 1 |
120
|
|
|
|
|
|
|
: 2; |
121
|
0
|
0
|
|
|
|
0
|
my $ysector = $point->[1] < $bbox->[1] ? 0 |
|
|
0
|
|
|
|
|
|
122
|
|
|
|
|
|
|
: $point->[1] < $bbox->[3] ? 1 |
123
|
|
|
|
|
|
|
: 2; |
124
|
0
|
|
|
|
|
0
|
$ysector * 3 + $xsector; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub _cross($$$) |
128
|
61
|
|
|
61
|
|
20286
|
{ my ($bbox, $from, $to) = @_; |
129
|
61
|
|
|
|
|
132
|
my ($xmin, $ymin, $xmax, $ymax) = @$bbox; |
130
|
|
|
|
|
|
|
|
131
|
61
|
|
|
|
|
128
|
my @cross = ( _cross_x($xmin, $from, $to) |
132
|
|
|
|
|
|
|
, _cross_x($xmax, $from, $to) |
133
|
|
|
|
|
|
|
, _cross_y($ymin, $from, $to) |
134
|
|
|
|
|
|
|
, _cross_y($ymax, $from, $to) |
135
|
|
|
|
|
|
|
); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# order the results |
138
|
8
|
|
|
|
|
38
|
$from->[0] < $to->[0] ? sort({$a->[0] <=> $b->[0]} @cross) |
139
|
8
|
|
|
|
|
30
|
: $from->[0] > $to->[0] ? sort({$b->[0] <=> $a->[0]} @cross) |
140
|
0
|
|
|
|
|
0
|
: $from->[1] < $to->[1] ? sort({$a->[1] <=> $b->[1]} @cross) |
141
|
61
|
100
|
|
|
|
313
|
: sort({$b->[1] <=> $a->[1]} @cross); |
|
0
|
100
|
|
|
|
0
|
|
|
|
100
|
|
|
|
|
|
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub _cross_inside($$$) |
145
|
45
|
|
|
45
|
|
80
|
{ my ($bbox, $from, $to) = @_; |
146
|
45
|
|
|
|
|
94
|
grep { _inside($bbox, $_) } _cross($bbox, $from, $to); |
|
25
|
|
|
|
|
58
|
|
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub _remove_doubles(@) |
150
|
0
|
0
|
|
0
|
|
0
|
{ my $this = shift or return (); |
151
|
0
|
|
|
|
|
0
|
my @ret = $this; |
152
|
0
|
|
|
|
|
0
|
while(@_) |
153
|
0
|
|
|
|
|
0
|
{ my $this = shift; |
154
|
0
|
0
|
0
|
|
|
0
|
next if $this->[0]==$ret[-1][0] && $this->[1]==$ret[-1][1]; |
155
|
0
|
|
|
|
|
0
|
push @ret, $this; |
156
|
|
|
|
|
|
|
} |
157
|
0
|
|
|
|
|
0
|
@ret; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub _cross_x($$$) |
161
|
126
|
|
|
126
|
|
4619
|
{ my ($x, $from, $to) = @_; |
162
|
126
|
|
|
|
|
207
|
my ($fx, $fy) = @$from; |
163
|
126
|
|
|
|
|
213
|
my ($tx, $ty) = @$to; |
164
|
126
|
100
|
100
|
|
|
794
|
return () unless $fx < $x && $x < $tx || $tx < $x && $x < $fx; |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
165
|
28
|
|
|
|
|
79
|
my $y = $fy + ($x - $fx)/($tx - $fx) * ($ty - $fy); |
166
|
|
|
|
|
|
|
#warn "X: $x,$y <-- $fx,$fy $tx,$ty\n"; |
167
|
28
|
50
|
66
|
|
|
205
|
(($fy <= $y && $y <= $ty) || ($ty <= $y && $y <= $fy)) ? [$x,$y] : (); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub _cross_y($$$) |
171
|
126
|
|
|
126
|
|
5763
|
{ my ($y, $from, $to) = @_; |
172
|
126
|
|
|
|
|
234
|
my ($fx, $fy) = @$from; |
173
|
126
|
|
|
|
|
230
|
my ($tx, $ty) = @$to; |
174
|
126
|
100
|
100
|
|
|
755
|
return () unless $fy < $y && $y < $ty || $ty < $y && $y < $fy; |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
175
|
27
|
|
|
|
|
85
|
my $x = $fx + ($y - $fy)/($ty - $fy) * ($tx - $fx); |
176
|
|
|
|
|
|
|
#warn "Y: $x,$y <-- $fx,$fy $tx,$ty\n"; |
177
|
27
|
50
|
66
|
|
|
212
|
(($fx <= $x && $x <= $tx) || ($tx <= $x && $x <= $fx)) ? [$x,$y] : (); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
1; |