| 1 |
=encoding utf8 |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Math::Polygon - Class for maintaining polygon data |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
my $poly = Math::Polygon->new( [1,2], [2,4], [5,7], [1,2] ); |
| 10 |
print $poly->nrPoints; |
| 11 |
my @p = $poly->points; |
| 12 |
|
| 13 |
my ($xmin, $ymin, $xmax, $ymax) = $poly->bbox; |
| 14 |
|
| 15 |
my $area = $poly->area; |
| 16 |
my $l = $poly->perimeter; |
| 17 |
if($poly->isClockwise) { ... }; |
| 18 |
|
| 19 |
my $rot = $poly->startMinXY; |
| 20 |
my $center = $poly->centroid; |
| 21 |
if($poly->contains($point)) { ... }; |
| 22 |
|
| 23 |
my $boxed = $poly->lineClip($xmin, $xmax, $ymin, $ymax); |
| 24 |
|
| 25 |
# [2.00] Stack-trace for errors, add this to your script |
| 26 |
# to see where your math goes wrong: |
| 27 |
use Log::Report mode => "DEBUG"; |
| 28 |
|
| 29 |
=head1 DESCRIPTION |
| 30 |
|
| 31 |
This class provides an Object Oriented interface around |
| 32 |
L<Math::Polygon::Calc|Math::Polygon::Calc>, L<Math::Polygon::Clip|Math::Polygon::Clip>, and other. Together, |
| 33 |
these modules provide basic transformations on 2D polygons in pure perl. |
| 34 |
|
| 35 |
B<WARNING:> These computations may show platform dependent rounding |
| 36 |
differences. These may also originate from compilation options of |
| 37 |
the Perl version you installed. |
| 38 |
|
| 39 |
B<TIP:> When you need better accuracy, you may use Math::BigFloat to |
| 40 |
represent coordinate values. Of course, this has a considerable price |
| 41 |
in performance. |
| 42 |
|
| 43 |
=head1 METHODS |
| 44 |
|
| 45 |
=head2 Constructors |
| 46 |
|
| 47 |
=over 4 |
| 48 |
|
| 49 |
=item $any-E<gt>B<new>(%options, [@points], %options) |
| 50 |
|
| 51 |
You may add C<%options> before and/or after the C<@points>. You may also use |
| 52 |
the "points" option to set the points. Each point in C<@points> is |
| 53 |
(a references) to an ARRAY with two elements: an X and a Y coordinate. |
| 54 |
|
| 55 |
When C<new()> is called as instance method, it is believed that the |
| 56 |
new polygon is derived from the callee, and therefore some facts |
| 57 |
(like clockwise or anti-clockwise direction) will get copied unless |
| 58 |
overruled. |
| 59 |
|
| 60 |
-Option --Default |
| 61 |
bbox undef |
| 62 |
clockwise undef |
| 63 |
points undef |
| 64 |
|
| 65 |
=over 2 |
| 66 |
|
| 67 |
=item bbox => [$xmin,$ymin, $xmax,$ymax] |
| 68 |
|
| 69 |
Usually computed from the shape automatically, but can also be |
| 70 |
overruled (for instance because it is already known). See L<bbox()|Math::Polygon/"Geometry">. |
| 71 |
|
| 72 |
=item clockwise => BOOLEAN |
| 73 |
|
| 74 |
Is not specified, it will be computed by the L<isClockwise()|Math::Polygon/"Geometry"> method |
| 75 |
on demand. |
| 76 |
|
| 77 |
=item points => \@points |
| 78 |
|
| 79 |
See L<points()|Math::Polygon/"Attributes"> and L<nrPoints()|Math::Polygon/"Attributes">. |
| 80 |
|
| 81 |
=back |
| 82 |
|
| 83 |
example: creation of new polygon |
| 84 |
|
| 85 |
my $p = Math::Polygon->new([1,0],[1,1],[0,1],[0,0],[1,0]); |
| 86 |
|
| 87 |
my @p = ([1,0],[1,1],[0,1],[0,0],[1,0]); |
| 88 |
my $p = Math::Polygon->new(points => \@p); |
| 89 |
|
| 90 |
=back |
| 91 |
|
| 92 |
=head2 Attributes |
| 93 |
|
| 94 |
=over 4 |
| 95 |
|
| 96 |
=item $obj-E<gt>B<nrPoints>() |
| 97 |
|
| 98 |
Returns the number of points, |
| 99 |
|
| 100 |
=item $obj-E<gt>B<order>() |
| 101 |
|
| 102 |
Returns the number of (unique?) points: one less than L<nrPoints()|Math::Polygon/"Attributes"> |
| 103 |
because we (usually) have a closed polygon. |
| 104 |
|
| 105 |
=item $obj-E<gt>B<point>( $index, [$index,...] ) |
| 106 |
|
| 107 |
Returns the point with the specified C<$index> or INDEXES. In SCALAR context, |
| 108 |
only the first C<$index> is used. |
| 109 |
|
| 110 |
example: |
| 111 |
|
| 112 |
my $point = $poly->point(2); |
| 113 |
my ($first, $last) = $poly->point(0, -1); |
| 114 |
|
| 115 |
=item $obj-E<gt>B<points>( [FORMAT] ) |
| 116 |
|
| 117 |
In LIST context, the points are returned as list, otherwise as |
| 118 |
reference to an ARRAY of points. |
| 119 |
|
| 120 |
[1.09] When a C<FORMAT> is given, each coordinate will get processed. |
| 121 |
This may be useful to hide platform specific rounding errors. C<FORMAT> |
| 122 |
may be a CODE reference or a C<printf()> alike string. |
| 123 |
See L<Math::Polygon::Calc::polygon_format()|Math::Polygon::Calc/"FUNCTIONS">. |
| 124 |
|
| 125 |
example: |
| 126 |
|
| 127 |
my @points = $poly->points; |
| 128 |
my $first = $points[0]; |
| 129 |
my $x0 = $points[0][0]; # == $first->[0] --> X |
| 130 |
my $y0 = $points[0][1]; # == $first->[1] --> Y |
| 131 |
|
| 132 |
my @points = $poly->points("%.2f"); |
| 133 |
|
| 134 |
=back |
| 135 |
|
| 136 |
=head2 Geometry |
| 137 |
|
| 138 |
=over 4 |
| 139 |
|
| 140 |
=item $obj-E<gt>B<area>() |
| 141 |
|
| 142 |
Returns the area enclosed by the polygon. The last point of the list |
| 143 |
must be the same as the first to produce a correct result. The computed |
| 144 |
result is cached. |
| 145 |
Function L<Math::Polygon::Calc::polygon_area()|Math::Polygon::Calc/"FUNCTIONS">. |
| 146 |
|
| 147 |
example: |
| 148 |
|
| 149 |
my $area = $poly->area; |
| 150 |
print "$area $poly_units ^2\n"; |
| 151 |
|
| 152 |
=item $obj-E<gt>B<bbox>() |
| 153 |
|
| 154 |
Returns a list with four elements: (xmin, ymin, xmax, ymax), which describe |
| 155 |
the bounding box of the polygon (all points of the polygon are inside that |
| 156 |
area). The computation is expensive, and therefore, the results are |
| 157 |
cached. |
| 158 |
Function L<Math::Polygon::Calc::polygon_bbox()|Math::Polygon::Calc/"FUNCTIONS">. |
| 159 |
|
| 160 |
example: |
| 161 |
|
| 162 |
my ($xmin, $ymin, $xmax, $ymax) = $poly->bbox; |
| 163 |
|
| 164 |
=item $obj-E<gt>B<beautify>(%options) |
| 165 |
|
| 166 |
Returns a new, beautified version of this polygon. |
| 167 |
Function L<Math::Polygon::Calc::polygon_beautify()|Math::Polygon::Calc/"FUNCTIONS">. |
| 168 |
|
| 169 |
Polygons, certainly after some computations, can have a lot of horrible |
| 170 |
artifacts: points which are double, spikes, etc. |
| 171 |
|
| 172 |
-Option --Default |
| 173 |
remove_spikes false |
| 174 |
|
| 175 |
=over 2 |
| 176 |
|
| 177 |
=item remove_spikes => BOOLEAN |
| 178 |
|
| 179 |
=back |
| 180 |
|
| 181 |
=item $obj-E<gt>B<centroid>(%options) |
| 182 |
|
| 183 |
Returns the centroid location of the polygon. The last point of the list |
| 184 |
must be the same as the first to produce a correct result. The computed |
| 185 |
result is cached. Function L<Math::Polygon::Calc::polygon_centroid()|Math::Polygon::Calc/"FUNCTIONS">. |
| 186 |
|
| 187 |
B<Be aware> that this algorithm does not like very flat polygons. |
| 188 |
|
| 189 |
-Option --Default |
| 190 |
is_large false |
| 191 |
|
| 192 |
=over 2 |
| 193 |
|
| 194 |
=item is_large => BOOLEAN |
| 195 |
|
| 196 |
[2.00] When the polygon is large with respect to its distance to the |
| 197 |
origin, we do not need to be afraid for rounding errors. When set, |
| 198 |
the calculation will be a bit faster. |
| 199 |
|
| 200 |
=back |
| 201 |
|
| 202 |
example: |
| 203 |
|
| 204 |
my $center = $poly->centroid; |
| 205 |
my ($cx, $cy) = @$center; |
| 206 |
|
| 207 |
=item $obj-E<gt>B<clockwise>() |
| 208 |
|
| 209 |
Make sure the points are in clockwise order. |
| 210 |
|
| 211 |
example: |
| 212 |
|
| 213 |
$poly->clockwise; |
| 214 |
|
| 215 |
=item $obj-E<gt>B<contains>($point) |
| 216 |
|
| 217 |
Returns a truth value indicating whether the point is inside the polygon |
| 218 |
or not. On the edge is inside. |
| 219 |
|
| 220 |
=item $obj-E<gt>B<counterClockwise>() |
| 221 |
|
| 222 |
Make sure the points are in counter-clockwise order. |
| 223 |
|
| 224 |
example: |
| 225 |
|
| 226 |
$poly->counterClockwise |
| 227 |
|
| 228 |
=item $obj-E<gt>B<distance>($point) |
| 229 |
|
| 230 |
[1.05] Returns the distance of the point to the closest point on the border of |
| 231 |
the polygon, zero if the point is on an edge. |
| 232 |
|
| 233 |
=item $obj-E<gt>B<equal>(($other|\@points, [$tolerance]) | @points) |
| 234 |
|
| 235 |
Compare two polygons on the level of C<@points>. When the polygons are |
| 236 |
the same but rotated or mirrored, this returns C<false>. See L<same()|Math::Polygon/"Geometry"> for |
| 237 |
the more thorrow (and expensive) comparison. |
| 238 |
Function L<Math::Polygon::Calc::polygon_equal()|Math::Polygon::Calc/"FUNCTIONS">. |
| 239 |
|
| 240 |
example: |
| 241 |
|
| 242 |
if($poly->equal($other_poly, 0.1)) ... |
| 243 |
if($poly->equal(\@points, 0.1)) ... |
| 244 |
if($poly->equal(@points)) ... |
| 245 |
|
| 246 |
=item $obj-E<gt>B<isClockwise>() |
| 247 |
|
| 248 |
The points are (in majority) orded in the direction of the hands of the clock. |
| 249 |
This calculation is quite expensive (same effort as calculating the area of |
| 250 |
the polygon), and the result is therefore cached. |
| 251 |
|
| 252 |
example: |
| 253 |
|
| 254 |
if($poly->isClockwise) ... |
| 255 |
|
| 256 |
=item $obj-E<gt>B<isClosed>() |
| 257 |
|
| 258 |
Returns C<true> if the first point of the poly definition is the same |
| 259 |
as the last point. |
| 260 |
|
| 261 |
=item $obj-E<gt>B<perimeter>() |
| 262 |
|
| 263 |
The length of the line of the polygon. This can also be used to compute |
| 264 |
the length of any line: of the last point is not equal to the first, then |
| 265 |
a line is presumed; for a polygon they must match. |
| 266 |
Function L<Math::Polygon::Calc::polygon_perimeter()|Math::Polygon::Calc/"FUNCTIONS">. |
| 267 |
|
| 268 |
example: |
| 269 |
|
| 270 |
my $fence = $poly->perimeter; |
| 271 |
print "fence length: $fence $poly_units\n" |
| 272 |
|
| 273 |
=item $obj-E<gt>B<same>(($other|\@points, [$tolerance]) | @points) |
| 274 |
|
| 275 |
[1.12] Compare two polygons, where the polygons may be rotated or |
| 276 |
mirrored wrt each other. This is (much) slower than L<equal()|Math::Polygon/"Geometry">, but |
| 277 |
some algorithms will cause un unpredictable rotation in the result. |
| 278 |
Function L<Math::Polygon::Calc::polygon_same()|Math::Polygon::Calc/"FUNCTIONS">. |
| 279 |
|
| 280 |
example: |
| 281 |
|
| 282 |
if($poly->same($other_poly, 0.1)) ... |
| 283 |
if($poly->same(\@points, 0.1)) ... |
| 284 |
if($poly->same(@points)) ... |
| 285 |
|
| 286 |
=item $obj-E<gt>B<startMinXY>() |
| 287 |
|
| 288 |
Returns a new polygon object, where the points are rotated in such a way |
| 289 |
that the point which is closest to the left-bottom point of the bounding |
| 290 |
box has become the first. |
| 291 |
|
| 292 |
Function L<Math::Polygon::Calc::polygon_start_minxy()|Math::Polygon::Calc/"FUNCTIONS">. |
| 293 |
|
| 294 |
=back |
| 295 |
|
| 296 |
=head2 Transformations |
| 297 |
|
| 298 |
Implemented in L<Math::Polygon::Transform|Math::Polygon::Transform>: changes on the structure of |
| 299 |
the polygon except clipping. All functions return a new polygon object |
| 300 |
or C<undef>. |
| 301 |
|
| 302 |
=over 4 |
| 303 |
|
| 304 |
=item $obj-E<gt>B<grid>(%options) |
| 305 |
|
| 306 |
Returns a polygon object with the points snapped to grid points. |
| 307 |
See L<Math::Polygon::Transform::polygon_grid()|Math::Polygon::Transform/"FUNCTIONS">. |
| 308 |
|
| 309 |
-Option--Default |
| 310 |
raster 1.0 |
| 311 |
|
| 312 |
=over 2 |
| 313 |
|
| 314 |
=item raster => FLOAT |
| 315 |
|
| 316 |
The raster size, which determines the points to round to. The origin |
| 317 |
C<[0,0]> is always on a grid-point. When the raster value is zero, |
| 318 |
no transformation will take place. |
| 319 |
|
| 320 |
=back |
| 321 |
|
| 322 |
=item $obj-E<gt>B<mirror>(%options) |
| 323 |
|
| 324 |
Mirror the polygon in a line. Only one of the options can be provided. |
| 325 |
Some programs call this "flip" or "flop". |
| 326 |
|
| 327 |
-Option--Default |
| 328 |
b 0 |
| 329 |
line <C<undef>> |
| 330 |
rc undef |
| 331 |
x undef |
| 332 |
y undef |
| 333 |
|
| 334 |
=over 2 |
| 335 |
|
| 336 |
=item b => FLOAT |
| 337 |
|
| 338 |
Only used in combination with option C<rc> to describe a line. |
| 339 |
|
| 340 |
=item line => [POINT, POINT] |
| 341 |
|
| 342 |
Alternative way to specify the mirror line. The C<rc> and C<b> are |
| 343 |
computed from the two points of the line. |
| 344 |
|
| 345 |
=item rc => FLOAT |
| 346 |
|
| 347 |
Description of the line which is used to mirror in. The line is |
| 348 |
C<y= rc*x+b>. The C<rc> equals C<-dy/dx>, the firing angle. If |
| 349 |
C<undef> is explicitly specified then C<b> is used as constant x: it's |
| 350 |
a vertical mirror. |
| 351 |
|
| 352 |
=item x => FLOAT |
| 353 |
|
| 354 |
Mirror in the line C<x=value>, which means that C<y> stays unchanged. |
| 355 |
|
| 356 |
=item y => FLOAT |
| 357 |
|
| 358 |
Mirror in the line C<y=value>, which means that C<x> stays unchanged. |
| 359 |
|
| 360 |
=back |
| 361 |
|
| 362 |
=item $obj-E<gt>B<move>(%options) |
| 363 |
|
| 364 |
Returns a moved polygon object: all point are moved over the |
| 365 |
indicated distance. See L<Math::Polygon::Transform::polygon_move()|Math::Polygon::Transform/"FUNCTIONS">. |
| 366 |
|
| 367 |
-Option--Default |
| 368 |
dx 0 |
| 369 |
dy 0 |
| 370 |
|
| 371 |
=over 2 |
| 372 |
|
| 373 |
=item dx => FLOAT |
| 374 |
|
| 375 |
Displacement in the horizontal direction. |
| 376 |
|
| 377 |
=item dy => FLOAT |
| 378 |
|
| 379 |
Displacement in the vertical direction. |
| 380 |
|
| 381 |
=back |
| 382 |
|
| 383 |
=item $obj-E<gt>B<resize>(%options) |
| 384 |
|
| 385 |
Returns a resized polygon object. |
| 386 |
See L<Math::Polygon::Transform::polygon_resize()|Math::Polygon::Transform/"FUNCTIONS">. |
| 387 |
|
| 388 |
-Option--Default |
| 389 |
center [0,0] |
| 390 |
scale 1.0 |
| 391 |
xscale <scale> |
| 392 |
yscale <scale> |
| 393 |
|
| 394 |
=over 2 |
| 395 |
|
| 396 |
=item center => $point |
| 397 |
|
| 398 |
=item scale => FLOAT |
| 399 |
|
| 400 |
Resize the polygon with the indicated factor. When the factor is larger |
| 401 |
than 1, the resulting polygon with grow, when small it will be reduced in |
| 402 |
size. The scale will be respective from the center. |
| 403 |
|
| 404 |
=item xscale => FLOAT |
| 405 |
|
| 406 |
Specific scaling factor in the horizontal direction. |
| 407 |
|
| 408 |
=item yscale => FLOAT |
| 409 |
|
| 410 |
Specific scaling factor in the vertical direction. |
| 411 |
|
| 412 |
=back |
| 413 |
|
| 414 |
=item $obj-E<gt>B<rotate>(%options) |
| 415 |
|
| 416 |
Returns a rotated polygon object: all point are moved over the |
| 417 |
indicated distance. See L<Math::Polygon::Transform::polygon_rotate()|Math::Polygon::Transform/"FUNCTIONS">. |
| 418 |
|
| 419 |
-Option --Default |
| 420 |
center [0,0] |
| 421 |
degrees 0 |
| 422 |
radians 0 |
| 423 |
|
| 424 |
=over 2 |
| 425 |
|
| 426 |
=item center => POINT |
| 427 |
|
| 428 |
=item degrees => FLOAT |
| 429 |
|
| 430 |
specify rotation angle in degrees (between -180 and 360). |
| 431 |
|
| 432 |
=item radians => FLOAT |
| 433 |
|
| 434 |
specify rotation angle in rads (between -pi and 2*pi) |
| 435 |
|
| 436 |
=back |
| 437 |
|
| 438 |
=item $obj-E<gt>B<simplify>(%options) |
| 439 |
|
| 440 |
Returns a polygon object where points are removed. |
| 441 |
See L<Math::Polygon::Transform::polygon_simplify()|Math::Polygon::Transform/"FUNCTIONS">. |
| 442 |
|
| 443 |
-Option --Default |
| 444 |
max_points undef |
| 445 |
same 0.0001 |
| 446 |
slope undef |
| 447 |
|
| 448 |
=over 2 |
| 449 |
|
| 450 |
=item max_points => INTEGER |
| 451 |
|
| 452 |
First, C<same> and C<slope> reduce the number of points. Then, if there |
| 453 |
are still more than the specified number of points left, the points with |
| 454 |
the widest angles will be removed until the specified maximum number is |
| 455 |
reached. |
| 456 |
|
| 457 |
=item same => FLOAT |
| 458 |
|
| 459 |
The distance between two points to be considered "the same" point. The value |
| 460 |
is used as radius of the circle. |
| 461 |
|
| 462 |
=item slope => FLOAT |
| 463 |
|
| 464 |
With three points X(n),X(n+1),X(n+2), the point X(n+1) will be removed if |
| 465 |
the length of the path over all three points is less than C<slope> longer |
| 466 |
than the direct path between X(n) and X(n+2). |
| 467 |
|
| 468 |
The slope will not be removed around the starting point of the polygon. |
| 469 |
Removing points will change the area of the polygon. |
| 470 |
|
| 471 |
=back |
| 472 |
|
| 473 |
=back |
| 474 |
|
| 475 |
=head2 Clipping |
| 476 |
|
| 477 |
=over 4 |
| 478 |
|
| 479 |
=item $obj-E<gt>B<fillClip1>($box) |
| 480 |
|
| 481 |
Clipping a polygon into rectangles can be done in various ways. |
| 482 |
With this algorithm, the parts of the polygon which are outside |
| 483 |
the C<$box> are mapped on the borders. The polygon stays in one piece, |
| 484 |
but may have vertices which are followed in two directions. |
| 485 |
|
| 486 |
Returned is one polygon, which is cleaned from double points, |
| 487 |
spikes and superfluous intermediate points, or C<undef> when |
| 488 |
no polygon is outside the C<$box>. |
| 489 |
Function L<Math::Polygon::Clip::polygon_fill_clip1()|Math::Polygon::Clip/"FUNCTIONS">. |
| 490 |
|
| 491 |
=item $obj-E<gt>B<lineClip>($box) |
| 492 |
|
| 493 |
Returned is a LIST of ARRAYS-of-points containing line pieces |
| 494 |
from the input polygon. |
| 495 |
Function L<Math::Polygon::Clip::polygon_line_clip()|Math::Polygon::Clip/"FUNCTIONS">. |
| 496 |
|
| 497 |
=back |
| 498 |
|
| 499 |
=head2 Display |
| 500 |
|
| 501 |
=over 4 |
| 502 |
|
| 503 |
=item $obj-E<gt>B<string>( [FORMAT] ) |
| 504 |
|
| 505 |
Print the polygon. |
| 506 |
|
| 507 |
[1.09] When a C<FORMAT> is specified, all coordinates will get formatted |
| 508 |
first. This may hide platform dependent rounding differences. |
| 509 |
|
| 510 |
=back |
| 511 |
|
| 512 |
=head1 SEE ALSO |
| 513 |
|
| 514 |
This module is part of Math-Polygon version 2.00, |
| 515 |
built on September 04, 2025. Website: F<http://perl.overmeer.net/CPAN/> |
| 516 |
|
| 517 |
=head1 LICENSE |
| 518 |
|
| 519 |
For contributors see file ChangeLog. |
| 520 |
|
| 521 |
This software is copyright (c) 2004-2025 by Mark Overmeer. |
| 522 |
|
| 523 |
This is free software; you can redistribute it and/or modify it under |
| 524 |
the same terms as the Perl 5 programming language system itself. |
| 525 |
|