| 1 | package SVG::Estimate::Polyline; | ||
| 2 | $SVG::Estimate::Polyline::VERSION = '1.0111'; | ||
| 3 | use Moo; | ||
| 4 | use Clone qw/clone/; | ||
| 5 | |||
| 6 | extends 'SVG::Estimate::Shape'; | ||
| 7 | 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 | 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 | my $self = shift; | ||
| 63 | return $self->parse_points($self->points); | ||
| 64 | }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub BUILDARGS { | ||
| 68 | my ($class, @args) = @_; | ||
| 69 | ##Upgrade to hashref | ||
| 70 | my $args = @args % 2 ? $args[0] : { @args }; | ||
| 71 | my $string = $args->{points}; | ||
| 72 | $string =~ s/^\s+|\s+$//g; | ||
| 73 | my @pairs = $class->_get_pairs($string); | ||
| 74 | my @points = (); | ||
| 75 | my ($min_x, $max_x, $min_y, $max_y) = (1e10, -1e10, 1e10, -1e10); | ||
| 76 | my $first = 1; | ||
| 77 | my $start = []; | ||
| 78 | my $length = 0; | ||
| 79 | PAIR: foreach my $pair (@pairs) { | ||
| 80 | my $point = [ split ',', $pair ]; | ||
| 81 | if ($args->{transformer}->has_transforms) { | ||
| 82 | $point = $args->{transformer}->transform($point); | ||
| 83 | } | ||
| 84 | $min_x = $point->[0] if $point->[0] < $min_x; | ||
| 85 | $max_x = $point->[0] if $point->[0] > $max_x; | ||
| 86 | $min_y = $point->[1] if $point->[1] < $min_y; | ||
| 87 | $max_y = $point->[1] if $point->[1] > $max_y; | ||
| 88 | push @points, $point; | ||
| 89 | if ($first) { | ||
| 90 | $first = 0; | ||
| 91 | $start = $point; | ||
| 92 | next PAIR; | ||
| 93 | } | ||
| 94 | $length += $class->pythagorean($start, $point); | ||
| 95 | $start = $point; | ||
| 96 | } | ||
| 97 | $args->{parsed_points} = \@points; | ||
| 98 | $args->{min_x} = $min_x; | ||
| 99 | $args->{max_x} = $max_x; | ||
| 100 | $args->{min_y} = $min_y; | ||
| 101 | $args->{max_y} = $max_y; | ||
| 102 | $args->{draw_start} = clone $points[0]; | ||
| 103 | $args->{draw_end} = clone $points[-1]; | ||
| 104 | $args->{shape_length} = $length; | ||
| 105 | 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 | my ($class, $string) = @_; | ||
| 112 | return split ' ', $string; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |