| 1 | package SVG::Estimate::Path::QuadraticBezier; | ||||||||||
| 2 | 5 | $SVG::Estimate::Path::QuadraticBezier::VERSION = '1.0116'; | |||||||||
| 3 | 5 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use List::Util qw/min max/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use Clone qw/clone/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 5 | extends 'SVG::Estimate::Path::Command'; | |||||||||
| 8 | 5 | with 'SVG::Estimate::Role::Pythagorean'; | |||||||||
| 9 | 5 | with 'SVG::Estimate::Role::SegmentLength'; | |||||||||
| 10 | 5 | with 'SVG::Estimate::Role::EndToPoint'; | |||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | SVG::Estimate::Path::QuadraticBezier - Handles estimating quadratic bezier curves. | ||||||||||
| 15 | |||||||||||
| 16 | =head1 VERSION | ||||||||||
| 17 | |||||||||||
| 18 | version 1.0116 | ||||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | my $curve = SVG::Estimate::Path::QuadraticBezier->new( | ||||||||||
| 23 | transformer => $transform, | ||||||||||
| 24 | start_point => [13, 19], | ||||||||||
| 25 | point => [45,13], | ||||||||||
| 26 | control => [10,3], | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | my $length = $curve->length; | ||||||||||
| 30 | |||||||||||
| 31 | =head1 INHERITANCE | ||||||||||
| 32 | |||||||||||
| 33 | This class extends L<SVG::Estimate::Path::Command> and consumes L<SVG::Estimate::Role::EndToPoint> and L<SVG::Estimate::Role::SegmentLength> | ||||||||||
| 34 | |||||||||||
| 35 | =head1 METHODS | ||||||||||
| 36 | |||||||||||
| 37 | =head2 new() | ||||||||||
| 38 | |||||||||||
| 39 | Constructor. | ||||||||||
| 40 | |||||||||||
| 41 | =over | ||||||||||
| 42 | |||||||||||
| 43 | =item point | ||||||||||
| 44 | |||||||||||
| 45 | An array ref containing two floats that represent a point. | ||||||||||
| 46 | |||||||||||
| 47 | =item control | ||||||||||
| 48 | |||||||||||
| 49 | An array ref containing two floats that represent a point. | ||||||||||
| 50 | |||||||||||
| 51 | =back | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | 5 | has point => ( | |||||||||
| 56 | is => 'ro', | ||||||||||
| 57 | required => 1, | ||||||||||
| 58 | ); | ||||||||||
| 59 | |||||||||||
| 60 | 5 | has control => ( | |||||||||
| 61 | is => 'ro', | ||||||||||
| 62 | required => 1, | ||||||||||
| 63 | ); | ||||||||||
| 64 | |||||||||||
| 65 | sub BUILDARGS { | ||||||||||
| 66 | 3 | ▶ | my ($class, @args) = @_; | ||||||||
|
Subroutine
BUILDARGS: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 67 | ##Upgrade to hashref | ||||||||||
| 68 | 3 | ▶ | my $args = @args % 2 ? $args[0] : { @args }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 3 | ▶ | if ($args->{transformer}->has_transforms) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 1 | $args->{point} = $args->{transformer}->transform($args->{point}); | |||||||||
| 71 | 1 | $args->{control} = $args->{transformer}->transform($args->{control}); | |||||||||
| 72 | } | ||||||||||
| 73 | 3 | $args->{end_point} = clone $args->{point}; | |||||||||
| 74 | #$args->{shape_length} = $class->_calculate_length($args); | ||||||||||
| 75 | 3 | my $start = $class->this_point($args, 0); | |||||||||
| 76 | 3 | my $end = $class->this_point($args, 1); | |||||||||
| 77 | ##Bounding box points approximated by the control points. | ||||||||||
| 78 | 3 | $args->{min_x} = min $args->{start_point}->[0], $args->{control}->[0], $args->{point}->[0]; | |||||||||
| 79 | 3 | $args->{max_x} = max $args->{start_point}->[0], $args->{control}->[0], $args->{point}->[0]; | |||||||||
| 80 | 3 | $args->{min_y} = min $args->{start_point}->[1], $args->{control}->[1], $args->{point}->[1]; | |||||||||
| 81 | 3 | $args->{max_y} = max $args->{start_point}->[1], $args->{control}->[1], $args->{point}->[1]; | |||||||||
| 82 | |||||||||||
| 83 | 3 | $args->{shape_length} = $class->segment_length($args, 0, 1, $start, $end, 1e-4, 7, 0); | |||||||||
| 84 | 3 | $args->{travel_length} = 0; | |||||||||
| 85 | |||||||||||
| 86 | 3 | return $args; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | sub _calculate_length { | ||||||||||
| 90 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
_calculate_length: not called CC=1 SCAR=6.9
| |||||||||||
| 91 | 0 | my $args = shift; | |||||||||
| 92 | 0 | my $start = $args->{start_point}; | |||||||||
| 93 | 0 | my $control = $args->{control}; | |||||||||
| 94 | 0 | my $end = $args->{point}; | |||||||||
| 95 | |||||||||||
| 96 | ##http://www.malczak.info/blog/quadratic-bezier-curve-length/ | ||||||||||
| 97 | 0 | my $a_x = $start->[0] - 2 * $control->[0] + $end->[0]; | |||||||||
| 98 | 0 | my $a_y = $start->[1] - 2 * $control->[1] + $end->[1]; | |||||||||
| 99 | 0 | my $b_x = 2 * ($end->[0] - $start->[0]); | |||||||||
| 100 | 0 | my $b_y = 2 * ($end->[1] - $start->[1]); | |||||||||
| 101 | |||||||||||
| 102 | 0 | my $A = 4 * ($a_x**2 + $a_y**2); | |||||||||
| 103 | 0 | my $B = 4 * ($a_x*$b_x + $a_y*$b_y); | |||||||||
| 104 | 0 | my $C = $b_x**2 + $b_y**2; | |||||||||
| 105 | |||||||||||
| 106 | 0 | my $SABC = 2 * sqrt($A + $B +$C); | |||||||||
| 107 | 0 | my $SA = sqrt($A); | |||||||||
| 108 | 0 | my $A32 = 2 * $A * $SA; | |||||||||
| 109 | 0 | my $SC = 2*sqrt($C); | |||||||||
| 110 | 0 | my $BA = $B / $SA; | |||||||||
| 111 | |||||||||||
| 112 | 0 | my $length = ( $A32 + $SA*$B*($SABC-$SC) + (4*$C*$A - $B*$B)*log( (2*$SA + $BA + $SABC)/($BA + $SC) ) ) / (4*($A32)); | |||||||||
| 113 | 0 | return $length; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | sub this_point { | ||||||||||
| 117 | 773 | ▶ | my $class = shift; | ||||||||
|
Subroutine
this_point: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 118 | 773 | my $args = shift; | |||||||||
| 119 | 773 | my $t = shift; | |||||||||
| 120 | return [ | ||||||||||
| 121 | $class->_this_point($t, $args->{start_point}->[0], $args->{control}->[0], $args->{point}->[0]), | ||||||||||
| 122 | 773 | $class->_this_point($t, $args->{start_point}->[1], $args->{control}->[1], $args->{point}->[1]) | |||||||||
| 123 | ]; | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | sub _this_point { | ||||||||||
| 127 | 1546 | ▶ | shift; | ||||||||
|
Subroutine
_this_point: called CC=1 SCAR=0.0
| |||||||||||
| 128 | 1546 | my ($t, $s, $c, $p) = @_; | |||||||||
| 129 | 1546 | return ((1 - $t)**2 * $s) | |||||||||
| 130 | + (2*(1 - $t)*$t*$c) | ||||||||||
| 131 | + ($t**2 * $p) | ||||||||||
| 132 | ; | ||||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 5 | 1; | |||||||||