| 1 | package SVG::Estimate::Path::Arc; | ||||||||||
| 2 | 4 | $SVG::Estimate::Path::Arc::VERSION = '1.0111'; | |||||||||
| 3 | 4 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use Math::Trig qw/pi acos deg2rad rad2deg/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use Clone qw/clone/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | extends 'SVG::Estimate::Path::Command'; | |||||||||
| 9 | 4 | with 'SVG::Estimate::Role::Pythagorean'; | |||||||||
| 10 | 4 | 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 | 4 | has rx => ( | |||||||||
| 81 | is => 'ro', | ||||||||||
| 82 | required => 1, | ||||||||||
| 83 | ); | ||||||||||
| 84 | |||||||||||
| 85 | 4 | has ry => ( | |||||||||
| 86 | is => 'ro', | ||||||||||
| 87 | required => 1, | ||||||||||
| 88 | ); | ||||||||||
| 89 | |||||||||||
| 90 | 4 | has x_axis_rotation => ( | |||||||||
| 91 | is => 'ro', | ||||||||||
| 92 | required => 1, | ||||||||||
| 93 | ); | ||||||||||
| 94 | |||||||||||
| 95 | 4 | has large_arc_flag => ( | |||||||||
| 96 | is => 'ro', | ||||||||||
| 97 | required => 1, | ||||||||||
| 98 | ); | ||||||||||
| 99 | |||||||||||
| 100 | 4 | has sweep_flag => ( | |||||||||
| 101 | is => 'ro', | ||||||||||
| 102 | required => 1, | ||||||||||
| 103 | ); | ||||||||||
| 104 | |||||||||||
| 105 | 4 | has x => ( | |||||||||
| 106 | is => 'ro', | ||||||||||
| 107 | required => 1, | ||||||||||
| 108 | ); | ||||||||||
| 109 | |||||||||||
| 110 | 4 | has y => ( | |||||||||
| 111 | is => 'ro', | ||||||||||
| 112 | required => 1, | ||||||||||
| 113 | ); | ||||||||||
| 114 | |||||||||||
| 115 | ##Used for conversion from endpoint to center parameterization | ||||||||||
| 116 | 4 | has _delta => ( | |||||||||
| 117 | is => 'rw', | ||||||||||
| 118 | ); | ||||||||||
| 119 | |||||||||||
| 120 | 4 | has _theta => ( | |||||||||
| 121 | is => 'rw', | ||||||||||
| 122 | ); | ||||||||||
| 123 | |||||||||||
| 124 | 4 | has _center => ( | |||||||||
| 125 | is => 'rw', | ||||||||||
| 126 | ); | ||||||||||
| 127 | |||||||||||
| 128 | sub BUILDARGS { | ||||||||||
| 129 | 2 | ▶ | my ($class, @args) = @_; | ||||||||
|
Subroutine
BUILDARGS: called CC=13 SCAR=26.0
Pod
undocumented
| |||||||||||
| 130 | ##Upgrade to hashref | ||||||||||
| 131 | 2 | ▶ | my $args = @args % 2 ? $args[0] : { @args }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 2 | ▶ | if ($args->{transformer}->has_transforms) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 1 | my $view_start_point = clone $args->{start_point}; | |||||||||
| 137 | 1 | $args->{start_point} = $args->{transformer}->untransform($args->{start_point}); | |||||||||
| 138 | 1 | $class->endpoint_to_center($args); | |||||||||
| 139 | 1 | my $point; | |||||||||
| 140 | 1 | my $first = 1; | |||||||||
| 141 | 1 | my $start; | |||||||||
| 142 | 1 | my $length = 0; | |||||||||
| 143 | 1 | ▶ | POINT: for (my $t=0; $t<=1; $t+=1/12) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 13 | $point = $class->this_point($args, $t); | |||||||||
| 145 | 13 | $point = $args->{transformer}->transform($point); | |||||||||
| 146 | 13 | ▶ | if ($first) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 1 | $first = 0; | |||||||||
| 148 | 1 | $start = $point; | |||||||||
| 149 | 1 | $args->{min_x} = $args->{max_x} = $point->[0]; | |||||||||
| 150 | 1 | $args->{min_y} = $args->{max_y} = $point->[1]; | |||||||||
| 151 | 1 | next POINT; | |||||||||
| 152 | } | ||||||||||
| 153 | 12 | $length += $class->pythagorean($start, $point); | |||||||||
| 154 | 12 | ▶ | $args->{min_x} = $point->[0] if $point->[0] < $args->{min_x}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 12 | ▶ | $args->{min_y} = $point->[1] if $point->[1] < $args->{min_y}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | 12 | ▶ | $args->{max_x} = $point->[0] if $point->[0] > $args->{max_x}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 12 | ▶ | $args->{max_y} = $point->[1] if $point->[1] > $args->{max_y}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | 12 | $start = $point; | |||||||||
| 159 | } | ||||||||||
| 160 | ##Restore the original start point in the viewport coordinate system | ||||||||||
| 161 | 1 | $args->{start_point} = $view_start_point; | |||||||||
| 162 | 1 | $args->{end_point} = $point; | |||||||||
| 163 | 1 | $args->{shape_length} = $length; | |||||||||
| 164 | 1 | $args->{travel_length} = 0; | |||||||||
| 165 | 1 | return $args; | |||||||||
| 166 | } | ||||||||||
| 167 | 1 | $class->endpoint_to_center($args); | |||||||||
| 168 | 1 | my $start = $class->this_point($args, 0); | |||||||||
| 169 | 1 | my $end = $class->this_point($args, 1); | |||||||||
| 170 | 1 | ▶ | $args->{min_x} = $start->[0] < $end->[0] ? $start->[0] : $end->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | 1 | ▶ | $args->{max_x} = $start->[0] > $end->[0] ? $start->[0] : $end->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 1 | ▶ | $args->{min_y} = $start->[1] < $end->[1] ? $start->[1] : $end->[1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 1 | ▶ | $args->{max_y} = $start->[1] > $end->[1] ? $start->[1] : $end->[1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 1 | $args->{shape_length} = $class->segment_length($args, 0, 1, $start, $end, 1e-4, 5, 0); | |||||||||
| 175 | 1 | $args->{travel_length} = 0; | |||||||||
| 176 | 1 | $args->{end_point} = clone $end; | |||||||||
| 177 | 1 | return $args; | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | sub endpoint_to_center { | ||||||||||
| 181 | 2 | ▶ | my $class = shift; | ||||||||
|
Subroutine
endpoint_to_center: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 182 | 2 | my $args = shift; | |||||||||
| 183 | 2 | my $rotr = deg2rad($args->{x_axis_rotation}); | |||||||||
| 184 | 2 | my $cosr = cos $rotr; | |||||||||
| 185 | 2 | my $sinr = sin $rotr; | |||||||||
| 186 | 2 | my $dx = ($args->{start_point}->[0] - $args->{x}) / 2; #* | |||||||||
| 187 | 2 | my $dy = ($args->{start_point}->[1] - $args->{y}) / 2; #* | |||||||||
| 188 | |||||||||||
| 189 | 2 | my $x1prim = $cosr * $dx + $sinr * $dy; #* | |||||||||
| 190 | 2 | my $y1prim = -1*$sinr * $dx + $cosr * $dy; #* | |||||||||
| 191 | |||||||||||
| 192 | 2 | my $x1prim_sq = $x1prim**2; #* | |||||||||
| 193 | 2 | my $y1prim_sq = $y1prim**2; #* | |||||||||
| 194 | |||||||||||
| 195 | 2 | my $rx = $args->{rx}; #* | |||||||||
| 196 | 2 | my $ry = $args->{ry}; #* | |||||||||
| 197 | |||||||||||
| 198 | 2 | my $rx_sq = $rx**2; #* | |||||||||
| 199 | 2 | my $ry_sq = $ry**2; #* | |||||||||
| 200 | |||||||||||
| 201 | 2 | my $t1 = $rx_sq * $y1prim_sq; | |||||||||
| 202 | 2 | my $t2 = $ry_sq * $x1prim_sq; | |||||||||
| 203 | 2 | my $ts = $t1 + $t2; | |||||||||
| 204 | 2 | my $c = sqrt(abs( (($rx_sq * $ry_sq) - $ts) / ($ts) ) ); | |||||||||
| 205 | |||||||||||
| 206 | 2 | ▶ | if ($args->{large_arc_flag} == $args->{sweep_flag}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 207 | 2 | $c *= -1; | |||||||||
| 208 | } | ||||||||||
| 209 | 2 | my $cxprim = $c * $rx * $y1prim / $ry; | |||||||||
| 210 | 2 | my $cyprim = -1 *$c * $ry * $x1prim / $rx; | |||||||||
| 211 | |||||||||||
| 212 | $args->{_center} = [ | ||||||||||
| 213 | ($cosr * $cxprim - $sinr * $cyprim) + ( ($args->{start_point}->[0] + $args->{x}) / 2 ), | ||||||||||
| 214 | 2 | ($sinr * $cxprim + $cosr * $cyprim) + ( ($args->{start_point}->[1] + $args->{y}) / 2 ) | |||||||||
| 215 | ]; | ||||||||||
| 216 | |||||||||||
| 217 | ##** | ||||||||||
| 218 | |||||||||||
| 219 | ##Theta calculation | ||||||||||
| 220 | 2 | my $ux = ($x1prim - $cxprim) / $rx; #* | |||||||||
| 221 | 2 | my $uy = ($y1prim - $cyprim) / $ry; #* | |||||||||
| 222 | 2 | my $n = sqrt($ux**2 + $uy**2); | |||||||||
| 223 | 2 | my $p = $ux; | |||||||||
| 224 | 2 | my $d = $p / $n; | |||||||||
| 225 | 2 | my $theta = rad2deg(acos($p/$n)); | |||||||||
| 226 | 2 | ▶ | if ($uy < 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 227 | 2 | $theta *= -1; | |||||||||
| 228 | } | ||||||||||
| 229 | 2 | $args->{_theta} = $theta % 360; | |||||||||
| 230 | |||||||||||
| 231 | 2 | my $vx = -1 * ($x1prim + $cxprim) / $rx; | |||||||||
| 232 | 2 | my $vy = -1 * ($y1prim + $cyprim) / $ry; | |||||||||
| 233 | 2 | $n = sqrt( ($ux**2 + $uy**2) * ($vx**2 + $vy**2)); | |||||||||
| 234 | 2 | $p = $ux*$vx + $uy*$vy; | |||||||||
| 235 | 2 | $d = $p / $n; | |||||||||
| 236 | |||||||||||
| 237 | 2 | my $delta = rad2deg(acos($d)); | |||||||||
| 238 | 2 | ▶ | if (($ux * $vy - $uy * $vx) < 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 239 | 2 | $delta *= -1; | |||||||||
| 240 | } | ||||||||||
| 241 | 2 | $delta = $delta % 360; | |||||||||
| 242 | |||||||||||
| 243 | 2 | ▶ | if (! $args->{sweep_flag}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 244 | 2 | $delta -= 360; | |||||||||
| 245 | } | ||||||||||
| 246 | 2 | $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 | 142 | ▶ | my $class = shift; | ||||||||
|
Subroutine
this_point: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 257 | 142 | my $args = shift; | |||||||||
| 258 | 142 | my $t = shift; | |||||||||
| 259 | 142 | my $angle = deg2rad($args->{_theta} + ($args->{_delta} * $t)); | |||||||||
| 260 | 142 | my $rotr = deg2rad($args->{x_axis_rotation}); | |||||||||
| 261 | 142 | my $cosr = cos $rotr; | |||||||||
| 262 | 142 | my $sinr = sin $rotr; | |||||||||
| 263 | 142 | my $x = ($cosr * cos($angle) * $args->{rx} - $sinr * sin($angle) * $args->{ry} + $args->{_center}->[0]); | |||||||||
| 264 | 142 | my $y = ($sinr * cos($angle) * $args->{rx} + $cosr * sin($angle) * $args->{ry} + $args->{_center}->[1]); | |||||||||
| 265 | 142 | return [$x, $y]; | |||||||||
| 266 | } | ||||||||||
| 267 | |||||||||||
| 268 | 4 | 1; | |||||||||