| 1 | package SVG::Estimate::Polyline; | ||||||||||
| 2 | 10 | $SVG::Estimate::Polyline::VERSION = '1.0111'; | |||||||||
| 3 | 10 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 10 | ▶ | use Clone qw/clone/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 10 | extends 'SVG::Estimate::Shape'; | |||||||||
| 7 | 10 | with 'SVG::Estimate::Role::Pythagorean'; | |||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | SVG::Estimate::Polyline - Handles estimating multi-part lines. | ||||||||||
| 12 | |||||||||||
| 13 | =head1 VERSION | ||||||||||
| 14 | |||||||||||
| 15 | version 1.0111 | ||||||||||
| 16 | |||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||
| 18 | |||||||||||
| 19 | my $line = SVG::Estimate::Polyline->new( | ||||||||||
| 20 | transformer => $transform, | ||||||||||
| 21 | start_point => [45,13], | ||||||||||
| 22 | points => '20,20 40,25 60,40 80,120 120,140 200,180', | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | my $length = $line->length; | ||||||||||
| 26 | |||||||||||
| 27 | =head1 INHERITANCE | ||||||||||
| 28 | |||||||||||
| 29 | This class extends L<SVG::Estimate::Shape> and consumes L<SVG::Estimate::Role::Pythagorean>. | ||||||||||
| 30 | |||||||||||
| 31 | =head1 METHODS | ||||||||||
| 32 | |||||||||||
| 33 | =head2 new() | ||||||||||
| 34 | |||||||||||
| 35 | Constructor. | ||||||||||
| 36 | |||||||||||
| 37 | =over | ||||||||||
| 38 | |||||||||||
| 39 | =item points | ||||||||||
| 40 | |||||||||||
| 41 | A string listing points for the polyline as defined by L<http://www.w3.org/TR/SVG/shapes.html>. | ||||||||||
| 42 | |||||||||||
| 43 | =back | ||||||||||
| 44 | |||||||||||
| 45 | =cut | ||||||||||
| 46 | |||||||||||
| 47 | 10 | has points => ( | |||||||||
| 48 | is => 'ro', | ||||||||||
| 49 | required => 1, | ||||||||||
| 50 | ); | ||||||||||
| 51 | |||||||||||
| 52 | =head2 parsed_points() | ||||||||||
| 53 | |||||||||||
| 54 | Returns an array reference of array references marking the parsed C<points> string. | ||||||||||
| 55 | |||||||||||
| 56 | =cut | ||||||||||
| 57 | |||||||||||
| 58 | has parsed_points => ( | ||||||||||
| 59 | is => 'ro', | ||||||||||
| 60 | lazy => 1, | ||||||||||
| 61 | default => sub { | ||||||||||
| 62 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 63 | 0 | return $self->parse_points($self->points); | |||||||||
| 64 | }, | ||||||||||
| 65 | 10 | ); | |||||||||
| 66 | |||||||||||
| 67 | sub BUILDARGS { | ||||||||||
| 68 | 6 | ▶ | my ($class, @args) = @_; | ||||||||
|
Subroutine
BUILDARGS: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||
| 69 | ##Upgrade to hashref | ||||||||||
| 70 | 6 | ▶ | my $args = @args % 2 ? $args[0] : { @args }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 6 | my $string = $args->{points}; | |||||||||
| 72 | 6 | $string =~ s/^\s+|\s+$//g; | |||||||||
| 73 | 6 | my @pairs = $class->_get_pairs($string); | |||||||||
| 74 | 6 | my @points = (); | |||||||||
| 75 | 6 | my ($min_x, $max_x, $min_y, $max_y) = (1e10, -1e10, 1e10, -1e10); | |||||||||
| 76 | 6 | my $first = 1; | |||||||||
| 77 | 6 | my $start = []; | |||||||||
| 78 | 6 | my $length = 0; | |||||||||
| 79 | 6 | PAIR: foreach my $pair (@pairs) { | |||||||||
| 80 | 68 | my $point = [ split ',', $pair ]; | |||||||||
| 81 | 68 | ▶ | if ($args->{transformer}->has_transforms) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 20 | $point = $args->{transformer}->transform($point); | |||||||||
| 83 | } | ||||||||||
| 84 | 68 | ▶ | $min_x = $point->[0] if $point->[0] < $min_x; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 68 | ▶ | $max_x = $point->[0] if $point->[0] > $max_x; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 68 | ▶ | $min_y = $point->[1] if $point->[1] < $min_y; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 68 | ▶ | $max_y = $point->[1] if $point->[1] > $max_y; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 68 | push @points, $point; | |||||||||
| 89 | 68 | ▶ | if ($first) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 6 | $first = 0; | |||||||||
| 91 | 6 | $start = $point; | |||||||||
| 92 | 6 | next PAIR; | |||||||||
| 93 | } | ||||||||||
| 94 | 62 | $length += $class->pythagorean($start, $point); | |||||||||
| 95 | 62 | $start = $point; | |||||||||
| 96 | } | ||||||||||
| 97 | 6 | $args->{parsed_points} = \@points; | |||||||||
| 98 | 6 | $args->{min_x} = $min_x; | |||||||||
| 99 | 6 | $args->{max_x} = $max_x; | |||||||||
| 100 | 6 | $args->{min_y} = $min_y; | |||||||||
| 101 | 6 | $args->{max_y} = $max_y; | |||||||||
| 102 | 6 | $args->{draw_start} = clone $points[0]; | |||||||||
| 103 | 6 | $args->{draw_end} = clone $points[-1]; | |||||||||
| 104 | 6 | $args->{shape_length} = $length; | |||||||||
| 105 | 6 | return $args; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | ##This method is here so that Polygon can wrap it to add a closing point. | ||||||||||
| 109 | |||||||||||
| 110 | sub _get_pairs { | ||||||||||
| 111 | 6 | ▶ | my ($class, $string) = @_; | ||||||||
|
Subroutine
_get_pairs: called CC=1 SCAR=0.0
| |||||||||||
| 112 | 6 | return split ' ', $string; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 10 | 1; | |||||||||