File Coverage

blib/lib/Geometry/Primitive/Polygon.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Geometry::Primitive::Polygon;
2 1     1   12092 use Moose;
  0            
  0            
3             use MooseX::Storage;
4              
5             with qw(Geometry::Primitive::Shape MooseX::Clone MooseX::Storage::Deferred);
6              
7             has 'points' => (
8             traits => [ qw(Array Clone) ],
9             is => 'rw',
10             isa => 'ArrayRef[Geometry::Primitive::Point]',
11             default => sub { [] },
12             handles => {
13             add_point => 'push',
14             clear_points=> 'clear',
15             get_point => 'get',
16             point_count => 'count'
17             }
18             );
19              
20             sub area {
21             my ($self) = @_;
22              
23             # http://mathworld.wolfram.com/PolygonArea.html
24             my $area = 0;
25             my $last = $self->get_point(0);
26             for (my $i = 1; $i < $self->point_count; $i++) {
27             my $curr = $self->get_point($i);
28              
29             $area += ($last->x * $curr->y) - ($curr->x * $last->y);
30             $last = $curr;
31             }
32              
33             return abs($area / 2);
34             }
35              
36             sub point_end {
37             my ($self) = @_;
38              
39             return $self->get_point($self->point_count - 1);
40             }
41              
42             sub point_start {
43             my ($self) = @_;
44              
45             return $self->get_point(0);
46             }
47              
48             sub scale {
49             my ($self, $amount) = @_;
50              
51             foreach my $p (@{ $self->points }) {
52             $p->x($p->x * $amount);
53             $p->y($p->y * $amount);
54             }
55              
56             }
57              
58             __PACKAGE__->meta->make_immutable;
59              
60             no Moose;
61             1;
62              
63             __END__
64              
65             =head1 NAME
66              
67             Geometry::Primitive::Polygon - Closed shape with an arbitrary number of points.
68              
69             =head1 DESCRIPTION
70              
71             Geometry::Primitive::Polygon represents a two dimensional figure bounded by a
72             series of points that represent a closed path.
73              
74             =head1 SYNOPSIS
75              
76             use Geometry::Primitive::Polygon;
77              
78             my $poly = Geometry::Primitive::Polygon->new;
79             $poly->add_point($point1);
80             $poly->add_point($point2);
81             $poly->add_point($point3);
82             # No need to close the path, it's handled automatically
83              
84             =head1 ATTRIBUTES
85              
86             =head2 points
87              
88             Set/Get the arrayref of points that make up this Polygon.
89              
90             =head1 METHODS
91              
92             =head2 new
93              
94             Creates a new Geometry::Primitive::Polygon
95              
96             =head2 area
97              
98             Area of this polygon. Assumes it is non-self-intersecting.
99              
100             =head2 add_point
101              
102             Add a point to this polygon.
103              
104             =head2 clear_points
105              
106             Clears all points from this polygon.
107              
108             =head2 point_count
109              
110             Returns the number of points that bound this polygon.
111              
112             =head2 get_point
113              
114             Returns the point at the specified offset.
115              
116             =head2 point_end
117              
118             Get the end point. Provided for Shape role.
119              
120             =head2 point_start
121              
122             Get the start point. Provided for Shape role.
123              
124             =head2 scale ($amount)
125              
126             Scale this this polygon by the supplied amount.
127              
128             =head1 AUTHOR
129              
130             Cory Watson <gphat@cpan.org>
131              
132             =head1 COPYRIGHT & LICENSE
133              
134             You can redistribute and/or modify this code under the same terms as Perl
135             itself.