=encoding utf8 =head1 NAME Math::Polygon - Class for maintaining polygon data =head1 SYNOPSIS my $poly = Math::Polygon->new( [1,2], [2,4], [5,7], [1,2] ); print $poly->nrPoints; my @p = $poly->points; my ($xmin, $ymin, $xmax, $ymax) = $poly->bbox; my $area = $poly->area; my $l = $poly->perimeter; if($poly->isClockwise) { ... }; my $rot = $poly->startMinXY; my $center = $poly->centroid; if($poly->contains($point)) { ... }; my $boxed = $poly->lineClip($xmin, $xmax, $ymin, $ymax); # [2.00] Stack-trace for errors, add this to your script # to see where your math goes wrong: use Log::Report mode => "DEBUG"; =head1 DESCRIPTION This class provides an Object Oriented interface around L, L, and other. Together, these modules provide basic transformations on 2D polygons in pure perl. B These computations may show platform dependent rounding differences. These may also originate from compilation options of the Perl version you installed. B When you need better accuracy, you may use Math::BigFloat to represent coordinate values. Of course, this has a considerable price in performance. =head1 METHODS =head2 Constructors =over 4 =item $any-EB(%options, [@points], %options) You may add C<%options> before and/or after the C<@points>. You may also use the "points" option to set the points. Each point in C<@points> is (a references) to an ARRAY with two elements: an X and a Y coordinate. When C is called as instance method, it is believed that the new polygon is derived from the callee, and therefore some facts (like clockwise or anti-clockwise direction) will get copied unless overruled. -Option --Default bbox undef clockwise undef points undef =over 2 =item bbox => [$xmin,$ymin, $xmax,$ymax] Usually computed from the shape automatically, but can also be overruled (for instance because it is already known). See L. =item clockwise => BOOLEAN Is not specified, it will be computed by the L method on demand. =item points => \@points See L and L. =back example: creation of new polygon my $p = Math::Polygon->new([1,0],[1,1],[0,1],[0,0],[1,0]); my @p = ([1,0],[1,1],[0,1],[0,0],[1,0]); my $p = Math::Polygon->new(points => \@p); =back =head2 Attributes =over 4 =item $obj-EB() Returns the number of points, =item $obj-EB() Returns the number of (unique?) points: one less than L because we (usually) have a closed polygon. =item $obj-EB( $index, [$index,...] ) Returns the point with the specified C<$index> or INDEXES. In SCALAR context, only the first C<$index> is used. example: my $point = $poly->point(2); my ($first, $last) = $poly->point(0, -1); =item $obj-EB( [FORMAT] ) In LIST context, the points are returned as list, otherwise as reference to an ARRAY of points. [1.09] When a C is given, each coordinate will get processed. This may be useful to hide platform specific rounding errors. C may be a CODE reference or a C alike string. See L. example: my @points = $poly->points; my $first = $points[0]; my $x0 = $points[0][0]; # == $first->[0] --> X my $y0 = $points[0][1]; # == $first->[1] --> Y my @points = $poly->points("%.2f"); =back =head2 Geometry =over 4 =item $obj-EB() Returns the area enclosed by the polygon. The last point of the list must be the same as the first to produce a correct result. The computed result is cached. Function L. example: my $area = $poly->area; print "$area $poly_units ^2\n"; =item $obj-EB() Returns a list with four elements: (xmin, ymin, xmax, ymax), which describe the bounding box of the polygon (all points of the polygon are inside that area). The computation is expensive, and therefore, the results are cached. Function L. example: my ($xmin, $ymin, $xmax, $ymax) = $poly->bbox; =item $obj-EB(%options) Returns a new, beautified version of this polygon. Function L. Polygons, certainly after some computations, can have a lot of horrible artifacts: points which are double, spikes, etc. -Option --Default remove_spikes false =over 2 =item remove_spikes => BOOLEAN =back =item $obj-EB(%options) Returns the centroid location of the polygon. The last point of the list must be the same as the first to produce a correct result. The computed result is cached. Function L. B that this algorithm does not like very flat polygons. -Option --Default is_large false =over 2 =item is_large => BOOLEAN [2.00] When the polygon is large with respect to its distance to the origin, we do not need to be afraid for rounding errors. When set, the calculation will be a bit faster. =back example: my $center = $poly->centroid; my ($cx, $cy) = @$center; =item $obj-EB() Make sure the points are in clockwise order. example: $poly->clockwise; =item $obj-EB($point) Returns a truth value indicating whether the point is inside the polygon or not. On the edge is inside. =item $obj-EB() Make sure the points are in counter-clockwise order. example: $poly->counterClockwise =item $obj-EB($point) [1.05] Returns the distance of the point to the closest point on the border of the polygon, zero if the point is on an edge. =item $obj-EB(($other|\@points, [$tolerance]) | @points) Compare two polygons on the level of C<@points>. When the polygons are the same but rotated or mirrored, this returns C. See L for the more thorrow (and expensive) comparison. Function L. example: if($poly->equal($other_poly, 0.1)) ... if($poly->equal(\@points, 0.1)) ... if($poly->equal(@points)) ... =item $obj-EB() The points are (in majority) orded in the direction of the hands of the clock. This calculation is quite expensive (same effort as calculating the area of the polygon), and the result is therefore cached. example: if($poly->isClockwise) ... =item $obj-EB() Returns C if the first point of the poly definition is the same as the last point. =item $obj-EB() The length of the line of the polygon. This can also be used to compute the length of any line: of the last point is not equal to the first, then a line is presumed; for a polygon they must match. Function L. example: my $fence = $poly->perimeter; print "fence length: $fence $poly_units\n" =item $obj-EB(($other|\@points, [$tolerance]) | @points) [1.12] Compare two polygons, where the polygons may be rotated or mirrored wrt each other. This is (much) slower than L, but some algorithms will cause un unpredictable rotation in the result. Function L. example: if($poly->same($other_poly, 0.1)) ... if($poly->same(\@points, 0.1)) ... if($poly->same(@points)) ... =item $obj-EB() Returns a new polygon object, where the points are rotated in such a way that the point which is closest to the left-bottom point of the bounding box has become the first. Function L. =back =head2 Transformations Implemented in L: changes on the structure of the polygon except clipping. All functions return a new polygon object or C. =over 4 =item $obj-EB(%options) Returns a polygon object with the points snapped to grid points. See L. -Option--Default raster 1.0 =over 2 =item raster => FLOAT The raster size, which determines the points to round to. The origin C<[0,0]> is always on a grid-point. When the raster value is zero, no transformation will take place. =back =item $obj-EB(%options) Mirror the polygon in a line. Only one of the options can be provided. Some programs call this "flip" or "flop". -Option--Default b 0 line > rc undef x undef y undef =over 2 =item b => FLOAT Only used in combination with option C to describe a line. =item line => [POINT, POINT] Alternative way to specify the mirror line. The C and C are computed from the two points of the line. =item rc => FLOAT Description of the line which is used to mirror in. The line is C. The C equals C<-dy/dx>, the firing angle. If C is explicitly specified then C is used as constant x: it's a vertical mirror. =item x => FLOAT Mirror in the line C, which means that C stays unchanged. =item y => FLOAT Mirror in the line C, which means that C stays unchanged. =back =item $obj-EB(%options) Returns a moved polygon object: all point are moved over the indicated distance. See L. -Option--Default dx 0 dy 0 =over 2 =item dx => FLOAT Displacement in the horizontal direction. =item dy => FLOAT Displacement in the vertical direction. =back =item $obj-EB(%options) Returns a resized polygon object. See L. -Option--Default center [0,0] scale 1.0 xscale yscale =over 2 =item center => $point =item scale => FLOAT Resize the polygon with the indicated factor. When the factor is larger than 1, the resulting polygon with grow, when small it will be reduced in size. The scale will be respective from the center. =item xscale => FLOAT Specific scaling factor in the horizontal direction. =item yscale => FLOAT Specific scaling factor in the vertical direction. =back =item $obj-EB(%options) Returns a rotated polygon object: all point are moved over the indicated distance. See L. -Option --Default center [0,0] degrees 0 radians 0 =over 2 =item center => POINT =item degrees => FLOAT specify rotation angle in degrees (between -180 and 360). =item radians => FLOAT specify rotation angle in rads (between -pi and 2*pi) =back =item $obj-EB(%options) Returns a polygon object where points are removed. See L. -Option --Default max_points undef same 0.0001 slope undef =over 2 =item max_points => INTEGER First, C and C reduce the number of points. Then, if there are still more than the specified number of points left, the points with the widest angles will be removed until the specified maximum number is reached. =item same => FLOAT The distance between two points to be considered "the same" point. The value is used as radius of the circle. =item slope => FLOAT With three points X(n),X(n+1),X(n+2), the point X(n+1) will be removed if the length of the path over all three points is less than C longer than the direct path between X(n) and X(n+2). The slope will not be removed around the starting point of the polygon. Removing points will change the area of the polygon. =back =back =head2 Clipping =over 4 =item $obj-EB($box) Clipping a polygon into rectangles can be done in various ways. With this algorithm, the parts of the polygon which are outside the C<$box> are mapped on the borders. The polygon stays in one piece, but may have vertices which are followed in two directions. Returned is one polygon, which is cleaned from double points, spikes and superfluous intermediate points, or C when no polygon is outside the C<$box>. Function L. =item $obj-EB($box) Returned is a LIST of ARRAYS-of-points containing line pieces from the input polygon. Function L. =back =head2 Display =over 4 =item $obj-EB( [FORMAT] ) Print the polygon. [1.09] When a C is specified, all coordinates will get formatted first. This may hide platform dependent rounding differences. =back =head1 SEE ALSO This module is part of Math-Polygon version 2.00, built on September 04, 2025. Website: F =head1 LICENSE For contributors see file ChangeLog. This software is copyright (c) 2004-2025 by Mark Overmeer. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.