Quick Navigator

 Search Site Miscellaneous Server Agreement Year 2038 Credits
 Math::Polygon(3) User Contributed Perl Documentation Math::Polygon(3)

# NAME

Math::Polygon - Class for maintaining polygon data

# 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);
```

# DESCRIPTION

This class provides an Object Oriented interface around Math::Polygon::Calc, Math::Polygon::Clip, and other. Together, these modules provide basic transformations on 2D polygons in pure perl.
WARNING: these computations may show platform dependent ronding differences. These may also originate from compilation options of the Perl version you installed.

# METHODS

## Constructors

\$obj->new(%options, [@points], %options)
Math::Polygon->new(%options, [@points], %options)
You may add %options before and/or after the @points. You may also use the "points" option to set the points. Each point in @points is (a references) to an ARRAY with two elements: an X and a Y coordinate.
When "new()" 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
```
bbox => [\$xmin,\$ymin, \$xmax,\$ymax]
Usually computed from the shape automatically, but can also be overruled. See bbox().
clockwise => BOOLEAN
Is not specified, it will be computed by the isClockwise() method on demand.
points => \@points
See points() and nrPoints().
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);
```

## Attributes

\$obj->nrPoints()
Returns the number of points,
\$obj->order()
Returns the number of (unique?) points: one less than nrPoints().
\$obj->point( \$index, [\$index,...] )
Returns the point with the specified \$index or INDEXES. In SCALAR context, only the first \$index is used.
example:
```  my \$point = \$poly->point(2);
my (\$first, \$last) = \$poly->point(0, -1);
```
\$obj->points( [FORMAT] )
In LIST context, the points are returned as list, otherwise as reference to an ARRAY of points.
[1.09] When a FORMAT is given, each coordinate will get processed. This may be useful to hide platform specific rounding errors. FORMAT may be a CODE reference or a "printf()" alike string. See Math::Polygon::Calc::polygon_format().
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");
```

## Geometry

\$obj->area()
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 Math::Polygon::Calc::polygon_area().
example:
```  my \$area = \$poly->area;
print "\$area \$poly_units ^2\n";
```
\$obj->bbox()
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 Math::Polygon::Calc::polygon_bbox().
example:
```  my (\$xmin, \$ymin, \$xmax, \$ymax) = \$poly->bbox;
```
\$obj->beautify(%options)
Returns a new, beautified version of this polygon. Function Math::Polygon::Calc::polygon_beautify().
Polygons, certainly after some computations, can have a lot of horrible artifacts: points which are double, spikes, etc. This functions provided by this module beautify them. A new polygon is returned.
``` -Option       --Default
remove_spikes  <false>
```
remove_spikes => BOOLEAN
\$obj->centroid()
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 Math::Polygon::Calc::polygon_centroid().
example:
```  my \$center = \$poly->centroid;
my (\$cx, \$cy) = @\$center;
```
\$obj->clockwise()
Make sure the points are in clockwise order.
example:
```  \$poly->clockwise;
```
\$obj->contains(\$point)
Returns a truth value indicating whether the point is inside the polygon or not. On the edge is inside.
\$obj->counterClockwise()
Make sure the points are in counter-clockwise order.
example:
```  \$poly->counterClockwise
```
\$obj->distance(\$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.
\$obj->equal( <\$other | \@points,[\$tolerance]> | \$points )
Compare two polygons, on the level of points. When the polygons are the same but rotated, this will return false. See same(). Function Math::Polygon::Calc::polygon_equal().
example:
```  if(\$poly->equal(\$other_poly, 0.1)) ...
if(\$poly->equal(\@points, 0.1)) ...
if(\$poly->equal(@points)) ...
```
\$obj->isClockwise()
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) ...
```
\$obj->isClosed()
Returns true if the first point of the poly definition is the same as the last point.
\$obj->perimeter()
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 Math::Polygon::Calc::polygon_perimeter().
example:
``` my \$fence = \$poly->perimeter;
print "fence length: \$fence \$poly_units\n"
```
\$obj->same( <\$other_polygon | \@points, [\$tolerance]> | @points )
Compare two polygons, where the polygons may be rotated wrt each other. This is (much) slower than equal(), but some algorithms will cause un unpredictable rotation in the result. Function Math::Polygon::Calc::polygon_same().
example:
```  if(\$poly->same(\$other_poly, 0.1)) ...
if(\$poly->same(\@points, 0.1)) ...
if(\$poly->same(@points)) ...
```
\$obj->startMinXY()
Returns a new polygon object, where the points are rotated in such a way that the point which is losest to the left-bottom point of the bounding box has become the first.
Function Math::Polygon::Calc::polygon_start_minxy().

## Transformations

Implemented in Math::Polygon::Transform: changes on the structure of the polygon except clipping. All functions return a new polygon object or undef.
\$obj->grid(%options)
Returns a polygon object with the points snapped to grid points. See Math::Polygon::Transform::polygon_grid().
``` -Option--Default
raster  1.0
```
raster => FLOAT
The raster size, which determines the points to round to. The origin "[0,0]" is always on a grid-point. When the raster value is zero, no transformation will take place.
\$obj->mirror(%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    <undef>
rc      undef
x       undef
y       undef
```
b => FLOAT
Only used in combination with option "rc" to describe a line.
line => [POINT, POINT]
Alternative way to specify the mirror line. The "rc" and "b" are computed from the two points of the line.
rc => FLOAT
Description of the line which is used to mirror in. The line is "y= rc*x+b". The "rc" equals "-dy/dx", the firing angle. If "undef" is explicitly specified then "b" is used as constant x: it's a vertical mirror.
x => FLOAT
Mirror in the line "x=value", which means that "y" stays unchanged.
y => FLOAT
Mirror in the line "y=value", which means that "x" stays unchanged.
\$obj->move(%options)
Returns a moved polygon object: all point are moved over the indicated distance. See Math::Polygon::Transform::polygon_move().
``` -Option--Default
dx      0
dy      0
```
dx => FLOAT
Displacement in the horizontal direction.
dy => FLOAT
Displacement in the vertical direction.
\$obj->resize(%options)
Returns a resized polygon object. See Math::Polygon::Transform::polygon_resize().
``` -Option--Default
center  [0,0]
scale   1.0
xscale  <scale>
yscale  <scale>
```
center => \$point
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.
xscale => FLOAT
Specific scaling factor in the horizontal direction.
yscale => FLOAT
Specific scaling factor in the vertical direction.
\$obj->rotate(%options)
Returns a rotated polygon object: all point are moved over the indicated distance. See Math::Polygon::Transform::polygon_rotate().
``` -Option --Default
center   [0,0]
degrees  0
```
center => POINT
degrees => FLOAT
specify rotation angle in degrees (between -180 and 360).
specify rotation angle in rads (between -pi and 2*pi)
\$obj->simplify(%options)
Returns a polygon object where points are removed. See Math::Polygon::Transform::polygon_simplify().
``` -Option    --Default
max_points  undef
same        0.0001
slope       undef
```
max_points => INTEGER
First, "same" and "slope" 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.
same => FLOAT
The distance between two points to be considered "the same" point. The value is used as radius of the circle.
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 "slope" 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.

## Clipping

\$obj->fillClip1(\$box)
Clipping a polygon into rectangles can be done in various ways. With this algorithm, the parts of the polygon which are outside the \$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 "undef" when no polygon is outside the \$box. Function Math::Polygon::Clip::polygon_fill_clip1().
\$obj->lineClip(\$box)
Returned is a list of ARRAYS-OF-POINTS containing line pieces from the input polygon. Function Math::Polygon::Clip::polygon_line_clip().

## Display

\$obj->string( [FORMAT] )
Print the polygon.
[1.09] When a FORMAT is specified, all coordinates will get formatted first. This may hide platform dependent rounding differences.