File Coverage

blib/lib/Shape/RegularPolygon.pm
Criterion Covered Total %
statement 40 40 100.0
branch 11 14 78.5
condition 2 3 66.6
subroutine 10 10 100.0
pod 5 6 83.3
total 68 73 93.1


line stmt bran cond sub pod time code
1             package Shape::RegularPolygon;
2              
3 1     1   24705 use 5.008006;
  1         3  
  1         29  
4 1     1   5 use strict;
  1         1  
  1         27  
5 1     1   4 use warnings;
  1         5  
  1         22  
6              
7 1     1   2089 use Math::Trig;
  1         34176  
  1         673  
8              
9             require Exporter;
10              
11             our @ISA = qw(Exporter);
12             our @EXPORT = qw();
13              
14             our $VERSION = '0.01';
15              
16              
17             sub new {
18 4     4 0 19 my $class = shift;
19 4         12 my %params = @_;
20              
21 4         20 my $self = {CenterX => 0,
22             CenterY => 0,
23             Sides => 3,
24             Radius => 50,
25             Angle => 0.0,
26             };
27              
28 4         10 foreach my $name (keys %params) {
29 10 50       26 $self->{$name} = $params{$name} if exists $self->{$name};
30             }
31              
32 4 50       12 ($self->{Sides} >= 3) or die "Sides is too small.";
33              
34 4         17 bless($self, $class);
35             }
36              
37             sub center {
38 5     5 1 27 my ($self, $x, $y) = @_;
39 5 100 66     31 return ($self->{CenterX}, $self->{CenterY})
40             if !defined $x || !defined $y;
41 2         3 $self->{CenterX} = $x;
42 2         5 $self->{CenterY} = $y;
43             }
44             sub sides {
45 5     5 1 1386 my ($self, $sides) = @_;
46 5 100       21 return $self->{Sides} if not defined $sides;
47              
48 2 50       5 ($sides >= 3) or die "Sides is too smaill.";
49 2         6 $self->{Sides} = $sides;
50             }
51             sub radius {
52 5     5 1 10 my ($self, $radius) = @_;
53 5 100       25 return $self->{Radius} if not defined $radius;
54 2         5 $self->{Radius} = $radius;
55             }
56             sub angle {
57 5     5 1 12 my ($self, $angle) = @_;
58 5 100       73 return $self->{Angle} if not defined $angle;
59 2         5 $self->{Angle} = $angle;
60             }
61              
62             sub points {
63 2     2 1 6 my ($self) = @_;
64              
65 2         2 my @points;
66              
67 2         4 my $rad = - pi / 2 + $self->{Angle}; # -90 degree
68 2         6 for (my $i = 0 ; $i < $self->{Sides} ; $i++) {
69 10         60 push @points, {x => cos($rad) * $self->{Radius} + $self->{CenterX},
70             y => sin($rad) * $self->{Radius} + $self->{CenterY}};
71              
72 10         26 $rad += pi * 2 / $self->{Sides};
73             }
74              
75 2         8 return @points;
76             }
77              
78             1;
79             __END__