File Coverage

lib/SVG/Estimate/Polyline.pm
Criterion Covered Total %
statement 42 42 100.0
branch 13 14 92.8
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 59 61 96.7


line stmt bran cond sub pod time code
1             package SVG::Estimate::Polyline;
2             $SVG::Estimate::Polyline::VERSION = '1.0107';
3 10     10   3169 use Moo;
  10         14  
  10         45  
4 10     10   2532 use Clone qw/clone/;
  10         3752  
  10         3688  
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.0107
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 and consumes L.
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.
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 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 6     6 0 14382 my ($class, @args) = @_;
69             ##Upgrade to hashref
70 6 50       62 my $args = @args % 2 ? $args[0] : { @args };
71 6         11 my $string = $args->{points};
72 6         59 $string =~ s/^\s+|\s+$//g;
73 6         75 my @pairs = $class->_get_pairs($string);
74 6         8 my @points = ();
75 6         8 my ($min_x, $max_x, $min_y, $max_y) = (1e10, -1e10, 1e10, -1e10);
76 6         7 my $first = 1;
77 6         9 my $start = [];
78 6         5 my $length = 0;
79 6         11 PAIR: foreach my $pair (@pairs) {
80 68         121 my $point = [ split ',', $pair ];
81 68 100       162 if ($args->{transformer}->has_transforms) {
82 20         39 $point = $args->{transformer}->transform($point);
83             }
84 68 100       2711 $min_x = $point->[0] if $point->[0] < $min_x;
85 68 100       93 $max_x = $point->[0] if $point->[0] > $max_x;
86 68 100       102 $min_y = $point->[1] if $point->[1] < $min_y;
87 68 100       83 $max_y = $point->[1] if $point->[1] > $max_y;
88 68         61 push @points, $point;
89 68 100       79 if ($first) {
90 6         7 $first = 0;
91 6         5 $start = $point;
92 6         16 next PAIR;
93             }
94 62         109 $length += $class->pythagorean($start, $point);
95 62         52 $start = $point;
96             }
97 6         11 $args->{parsed_points} = \@points;
98 6         8 $args->{min_x} = $min_x;
99 6         10 $args->{max_x} = $max_x;
100 6         6 $args->{min_y} = $min_y;
101 6         10 $args->{max_y} = $max_y;
102 6         50 $args->{draw_start} = clone $points[0];
103 6         27 $args->{draw_end} = clone $points[-1];
104 6         6 $args->{shape_length} = $length;
105 6         116 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     6   9 my ($class, $string) = @_;
112 6         39 return split ' ', $string;
113             }
114              
115             1;