Debian Codegraph @

lib/Math/Polygon/Calc.pod

raw
1 =encoding utf8
2
3 =head1 NAME
4
5 Math::Polygon::Calc - Simple polygon calculations
6
7 =head1 INHERITANCE
8
9 Math::Polygon::Calc
10 is an Exporter
11
12 =head1 SYNOPSIS
13
14 my @poly = ( [1,2], [2,4], [5,7], [1, 2] );
15
16 my ($xmin, $ymin, $xmax, $ymax) = polygon_bbox @poly;
17
18 my $area = polygon_area @poly;
19 MY $L = polygon_perimeter @poly;
20 if(polygon_is_clockwise @poly) { ... };
21
22 my @rot = polygon_start_minxy @poly;
23
24 =head1 DESCRIPTION
25
26 This package contains a wide variaty of relatively easy polygon
27 calculations. More complex calculations are put in separate
28 packages.
29
30 =head1 FUNCTIONS
31
32 =over 4
33
34 =item B<polygon_area>(@points)
35
36 Returns the area enclosed by the polygon. The last point of the list
37 must be the same as the first to produce a correct result.
38
39 The algorithm was found at L<https://mathworld.wolfram.com/PolygonArea.html>,
40 and sounds:
41
42 A = abs( 1/2 * (x1y2-x2y1 + x2y3-x3y2 ...)
43
44 =item B<polygon_bbox>(@points)
45
46 Returns a list with four elements: (xmin, ymin, xmax, ymax), which describe
47 the bounding box of the polygon (all points of the polygon are within that
48 area.
49
50 =item B<polygon_beautify>( [\%options], @points )
51
52 Polygons, certainly after some computations, can have a lot of
53 horrible artifacts: points which are double, spikes, etc.
54 The optional HASH contains the C<%options>.
55
56 -Option --Default
57 remove_spikes <C<false>>
58
59 =over 2
60
61 =item remove_spikes => BOOLEAN
62
63 Spikes contain of three successive points, where the first is on the
64 line between the second and the third. The line goes from first to
65 second, but then back to get to the third point.
66
67 At the moment, only pure horizontal and pure vertical spikes are
68 removed.
69
70 =back
71
72 =item B<polygon_centroid>( [%options|\%options], @points )
73
74 Returns the centroid location of the polygon.
75
76 The last point of the list must be the same as the first (must be
77 'closed') to produce a correct result.
78
79 B<warning:> When the polygon is very flat, it will not produce a
80 stable result: minor changes in single coordinates will move the
81 centroid too far.
82
83 The algorithm was found at
84 L<https://en.wikipedia.org/wiki/Centroid#Of_a_polygon>
85
86 -Option --Default
87 is_large false
88
89 =over 2
90
91 =item is_large => BOOLEAN
92
93 When the polygon is small and far from the origin C<(0,0)> (as often
94 happens when processing geo coordinates), then rounding errors will have a
95 large impact on result of the algorithm. To avoid this, we will move the
96 poly first close to the origin, and move the calculated center point back.
97
98 This transform, which cost modest performance, can be disabled with
99 this option. The transformation will also not happen when the first
100 C<x> coordinate is an object, like Math::BigFloat.
101
102 =back
103
104 =item B<polygon_clockwise>(@points)
105
106 Be sure the polygon points are in clockwise order.
107
108 =item B<polygon_contains_point>($point, @points)
109
110 Returns C<true> if the point is inside the closed polygon. On an edge will
111 be flagged as 'inside'. But be warned of rounding issues, caused by
112 the floating-point calculations used by this algorithm.
113
114 =item B<polygon_counter_clockwise>(@points)
115
116 Be sure the polygon points are in counter-clockwise order.
117
118 =item B<polygon_distance>($point, @polygon)
119
120 [1.05] calculate the shortest distance between a point and any vertex of
121 a closed polygon.
122
123 =item B<polygon_equal>( \@points1, \@points2, [$tolerance] )
124
125 Compare two polygons, on the level of points. When the polygons are
126 the same but rotated, this will return C<false>. See L<polygon_same()|Math::Polygon::Calc/"FUNCTIONS">.
127
128 =item B<polygon_format>($format, @points)
129
130 [1.07] Map the C<$format> over all C<@points>, both the X and Y coordinate. This
131 is especially useful to reduce the number of digits in the stringification.
132 For instance, when you want reproducible results in regression scripts.
133
134 The format is anything supported by C<printf()>, for instance C<"%5.2f">. Or,
135 you can pass a code reference which accepts a single value.
136
137 =item B<polygon_is_clockwise>(@points)
138
139 Z<>
140
141 =item B<polygon_is_closed>(@points)
142
143 Z<>
144
145 =item B<polygon_perimeter>(@points)
146
147 The length of the line of the polygon. This can also be used to compute
148 the length of any line: of the last point is not equal to the first, then
149 a line is presumed; for a polygon they must match.
150
151 This is simply Pythagoras.
152
153 $l = sqrt((x1-x0)^2 + (y1-y0)^2) + sqrt((x2-x1)^2+(y2-y1)^2) + ...
154
155 =item B<polygon_same>( \@points1, \@points2, [$tolerance] )
156
157 [1.12] Compare two polygons, where the polygons may be rotated or mirrored
158 wrt each other. This is (much) slower than L<polygon_equal()|Math::Polygon::Calc/"FUNCTIONS">, but some
159 algorithms will cause un unpredictable rotation in the result.
160
161 =item B<polygon_start_minxy>(@points)
162
163 Returns the polygon, where the point which is closest to the left-bottom
164 corner of the bounding box is made first.
165
166 =item B<polygon_string>(@points)
167
168 Z<>
169
170 =back
171
172 =head1 DIAGNOSTICS
173
174 =over 4
175
176 =item Error: empty polygon is neither closed nor open
177
178 Cast by polygon_is_closed()
179
180 =item Error: polygon points on a line, so no centroid
181
182 Cast by polygon_centroid()
183
184 =back
185
186 =head1 SEE ALSO
187
188 This module is part of Math-Polygon version 2.00,
189 built on September 04, 2025. Website: F<http://perl.overmeer.net/CPAN/>
190
191 =head1 LICENSE
192
193 For contributors see file ChangeLog.
194
195 This software is copyright (c) 2004-2025 by Mark Overmeer.
196
197 This is free software; you can redistribute it and/or modify it under
198 the same terms as the Perl 5 programming language system itself.
199