File Coverage

lib/SVG/Estimate/Circle.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 1 2 50.0
total 21 22 95.4


line stmt bran cond sub pod time code
1             package SVG::Estimate::Circle;
2             $SVG::Estimate::Circle::VERSION = '1.0107';
3 3     3   1210 use Moo;
  3         3  
  3         19  
4 3     3   705 use Math::Trig qw/pi/;
  3         3  
  3         1035  
5              
6             extends 'SVG::Estimate::Shape';
7             with 'SVG::Estimate::Role::MakePolygon';
8             with 'SVG::Estimate::Role::ArgsWithUnits';
9              
10             =head1 NAME
11              
12             SVG::Estimate::Circle - Handles estimating circles.
13              
14             =head1 VERSION
15              
16             version 1.0107
17              
18             =head1 SYNOPSIS
19              
20             my $circle = SVG::Estimate::Circle->new(
21             transformer => $transform,
22             start_point => [45,13],
23             cx => 1,
24             cy => 3,
25             r => 1,
26             );
27              
28             my $length = $circle->length;
29              
30             =head1 INHERITANCE
31              
32             This class extends L.
33              
34             =head1 METHODS
35              
36             =head2 new()
37              
38             Constructor.
39              
40             =over
41              
42             =item cx
43              
44             Float representing center x.
45              
46             =item cy
47              
48             Float representing center y.
49              
50             =item r
51              
52             Float representing the radius.
53              
54             =back
55              
56             =cut
57              
58             has cx => (
59             is => 'ro',
60             default => sub { 0 },
61             );
62              
63             has cy => (
64             is => 'ro',
65             default => sub { 0 },
66             );
67              
68             has r => (
69             is => 'ro',
70             required => 1,
71             );
72              
73             sub args_with_units {
74 4     4 0 8 return qw/r/;
75             }
76              
77             sub BUILDARGS {
78             my ($class, @args) = @_;
79             ##Upgrade to hashref
80             my $args = @args % 2 ? $args[0] : { @args };
81             my $center = [ $args->{cx}, $args->{cy} ];
82             if ($args->{transformer}->has_transforms) {
83             ##Approximate the circle with a polygon
84             my $poly = $class->make_polygon($args);
85             $args->{draw_start} = $poly->draw_start;
86             $args->{draw_end} = $poly->draw_end;
87             $args->{shape_length} = $poly->shape_length;
88             $args->{min_x} = $poly->min_x;
89             $args->{min_y} = $poly->min_y;
90             $args->{max_x} = $poly->max_x;
91             $args->{max_y} = $poly->max_y;
92             return $args;
93             }
94             $args->{draw_start} = [$args->{cx}+$args->{r}, $args->{cy}];
95             $args->{draw_end} = $args->{draw_start};
96             $args->{shape_length} = 2 * pi * $args->{r};
97             $args->{min_x} = $args->{cx} - $args->{r};
98             $args->{min_y} = $args->{cy} - $args->{r};
99             $args->{max_x} = $args->{cx} + $args->{r};
100             $args->{max_y} = $args->{cy} + $args->{r};
101             return $args;
102             }
103              
104             =head2 this_point ($args, $t)
105              
106             This class method is used to calculate a point on a circle, given it's relative position (C<$t>, ranging
107             from 0 to 1, inclusive), and the radius and center of the circle from a hashref of C<$args> (C and C and ).
108              
109             =cut
110              
111             sub this_point {
112 13     13 1 8 my $class = shift;
113 13         10 my $args = shift;
114 13         9 my $t = shift;
115 13         10 my $angle = $t * 2 * pi;
116 13         38 my $cosr = cos $angle;
117 13         12 my $sinr = sin $angle;
118 13         16 my $x = $cosr * $args->{r} + $args->{cx};
119 13         23 my $y = $sinr * $args->{r} + $args->{cy};
120 13         20 return [$x, $y];
121             }
122              
123             1;