blib/lib/SVG/Estimate/Path/Arc.pm untested

filter: Statementstmt 0.0% 0 / 134 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 174 CC 16 SCAR 45.3
CC
16
Cov
0%
BUILDARGS
156.0
endpoint_to_center
30.0
this_point
2.0
CRAP 92.4
1 package SVG::Estimate::Path::Arc;
2 $SVG::Estimate::Path::Arc::VERSION = '1.0111';
3 use Moo;
4 use Math::Trig qw/pi acos deg2rad rad2deg/;
5 use Clone qw/clone/;
6 use strict;
7  
8 extends 'SVG::Estimate::Path::Command';
9 with 'SVG::Estimate::Role::Pythagorean';
10 with 'SVG::Estimate::Role::SegmentLength';
11  
12 =head1 NAME
13  
14 SVG::Estimate::Path::Arc - Handles estimating arcs.
15  
16 =head1 VERSION
17  
18 version 1.0111
19  
20 =head1 SYNOPSIS
21  
22 my $arc = SVG::Estimate::Path::Arc->new(
23 transformer => $transform,
24 start_point => [13, 19],
25 x => 45,
26 y => 13,
27 rx => 1,
28 ry => 3,
29 x_axis_rotation => 0,
30 large_arc_flag => 0,
31 sweep_flag => 0,
32 );
33  
34 my $length = $arc->length;
35  
36 =head1 INHERITANCE
37  
38 This class extends L<SVG::Estimate::Path::Command> and consumes L<SVG::Estimate::Role::Pythagorean>, L<SVG::Estimate::Role::SegmentLength>, and L<SVG::Estimate::Role::EndToPoint>.
39  
40 =head1 METHODS
41  
42 =head2 new()
43  
44 Constructor.
45  
46 =over
47  
48 =item x
49  
50 The x coordinate for the end-point of the arc.
51  
52 =item y
53  
54 The y coordinate for the end-point of the arc.
55  
56 =item rx
57  
58 Float representing the x radius.
59  
60 =item ry
61  
62 Float representing the y radius.
63  
64 =item x_axis_rotation
65  
66 Float that indicates how the ellipse as a whole is rotated relative to the current coordinate system.
67  
68 =item large_arc_flag
69  
70 Must be 1 or 0. See details L<http://www.w3.org/TR/SVG/paths.html>.
71  
72 =item sweep_flag
73  
74 Must be 1 or 0. See details L<http://www.w3.org/TR/SVG/paths.html>.
75  
76 =back
77  
78 =cut
79  
80 has rx => (
81 is => 'ro',
82 required => 1,
83 );
84  
85 has ry => (
86 is => 'ro',
87 required => 1,
88 );
89  
90 has x_axis_rotation => (
91 is => 'ro',
92 required => 1,
93 );
94  
95 has large_arc_flag => (
96 is => 'ro',
97 required => 1,
98 );
99  
100 has sweep_flag => (
101 is => 'ro',
102 required => 1,
103 );
104  
105 has x => (
106 is => 'ro',
107 required => 1,
108 );
109  
110 has y => (
111 is => 'ro',
112 required => 1,
113 );
114  
115 ##Used for conversion from endpoint to center parameterization
116 has _delta => (
117 is => 'rw',
118 );
119  
120 has _theta => (
121 is => 'rw',
122 );
123  
124 has _center => (
125 is => 'rw',
126 );
127  
128 sub BUILDARGS {
129 my ($class, @args) = @_;
130 ##Upgrade to hashref
131 my $args = @args % 2 ? $args[0] : { @args };
132 if ($args->{transformer}->has_transforms) {
133 ##The start point and end point are in different coordinate systems (view and user, respectively).
134 ##To make the set of point in the user space, transform the start_point into user space
135 ##Then run all the calculations
136 my $view_start_point = clone $args->{start_point};
137 $args->{start_point} = $args->{transformer}->untransform($args->{start_point});
138 $class->endpoint_to_center($args);
139 my $point;
140 my $first = 1;
141 my $start;
142 my $length = 0;
143 POINT: for (my $t=0; $t<=1; $t+=1/12) {
144 $point = $class->this_point($args, $t);
145 $point = $args->{transformer}->transform($point);
146 if ($first) {
147 $first = 0;
148 $start = $point;
149 $args->{min_x} = $args->{max_x} = $point->[0];
150 $args->{min_y} = $args->{max_y} = $point->[1];
151 next POINT;
152 }
153 $length += $class->pythagorean($start, $point);
154 $args->{min_x} = $point->[0] if $point->[0] < $args->{min_x};
155 $args->{min_y} = $point->[1] if $point->[1] < $args->{min_y};
156 $args->{max_x} = $point->[0] if $point->[0] > $args->{max_x};
157 $args->{max_y} = $point->[1] if $point->[1] > $args->{max_y};
158 $start = $point;
159 }
160 ##Restore the original start point in the viewport coordinate system
161 $args->{start_point} = $view_start_point;
162 $args->{end_point} = $point;
163 $args->{shape_length} = $length;
164 $args->{travel_length} = 0;
165 return $args;
166 }
167 $class->endpoint_to_center($args);
168 my $start = $class->this_point($args, 0);
169 my $end = $class->this_point($args, 1);
170 $args->{min_x} = $start->[0] < $end->[0] ? $start->[0] : $end->[0];
171 $args->{max_x} = $start->[0] > $end->[0] ? $start->[0] : $end->[0];
172 $args->{min_y} = $start->[1] < $end->[1] ? $start->[1] : $end->[1];
173 $args->{max_y} = $start->[1] > $end->[1] ? $start->[1] : $end->[1];
174 $args->{shape_length} = $class->segment_length($args, 0, 1, $start, $end, 1e-4, 5, 0);
175 $args->{travel_length} = 0;
176 $args->{end_point} = clone $end;
177 return $args;
178 }
179  
180 sub endpoint_to_center {
181 my $class = shift;
182 my $args = shift;
183 my $rotr = deg2rad($args->{x_axis_rotation});
184 my $cosr = cos $rotr;
185 my $sinr = sin $rotr;
186 my $dx = ($args->{start_point}->[0] - $args->{x}) / 2; #*
187 my $dy = ($args->{start_point}->[1] - $args->{y}) / 2; #*
188  
189 my $x1prim = $cosr * $dx + $sinr * $dy; #*
190 my $y1prim = -1*$sinr * $dx + $cosr * $dy; #*
191  
192 my $x1prim_sq = $x1prim**2; #*
193 my $y1prim_sq = $y1prim**2; #*
194  
195 my $rx = $args->{rx}; #*
196 my $ry = $args->{ry}; #*
197  
198 my $rx_sq = $rx**2; #*
199 my $ry_sq = $ry**2; #*
200  
201 my $t1 = $rx_sq * $y1prim_sq;
202 my $t2 = $ry_sq * $x1prim_sq;
203 my $ts = $t1 + $t2;
204 my $c = sqrt(abs( (($rx_sq * $ry_sq) - $ts) / ($ts) ) );
205  
206 if ($args->{large_arc_flag} == $args->{sweep_flag}) {
207 $c *= -1;
208 }
209 my $cxprim = $c * $rx * $y1prim / $ry;
210 my $cyprim = -1 *$c * $ry * $x1prim / $rx;
211  
212 $args->{_center} = [
213 ($cosr * $cxprim - $sinr * $cyprim) + ( ($args->{start_point}->[0] + $args->{x}) / 2 ),
214 ($sinr * $cxprim + $cosr * $cyprim) + ( ($args->{start_point}->[1] + $args->{y}) / 2 )
215 ];
216  
217 ##**
218  
219 ##Theta calculation
220 my $ux = ($x1prim - $cxprim) / $rx; #*
221 my $uy = ($y1prim - $cyprim) / $ry; #*
222 my $n = sqrt($ux**2 + $uy**2);
223 my $p = $ux;
224 my $d = $p / $n;
225 my $theta = rad2deg(acos($p/$n));
226 if ($uy < 0) {
227 $theta *= -1;
228 }
229 $args->{_theta} = $theta % 360;
230  
231 my $vx = -1 * ($x1prim + $cxprim) / $rx;
232 my $vy = -1 * ($y1prim + $cyprim) / $ry;
233 $n = sqrt( ($ux**2 + $uy**2) * ($vx**2 + $vy**2));
234 $p = $ux*$vx + $uy*$vy;
235 $d = $p / $n;
236  
237 my $delta = rad2deg(acos($d));
238 if (($ux * $vy - $uy * $vx) < 0 ) {
239 $delta *= -1;
240 }
241 $delta = $delta % 360;
242  
243 if (! $args->{sweep_flag}) {
244 $delta -= 360;
245 }
246 $args->{_delta} = $delta;
247 }
248  
249 =head2 this_point (args, t)
250  
251 Calculate a point on the graph, normalized from start point to end point as t, in 2-D space
252  
253 =cut
254  
255 sub this_point {
256 my $class = shift;
257 my $args = shift;
258 my $t = shift;
259 my $angle = deg2rad($args->{_theta} + ($args->{_delta} * $t));
260 my $rotr = deg2rad($args->{x_axis_rotation});
261 my $cosr = cos $rotr;
262 my $sinr = sin $rotr;
263 my $x = ($cosr * cos($angle) * $args->{rx} - $sinr * sin($angle) * $args->{ry} + $args->{_center}->[0]);
264 my $y = ($sinr * cos($angle) * $args->{rx} + $cosr * sin($angle) * $args->{ry} + $args->{_center}->[1]);
265 return [$x, $y];
266 }
267  
268 1;