The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Math::Polygon::Tree - fast check if point is inside polygon

VERSION

version 0.05

SYNOPSIS

    use Math::Polygon::Tree;

    my $poly  = [ [0,0], [0,2], [2,2], ... ];
    my $bound = Math::Polygon::Tree->new( $poly );

    if ( $bound->contains( [1,1] ) )  { ... }

DESCRIPTION

Math::Polygon::Tree creates a B-tree of polygon parts for fast check if object is inside this polygon. This method is effective if polygon has hundreds or more segments.

METHODS

new

Takes [at least one] contour and creates a tree structure. All polygons are outer, inners in not implemented.

Contour is an arrayref of points:

    my $poly1 = [ [0,0], [0,2], [2,2], ... ];   
    ...
    my $bound = Math::Polygon::Tree->new( $poly1, $poly2, ... );

or a .poly file

    my $bound = Math::Polygon::Tree->new( 'boundary.poly' );

contains

    if ( $bound->contains( [1,1] ) )  { ... }

Checks if point is inside bound polygon.

Returns 1 if point is inside polygon, -1 if it lays on polygon boundary (dirty), or 0 otherwise.

contains_points

Checks if points are inside bound polygon.

Returns 1 if all points are inside polygon, 0 if all outside, or undef.

    if ( $bound->contains_points( [1,1], [2,2] ... ) )  { ...

contains_bbox_rough

Checks if box is inside bound polygon.

Returns 1 if box is inside polygon, 0 if box is outside polygon or undef if it 'doubts'.

    my ($xmin, $ymin, $xmax, $ymax) = ( 1, 1, 2, 2 );
    if ( $bound->contains_bbox_rough( $xmin, $ymin, $xmax, $ymax ) )  { ... }

contains_polygon_rough

Checks if polygon is inside bound polygon.

Returns 1 if inside, 0 if outside or undef if 'doubts'.

    if ( $bound->contains_polygon_rough( [ [1,1], [1,2], [2,2], ... ] ) )  { ... }

bbox

Returns polygon's bounding box.

    my ( $xmin, $ymin, $xmax, $ymax ) = $bound->bbox();

FUNCTIONS

polygon_bbox

Function that returns polygon's bbox.

    my ( $xmin, $ymin, $xmax, $ymax ) = polygon_bbox( [1,1], [1,2], [2,2], ... );

polygon_centroid

Function that returns polygon's weightened center.

    my ( $x, $y ) = polygon_centroid( [1,1], [1,2], [2,2], ... );

polygon_contains_point

Function that tests if polygon contains point (modified one from Math::Polygon::Calc).

Returns -1 if point lays on polygon's boundary

AUTHOR

liosha <liosha@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by liosha.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.