| 1 | package SVG::Estimate::Circle; | ||
| 2 | $SVG::Estimate::Circle::VERSION = '1.0116'; | ||
| 3 | use Moo; | ||
| 4 | use Math::Trig qw/pi/; | ||
| 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.0116 | ||
| 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<SVG::Estimate::Shape>. | ||
| 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 | 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 | $args->{cx} //= 0; | ||
| 82 | $args->{cy} //= 0; | ||
| 83 | my $center = [ $args->{cx}, $args->{cy} ]; | ||
| 84 | if ($args->{transformer}->has_transforms) { | ||
| 85 | ##Approximate the circle with a polygon | ||
| 86 | my $poly = $class->make_polygon($args); | ||
| 87 | $args->{draw_start} = $poly->draw_start; | ||
| 88 | $args->{draw_end} = $poly->draw_end; | ||
| 89 | $args->{shape_length} = $poly->shape_length; | ||
| 90 | $args->{min_x} = $poly->min_x; | ||
| 91 | $args->{min_y} = $poly->min_y; | ||
| 92 | $args->{max_x} = $poly->max_x; | ||
| 93 | $args->{max_y} = $poly->max_y; | ||
| 94 | return $args; | ||
| 95 | } | ||
| 96 | $args->{draw_start} = [$args->{cx}+$args->{r}, $args->{cy}]; | ||
| 97 | $args->{draw_end} = $args->{draw_start}; | ||
| 98 | $args->{shape_length} = 2 * pi * $args->{r}; | ||
| 99 | $args->{min_x} = $args->{cx} - $args->{r}; | ||
| 100 | $args->{min_y} = $args->{cy} - $args->{r}; | ||
| 101 | $args->{max_x} = $args->{cx} + $args->{r}; | ||
| 102 | $args->{max_y} = $args->{cy} + $args->{r}; | ||
| 103 | return $args; | ||
| 104 | } | ||
| 105 | |||
| 106 | =head2 this_point ($args, $t) | ||
| 107 | |||
| 108 | This class method is used to calculate a point on a circle, given it's relative position (C<$t>, ranging | ||
| 109 | from 0 to 1, inclusive), and the radius and center of the circle from a hashref of C<$args> (C<r> and C<cx> and <cy>). | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub this_point { | ||
| 114 | my $class = shift; | ||
| 115 | my $args = shift; | ||
| 116 | my $t = shift; | ||
| 117 | my $angle = $t * 2 * pi; | ||
| 118 | my $cosr = cos $angle; | ||
| 119 | my $sinr = sin $angle; | ||
| 120 | my $x = $cosr * $args->{r} + $args->{cx}; | ||
| 121 | my $y = $sinr * $args->{r} + $args->{cy}; | ||
| 122 | return [$x, $y]; | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; |