lib/Graphics/Penplotter/GcodeXY/Geometry2D.pm

filter: Statementstmt 6.2% 50 / 804 Branchbran 0.0% 0 / 284 Conditioncond 0.0% 0 / 72 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 13.8% 10 / 72 Podpod 0.0% 0 / 37 totaltotal 4.5% 60 / 1311 CC 198 SCAR 56.9
CC
198
Cov
4%
_recbezier4
1482.0
_LiangBarsky
506.0
polygon_clip
272.0
CRAP 296.8
« lib/Graphics/Penplotter/GcodeXY/Split.pm ↑ Summary lib/Graphics/Penplotter/GcodeXY/Hatch.pm »
1 package Graphics::Penplotter::GcodeXY::Geometry2D v0.9.4;
2  
3 9 ▶use v5.38.2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use feature qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶no warnings qw( experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Readonly qw( Readonly );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Math::Trig qw( acos tan );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Math::Bezier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use POSIX qw( ceil );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ---------------------------------------------------------------------------
15 # Graphics::Penplotter::GcodeXY::Geometry2D
16 # Role providing all geometric primitives, coordinate transformations, and
17 # line-clipping for GcodeXY.
18 # ---------------------------------------------------------------------------
19  
20 9 requires qw(penup pendown _genfastmove _genslowmove stroke _croak _penlock _penunlock);
21  
22 # ---------------------------------------------------------------------------
23 # Constants -- private copies of values defined in GcodeXY.pm.
24 # ---------------------------------------------------------------------------
25  
26 9 Readonly my $D2R => 0.01745329251; # degrees to radians
27 9 Readonly my $R2D => 57.2957795130; # radians to degrees
28 9 Readonly my $PI => 3.14159265359;
29 9 Readonly my $HALFPI => 1.57079632680;
30 9 Readonly my $TWOPI => 6.28318530718;
31 9 Readonly my $THREEPI2 => 4.71238898038;
32 9 Readonly my $EPSILON => 0.000001;
33 9 Readonly my $EOL => qq{\n};
34  
35 # Unit conversion table (multiplication factors from inches).
36 9 my %inches_to_unit = (
37 pt => 72.0,
38 in => 1.0,
39 mm => 25.4,
40 cm => 2.54,
41 px => 96.0,
42 pc => 6.0,
43 );
44  
45 # ---------------------------------------------------------------------------
46 # Bezier curve module-level state -- moved from GcodeXY.pm.
47 # (These are mutable, not Readonly.)
48 # ---------------------------------------------------------------------------
49  
50 9 my $m_approxscale = 1.0;
51 9 my $m_disttolscale = 0.5 / $m_approxscale;
52 9 $m_disttolscale *= $m_disttolscale;
53 9 my $curve_colleps = 1e-30;
54 9 my $m_angletol = 0.0;
55 9 my $curve_angletoleps = 0.01;
56 9 my $m_cusp_limit = 0.0;
57 9 my $m_count = 0;
58 9 my $curve_reclim = 50;
59 9 my @m_points = ();
60  
61  
62 # ===========================================================================
63 # GRAPHICS STATE
64 # ===========================================================================
65  
66 # Save the current graphics state (CTM, position, pen and font attributes)
67 # onto the graphics state stack.
68 0 ▶sub gsave ($self) {
Subroutine gsave: not called CC=1 SCAR=6.9
Pod undocumented
69 0 push @{ $self->{gstate} }, $self->{fontname};
70 0 push @{ $self->{gstate} }, $self->{fontsize};
71 0 push @{ $self->{gstate} }, $self->{curvepts};
72 0 push @{ $self->{gstate} }, $self->{penlocked};
73 0 push @{ $self->{gstate} }, $self->{posx};
74 0 push @{ $self->{gstate} }, $self->{posy};
75 0 push @{ $self->{gstate} }, $self->{CTM}->[0][0];
76 0 push @{ $self->{gstate} }, $self->{CTM}->[0][1];
77 0 push @{ $self->{gstate} }, $self->{CTM}->[0][2];
78 0 push @{ $self->{gstate} }, $self->{CTM}->[1][0];
79 0 push @{ $self->{gstate} }, $self->{CTM}->[1][1];
80 0 push @{ $self->{gstate} }, $self->{CTM}->[1][2];
81 0 push @{ $self->{gstate} }, $self->{CTM}->[2][0];
82 0 push @{ $self->{gstate} }, $self->{CTM}->[2][1];
83 0 push @{ $self->{gstate} }, $self->{CTM}->[2][2];
84 0 return 1;
85 }
86  
87 # Restore the most recently saved graphics state.
88 0 ▶sub grestore ($self) {
Subroutine grestore: not called CC=1 SCAR=6.9
Pod undocumented
89 0 $self->{CTM}->[2][2] = pop @{ $self->{gstate} };
90 0 $self->{CTM}->[2][1] = pop @{ $self->{gstate} };
91 0 $self->{CTM}->[2][0] = pop @{ $self->{gstate} };
92 0 $self->{CTM}->[1][2] = pop @{ $self->{gstate} };
93 0 $self->{CTM}->[1][1] = pop @{ $self->{gstate} };
94 0 $self->{CTM}->[1][0] = pop @{ $self->{gstate} };
95 0 $self->{CTM}->[0][2] = pop @{ $self->{gstate} };
96 0 $self->{CTM}->[0][1] = pop @{ $self->{gstate} };
97 0 $self->{CTM}->[0][0] = pop @{ $self->{gstate} };
98 0 $self->{posy} = pop @{ $self->{gstate} };
99 0 $self->{posx} = pop @{ $self->{gstate} };
100 0 $self->{penlocked} = pop @{ $self->{gstate} };
101 0 $self->{curvepts} = pop @{ $self->{gstate} };
102 0 $self->{fontsize} = pop @{ $self->{gstate} };
103 0 $self->{fontname} = pop @{ $self->{gstate} };
104 0 return 1;
105 }
106  
107  
108 # ===========================================================================
109 # COORDINATE SYSTEM
110 # ===========================================================================
111  
112 # We use three coordinate spaces:
113 # User -- result of scaling, translation and rotation applied by the caller.
114 # Paper -- absolute position on the page; found by applying the CTM to user.
115 # Device -- physical inches; found by scaling paper coords by dscale.
116 # After translate(100,200), the user origin is at (0,0) and the paper at (100,200).
117  
118 # Return the current point in USER coordinates, or set it if args are given.
119 0 ▶sub currentpoint ($self, $new_x = undef, $new_y = undef) {
Subroutine currentpoint: not called CC=4 SCAR=30.0
Pod undocumented
120 0 ▶ if ( defined $new_x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_x) 0 0 0
121 0 $self->{posx} = $new_x;
122 0 $self->{posy} = $new_y;
123 0 return 1;
124 }
125 0 return ( $self->{posx}, $self->{posy} );
126 }
127  
128 # Return paper coordinates of a user-space point (apply CTM).
129 0 ▶sub _u_to_p ($self, $x, $y) {
Subroutine _u_to_p: not called CC=1 SCAR=6.9
130 0 my @point = ( $x, $y );
131 0 $self->_transform( 1, \@point );
132 0 return @point;
133 }
134  
135 # Return device coordinates of a paper-space point (scale by dscale).
136 0 ▶sub _p_to_d ($self, $x, $y) {
Subroutine _p_to_d: not called CC=1 SCAR=6.9
137 0 return ( $x * $self->{dscale}, $y * $self->{dscale} );
138 }
139  
140 # Return device coordinates of a user-space point.
141 0 ▶sub _u_to_d ($self, $x, $y) {
Subroutine _u_to_d: not called CC=1 SCAR=6.9
142 0 my ( $rx, $ry ) = $self->_u_to_p( $x, $y );
143 0 return $self->_p_to_d( $rx, $ry );
144 }
145  
146  
147 # ===========================================================================
148 # LINE PRIMITIVES
149 # ===========================================================================
150  
151 # Move to ($x,$y), lifting and then lowering the pen.
152 0 ▶sub moveto ($self, $x, $y) {
Subroutine moveto: not called CC=1 SCAR=6.9
Pod undocumented
153 0 $self->penup();
154 0 $self->_genfastmove( $x, $y );
155 0 $self->pendown();
156 0 return 1;
157 }
158  
159 # Move to ($x,$y) relative to the current point.
160 0 ▶sub movetoR ($self, $x, $y) {
Subroutine movetoR: not called CC=1 SCAR=6.9
Pod undocumented
161 0 my ( $cx, $cy ) = $self->currentpoint();
162 0 $self->penup();
163 0 $self->_genfastmove( $x + $cx, $y + $cy );
164 0 $self->pendown();
165 0 return 1;
166 }
167  
168 # Draw a line. With 4 args: moveto ($x1,$y1) then draw to ($x2,$y2).
169 # With 2 args: draw from the current point to ($x1,$y1).
170 0 ▶sub line ($self, $x1, $y1 = undef, $x2 = undef, $y2 = undef) {
Subroutine line: not called CC=9 SCAR=45.0
Pod undocumented
171 0 ▶ if ( defined $x2 && defined $y2 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $x2 and defined $y2) { } 0 0 0
elsif (defined $x2 and not defined $y2) { } 0 0 0
elsif (defined $y1) { } 0 0 0
Condition0%0/3
defined $x2 and defined $y2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $x2 and defined $y2
defined $x2 defined $y2
Truth table0%0/3
A and B
A= defined $x2B= defined $y2
ABresult
0X0
100
111
172 0 $self->moveto( $x1, $y1 );
173 0 $self->_genslowmove( $x2, $y2 );
174 0 return 1;
175 }
176 ▶ elsif ( defined $x2 && !defined $y2 ) {
Condition0%0/3
defined $x2 and not defined $y2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $x2 and not defined $y2
defined $x2 not defined $y2
Truth table0%0/3
A and not(B)
A= defined $x2B= not defined $y2
ABresult
0X0
100
111
177 0 $self->_croak('wrong number of args for line (2 or 4)');
178 0 return 0;
179 }
180 elsif ( defined $y1 ) {
181 0 $self->_genslowmove( $x1, $y1 );
182 0 return 1;
183 }
184 else {
185 0 $self->_croak('wrong number of args for line (2 or 4)');
186 0 return 0;
187 }
188 }
189  
190 # Draw a line from the current point using relative coordinates.
191 0 ▶sub lineR ($self, $x, $y) {
Subroutine lineR: not called CC=1 SCAR=6.9
Pod undocumented
192 0 my ( $cx, $cy ) = $self->currentpoint();
193 0 $self->_genslowmove( $x + $cx, $y + $cy );
194 0 return 1;
195 }
196  
197  
198 # ===========================================================================
199 # POLYGON / HIDDEN-LINE CLIPPING
200 # ===========================================================================
201  
202 # Compute the axis-aligned bounding box of a flat list of (x,y) pairs.
203 0 ▶sub _bbox_from_points ($self, @pts) {
Subroutine _bbox_from_points: not called CC=6 SCAR=37.4
204 0 my ( $minx, $miny, $maxx, $maxy );
205 0 $minx = $miny = 1e99;
206 0 $maxx = $maxy = -1e99;
207 0 ▶ while (@pts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pts) 0 0 0
208 0 my $x = shift @pts;
209 0 my $y = shift @pts;
210 0 ▶ $minx = $x if $x < $minx;
Branch: 1 branch
BranchTrueFalseTotal
if $x < $minx 0 0 0
211 0 ▶ $miny = $y if $y < $miny;
Branch: 1 branch
BranchTrueFalseTotal
if $y < $miny 0 0 0
212 0 ▶ $maxx = $x if $x > $maxx;
Branch: 1 branch
BranchTrueFalseTotal
if $x > $maxx 0 0 0
213 0 ▶ $maxy = $y if $y > $maxy;
Branch: 1 branch
BranchTrueFalseTotal
if $y > $maxy 0 0 0
214 }
215 0 return ( $minx, $miny, $maxx, $maxy );
216 }
217  
218 # Return true if two bounding boxes (each [minx,miny,maxx,maxy]) intersect.
219 0 ▶sub _bbox_intersect ($self, $a, $b) {
Subroutine _bbox_intersect: not called CC=4 SCAR=30.0
220 0 ▶ return !( $a->[2] < $b->[0] || $a->[0] > $b->[2]
Condition0%0/9
$a->[2] < $b->[0] || $a->[0] > $b->[2] || $a->[3] < $b->[1] || $a->[1] > $b->[3]
l!l&&r!l&&!r
000
$a->[2] < $b->[0] or $a->[0] > $b->[2] or $a->[3] < $b->[1]
l!l&&r!l&&!r
000
$a->[2] < $b->[0] or $a->[0] > $b->[2]
l!l&&r!l&&!r
000
MC/DC0%0/4
$a->[2] < $b->[0] || $a->[0] > $b->[2] || $a->[3] < $b->[1] || $a->[1] > $b->[3]
$a->[2] < $b->[0] $a->[0] > $b->[2] $a->[3] < $b->[1] $a->[1] > $b->[3]
Truth table0%0/5
A or B or C || D
A= $a->[2] < $b->[0]B= $a->[0] > $b->[2]C= $a->[3] < $b->[1]D= $a->[1] > $b->[3]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
221 || $a->[3] < $b->[1] || $a->[1] > $b->[3] );
222 }
223  
224 # Point-in-polygon test (ray-crossing algorithm).
225 # Returns 1 if ($px,$py) is inside the polygon, 0 otherwise.
226 0 ▶sub _point_in_poly ($self, $px, $py, @poly) {
Subroutine _point_in_poly: not called CC=7 SCAR=40.3
227 0 my $inside = 0;
228 0 my $n = scalar @poly / 2;
229 0 ▶ return 0 if $n < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 3 0 0 0
230 0 ▶ for ( my $i = 0; $i < $n; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
231 0 my $xi = $poly[ 2 * $i ];
232 0 my $yi = $poly[ 2 * $i + 1 ];
233 0 ▶ my $j = ( $i == 0 ) ? $n - 1 : $i - 1;
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 0 0 0
234 0 my $xj = $poly[ 2 * $j ];
235 0 my $yj = $poly[ 2 * $j + 1 ];
236 0 ▶ my $intersect =
Condition0%0/3
$yi > $py != $yj > $py && $px < ($xj - $xi) * ($py - $yi) / ($yj - $yi + 0.0) + $xi
!ll&&!rl&&r
000
MC/DC0%0/2
$yi > $py != $yj > $py && $px < ($xj - $xi) * ($py - $yi) / ($yj - $yi + 0.0) + $xi
$yi > $py != $yj > $py $px < ($xj - $xi) * ($py - $yi) / ($yj - $yi + 0.0) + $xi
Truth table0%0/3
A && B
A= $yi > $py != $yj > $pyB= $px < ($xj - $xi) * ($py - $yi) / ($yj - $yi + 0.0) + $xi
ABresult
0X0
100
111
237 ( ( $yi > $py ) != ( $yj > $py ) ) &&
238 ( $px < ( $xj - $xi ) * ( $py - $yi ) / ( $yj - $yi + 0.0 ) + $xi );
239 0 ▶ $inside = !$inside if $intersect;
Branch: 1 branch
BranchTrueFalseTotal
if $intersect 0 0 0
240 }
241 0 ▶ return $inside ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$inside ? : 0 0 0
242 }
243  
244 # Add a polygon to the clip queue, removing parts of previously queued
245 # polygons that fall inside the new one.
246 0 ▶sub polygon_clip ($self, $x, $y, @rest) {
Subroutine polygon_clip: not called CC=16 SCAR=56.1
Pod undocumented
247 0 ▶ if ( scalar @rest == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rest == 0) 0 0 0
248 0 $self->_croak('bad polygon_clip - no line segments found');
249 0 return 0;
250 }
251 0 ▶ if ( scalar @rest % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rest % 2) 0 0 0
252 0 $self->_croak('bad polygon_clip - odd number of points');
253 0 return 0;
254 }
255 0 my @poly = ( $x, $y, @rest );
256 # Ensure the polygon is closed
257 0 ▶ if ( $poly[-2] != $poly[0] || $poly[-1] != $poly[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($poly[-2] != $poly[0] or $poly[-1] != $poly[1]) 0 0 0
Condition0%0/3
$poly[-2] != $poly[0] or $poly[-1] != $poly[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$poly[-2] != $poly[0] or $poly[-1] != $poly[1]
$poly[-2] != $poly[0] $poly[-1] != $poly[1]
Truth table0%0/3
A or B
A= $poly[-2] != $poly[0]B= $poly[-1] != $poly[1]
ABresult
1X1
011
000
258 0 push @poly, $poly[0], $poly[1];
259 }
260 0 ▶ $self->{clip_queue} //= [];
Condition0%0/2
$self->{"clip_queue"} //= []
l!l
00
MC/DC0%0/1
$self->{"clip_queue"} //= []
$self->{"clip_queue"}
Truth table0%0/2
A
A= $self->{"clip_queue"}
Aresult
11
00
261 0 my ( $minx, $miny, $maxx, $maxy ) = $self->_bbox_from_points(@poly);
262 0 my $newbbox = [ $minx, $miny, $maxx, $maxy ];
263  
264 0 foreach my $old ( @{ $self->{clip_queue} } ) {
265 0 ▶ next unless $self->_bbox_intersect( $old->{bbox}, $newbbox );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_bbox_intersect($old->{"bbox"}, $newbbox) 0 0 0
266 0 my @newsegs = ();
267 0 foreach my $seg ( @{ $old->{segs} } ) {
268 0 my @ts = ( 0.0, 1.0 );
269 0 ▶ for ( my $i = 0; $i < @poly - 2; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @poly - 2) 0 0 0
270 0 my ( $x2, $y2 ) = ( $poly[$i], $poly[ $i + 1 ] );
271 0 my ( $x3, $y3 ) = ( $poly[ $i + 2 ], $poly[ $i + 3 ] );
272 my $t = $self->_getsegintersect(
273 $seg->{sx}, $seg->{sy}, $seg->{dx}, $seg->{dy},
274 0 $x2, $y2, $x3, $y3 );
275 0 ▶ if ( $t && $t > 0.0 && $t < 1.0 ) { push @ts, $t }
Branch: 1 branch
BranchTrueFalseTotal
if ($t and $t > 0 and $t < 1) 0 0 0
Condition0%0/6
$t and $t > 0 and $t < 1
!ll&&!rl&&r
000
$t and $t > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$t and $t > 0 and $t < 1
$t $t > 0 $t < 1
Truth table0%0/4
A and B and C
A= $tB= $t > 0C= $t < 1
ABCresult
0XX0
10X0
1100
1111
276 }
277 0 my %seen;
278 0 @ts = sort { $a <=> $b }
279 0 grep { !$seen{ sprintf( "%.8f", $_ ) }++ } @ts;
280 0 ▶ for ( my $k = 0; $k < @ts - 1; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @ts - 1) 0 0 0
281 0 my ( $t1, $t2 ) = ( $ts[$k], $ts[ $k + 1 ] );
282 0 my $mx = $seg->{sx} + ( $seg->{dx} - $seg->{sx} ) * ( ( $t1 + $t2 ) / 2.0 );
283 0 my $my = $seg->{sy} + ( $seg->{dy} - $seg->{sy} ) * ( ( $t1 + $t2 ) / 2.0 );
284 0 ▶ next if $self->_point_in_poly( $mx, $my, @poly );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_point_in_poly($mx, $my, @poly) 0 0 0
285 0 my $sx = $seg->{sx} + ( $seg->{dx} - $seg->{sx} ) * $t1;
286 0 my $sy = $seg->{sy} + ( $seg->{dy} - $seg->{sy} ) * $t1;
287 0 my $dx = $seg->{sx} + ( $seg->{dx} - $seg->{sx} ) * $t2;
288 0 my $dy = $seg->{sy} + ( $seg->{dy} - $seg->{sy} ) * $t2;
289 0 push @newsegs, { sx => $sx, sy => $sy, dx => $dx, dy => $dy };
290 }
291 }
292 0 $old->{segs} = [@newsegs];
293 }
294 0 my @segs = ();
295 0 ▶ for ( my $i = 0; $i < @poly - 2; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @poly - 2) 0 0 0
296 0 my ( $sx, $sy ) = ( $poly[$i], $poly[ $i + 1 ] );
297 0 my ( $dx, $dy ) = ( $poly[ $i + 2 ], $poly[ $i + 3 ] );
298 0 push @segs, { sx => $sx, sy => $sy, dx => $dx, dy => $dy };
299 }
300 0 push @{ $self->{clip_queue} }, { segs => [@segs], bbox => $newbbox };
301 0 return 1;
302 }
303  
304 # Flush the clip queue: draw all surviving segments and clear the queue.
305 0 ▶sub polygon_clip_end ($self) {
Subroutine polygon_clip_end: not called CC=4 SCAR=30.0
Pod undocumented
306 0 ▶ $self->{clip_queue} //= [];
Condition0%0/2
$self->{"clip_queue"} //= []
l!l
00
MC/DC0%0/1
$self->{"clip_queue"} //= []
$self->{"clip_queue"}
Truth table0%0/2
A
A= $self->{"clip_queue"}
Aresult
11
00
307 0 foreach my $poly ( @{ $self->{clip_queue} } ) {
308 0 foreach my $seg ( @{ $poly->{segs} } ) {
309 0 $self->penup();
310 0 $self->_genfastmove( $seg->{sx}, $seg->{sy} );
311 0 $self->pendown();
312 0 $self->_genslowmove( $seg->{dx}, $seg->{dy} );
313 }
314 }
315 0 $self->stroke();
316 0 @{ $self->{clip_queue} } = ();
317 0 return 1;
318 }
319  
320 # Draw a series of consecutive line segments from ($x,$y).
321 0 ▶sub polygon ($self, $x, $y, @rest) {
Subroutine polygon: not called CC=4 SCAR=30.0
Pod undocumented
322 0 ▶ if ( scalar @rest == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rest == 0) 0 0 0
323 0 $self->_croak('bad polygon - no line segments found');
324 0 return 0;
325 }
326 0 ▶ if ( scalar @rest % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rest % 2) 0 0 0
327 0 $self->_croak('bad polygon - odd number of points');
328 0 return 0;
329 }
330 0 $self->moveto( $x, $y );
331 0 ▶ while (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 0 0 0
332 0 $x = shift @rest;
333 0 $y = shift @rest;
334 0 $self->line( $x, $y );
335 }
336 0 return 1;
337 }
338  
339 # Draw a series of consecutive line segments from the current position,
340 # in absolute coordinates.
341 0 ▶sub polygonC ($self, @coords) {
Subroutine polygonC: not called CC=4 SCAR=30.0
Pod undocumented
342 0 ▶ if ( scalar @coords < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @coords < 2) 0 0 0
343 0 $self->_croak('bad polygon (C) - not enough points');
344 0 return 0;
345 }
346 0 ▶ if ( scalar @coords % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @coords % 2) 0 0 0
347 0 $self->_croak('bad polygon (C) - odd number of points');
348 0 return 0;
349 }
350 0 ▶ while (@coords) {
Branch: 1 branch
BranchTrueFalseTotal
if (@coords) 0 0 0
351 0 my $x = shift @coords;
352 0 my $y = shift @coords;
353 0 $self->line( $x, $y );
354 }
355 0 return 1;
356 }
357  
358 # Draw a series of consecutive line segments in relative coordinates.
359 0 ▶sub polygonR ($self, @coords) {
Subroutine polygonR: not called CC=4 SCAR=30.0
Pod undocumented
360 0 ▶ if ( scalar @coords < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @coords < 2) 0 0 0
361 0 $self->_croak('bad polygon (C) - not enough points');
362 0 return 0;
363 }
364 0 ▶ if ( scalar @coords % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @coords % 2) 0 0 0
365 0 $self->_croak('bad polygon (C) - odd number of points');
366 0 return 0;
367 }
368 0 ▶ while (@coords) {
Branch: 1 branch
BranchTrueFalseTotal
if (@coords) 0 0 0
369 0 my ( $cx, $cy ) = $self->currentpoint();
370 0 my $x = shift @coords;
371 0 my $y = shift @coords;
372 0 $self->line( $x + $cx, $y + $cy );
373 }
374 0 return 1;
375 }
376  
377 # Draw a polygon with rounded corners between line segments.
378 # Requires at least 3 points (6 numbers) plus a radius $r.
379 0 ▶sub polygonround ($self, $r, @rest) {
Subroutine polygonround: not called CC=4 SCAR=30.0
Pod undocumented
380 0 ▶ if ( scalar @rest < 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rest < 6) 0 0 0
381 0 $self->_croak('bad polygonround - need at least 3 points (6 numbers)');
382 0 return 0;
383 }
384 0 ▶ if ( scalar @rest % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rest % 2) 0 0 0
385 0 $self->_croak('bad polygonround - odd number of coordinates');
386 0 return 0;
387 }
388 0 my $x = shift @rest;
389 0 my $y = shift @rest;
390 0 $self->moveto( $x, $y );
391 0 my $dx = shift @rest;
392 0 my $dy = shift @rest;
393 0 my $halfx = ( $dx - $x ) / 2.0;
394 0 my $halfy = ( $dy - $y ) / 2.0;
395 0 $self->lineR( $halfx, $halfy );
396 0 my ( $newhalfx, $newhalfy );
397 0 ▶ while (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 0 0 0
398 0 $x = $dx;
399 0 $y = $dy;
400 0 $dx = shift @rest;
401 0 $dy = shift @rest;
402 0 $newhalfx = ( $dx - $x ) / 2.0;
403 0 $newhalfy = ( $dy - $y ) / 2.0;
404 0 $self->arcto( $x, $y, $x + $newhalfx, $y + $newhalfy, $r );
405 0 $halfx = $newhalfx;
406 0 $halfy = $newhalfy;
407 }
408 0 $self->line( $dx, $dy );
409 0 return 1;
410 }
411  
412  
413 # ===========================================================================
414 # RECTANGLES
415 # ===========================================================================
416  
417 # Create a rectangle. With 4 args: (x1,y1) to (x2,y2).
418 # With 2 args: extend from the current point by (x,y).
419 0 ▶sub box ($self, $x1, $y1, $x2 = undef, $y2 = undef) {
Subroutine box: not called CC=7 SCAR=40.3
Pod undocumented
420 0 ▶ if ( defined $x2 && defined $y2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $x2 and defined $y2) { } 0 0 0
elsif (not defined $x2 || defined $y2) { } 0 0 0
Condition0%0/3
defined $x2 and defined $y2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $x2 and defined $y2
defined $x2 defined $y2
Truth table0%0/3
A and B
A= defined $x2B= defined $y2
ABresult
0X0
100
111
421 0 $self->polygon( $x1, $y1, $x2, $y1, $x2, $y2, $x1, $y2, $x1, $y1 );
422 }
423 ▶ elsif ( !defined $x2 && !defined $y2 ) {
Condition0%0/3
defined $x2 || defined $y2
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $x2 || defined $y2
defined $x2 defined $y2
Truth table0%0/3
A || B
A= defined $x2B= defined $y2
ABresult
1X1
011
000
424 0 $x2 = $x1;
425 0 $y2 = $y1;
426 0 ( $x1, $y1 ) = $self->currentpoint();
427 0 $self->polygonC( $x2, $y1, $x2, $y2, $x1, $y2, $x1, $y1 );
428 }
429 else {
430 0 $self->_croak('box: wrong number of arguments (must be 2 or 4)');
431 0 return 0;
432 }
433 0 return 1;
434 }
435  
436 # Create a rectangle from the current point using relative coordinates.
437 0 ▶sub boxR ($self, $x, $y) {
Subroutine boxR: not called CC=1 SCAR=6.9
Pod undocumented
438 0 my ( $cx, $cy ) = $self->currentpoint();
439 0 $self->polygonR( $x, 0, 0, $y, -$x, 0, 0, -$y );
440 0 return 1;
441 }
442  
443 # Create a box with rounded corners.
444 0 ▶sub boxround ($self, $r, $bx, $by, $tx, $ty) {
Subroutine boxround: not called CC=1 SCAR=6.9
Pod undocumented
445 0 my $halfheight = ( $ty - $by ) / 2.0;
446 0 my $halfwidth = ( $tx - $bx ) / 2.0;
447 0 $self->moveto( $bx, $by + $halfheight );
448 0 $self->arcto( $bx, $ty, $bx + $halfwidth, $ty, $r );
449 0 $self->arcto( $tx, $ty, $tx, $ty - $halfheight, $r );
450 0 $self->arcto( $tx, $by, $tx - $halfwidth, $by, $r );
451 0 $self->arcto( $bx, $by, $bx, $by + $halfheight, $r );
452 0 return 1;
453 }
454  
455  
456 # ===========================================================================
457 # ARROWHEAD AND PAGE BORDER
458 # ===========================================================================
459  
460 # Create an arrowhead at the tip of the most recent segment.
461 # $type is 'open' (default) or 'closed'.
462 0 ▶sub arrowhead ($self, $length, $width, $type = 'open') {
Subroutine arrowhead: not called CC=4 SCAR=30.0
Pod undocumented
463 0 my ( $tailx, $taily, $tipx, $tipy, $dx, $dy, $angle );
464 0 ▶ if ( scalar $self->{psegments} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $self->{'psegments'} > 0) { } 0 0 0
465 0 $tailx = $self->{psegments}[-1]{sx};
466 0 $taily = $self->{psegments}[-1]{sy};
467 0 $tipx = $self->{psegments}[-1]{dx};
468 0 $tipy = $self->{psegments}[-1]{dy};
469 0 $dx = $tipx - $tailx;
470 0 $dy = $tipy - $taily;
471 0 $angle = atan2( $dy, $dx ); # radians
472 }
473 else {
474 0 ( $tipx, $tipy ) = $self->currentpoint();
475 0 $angle = 0.0;
476 }
477 0 $self->gsave();
478 # Segments are already scaled to inches; scale back to the current unit.
479 0 $self->translate( $self->_in_unitfied($tipx), $self->_in_unitfied($tipy) );
480 0 $self->rotate( $angle * $R2D );
481 0 $self->line( -$length, $width / 2.0 );
482 0 ▶ if ( $type eq 'closed' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'closed') { } 0 0 0
483 0 $self->line( -$length, -$width / 2.0 );
484 0 $self->line( 0, 0 );
485 }
486 else {
487 0 $self->line( -$length, -$width / 2.0, 0, 0 );
488 }
489 0 $self->grestore();
490 0 return 1;
491 }
492  
493 # Draw a box inset by $margin from the page edges.
494 0 ▶sub pageborder ($self, $margin) {
Subroutine pageborder: not called CC=1 SCAR=6.9
Pod undocumented
495 $self->box(
496 $margin, $margin,
497 0 $self->{xsize} - $margin, $self->{ysize} - $margin
498 );
499 0 return 1;
500 }
501  
502  
503 # ===========================================================================
504 # BEZIER CURVES
505 # ===========================================================================
506  
507 # Draw a Bézier curve. 6 control points = quadratic, 8 = cubic, more = higher order.
508 0 ▶sub curve ($self, @control) {
Subroutine curve: not called CC=4 SCAR=30.0
Pod undocumented
509 0 ▶ if ( scalar @control < 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @control < 6) 0 0 0
510 0 $self->_croak('wrong number of args for curve');
511 0 return 0;
512 }
513 0 ▶ if ( scalar @control == 6 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @control == 6) { } 0 0 0
elsif (scalar @control == 8) { } 0 0 0
514 0 $self->_curve3(@control);
515 }
516 elsif ( scalar @control == 8 ) {
517 0 $self->_curve4(@control);
518 }
519 else {
520 0 my $b = Math::Bezier->new(@control);
521 0 my $pts = $self->{curvepts};
522 0 my @points = $b->curve($pts);
523 0 $self->polygon(@points);
524 }
525 0 return 1;
526 }
527  
528 # Draw a Bézier curve starting from the current position.
529 0 ▶sub curveto ($self, @control) {
Subroutine curveto: not called CC=4 SCAR=30.0
Pod undocumented
530 0 ▶ if ( scalar @control < 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @control < 6) 0 0 0
531 0 $self->_croak('wrong number of args for curveto');
532 0 return 0;
533 }
534 0 unshift @control, $self->currentpoint();
535 0 ▶ if ( scalar @control == 6 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @control == 6) { } 0 0 0
elsif (scalar @control == 8) { } 0 0 0
536 0 $self->_penlock();
537 0 $self->_curve3(@control);
538 0 $self->_penunlock();
539 }
540 elsif ( scalar @control == 8 ) {
541 0 $self->_penlock();
542 0 $self->_curve4(@control);
543 0 $self->_penunlock();
544 }
545 else {
546 0 my $b = Math::Bezier->new(@control);
547 0 my $pts = $self->{curvepts};
548 0 my @points = $b->curve($pts);
549 0 shift @points;
550 0 shift @points; # remove start point — already there
551 0 $self->polygonC(@points);
552 }
553 0 return 1;
554 }
555  
556 # Internal: render a quadratic Bézier curve by recursive subdivision.
557 0 ▶sub _curve3 ($self, $x1, $y1, $x2, $y2, $x3, $y3) {
Subroutine _curve3: not called CC=1 SCAR=6.9
558 0 @m_points = ();
559 0 push @m_points, ( $x1, $y1 );
560 0 $self->_recbezier3( $x1, $y1, $x2, $y2, $x3, $y3, 0 );
561 0 push @m_points, ( $x3, $y3 );
562 0 $self->polygon(@m_points);
563 0 return 1;
564 }
565  
566 0 ▶sub _recbezier3 ($self, $x1, $y1, $x2, $y2, $x3, $y3, $level) {
Subroutine _recbezier3: not called CC=13 SCAR=52.0
567 0 ▶ if ( $level > $curve_reclim ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($level > $curve_reclim) 0 0 0
568 0 my $x12 = ( $x1 + $x2 ) / 2;
569 0 my $y12 = ( $y1 + $y2 ) / 2;
570 0 my $x23 = ( $x2 + $x3 ) / 2;
571 0 my $y23 = ( $y2 + $y3 ) / 2;
572 0 my $x123 = ( $x12 + $x23 ) / 2;
573 0 my $y123 = ( $y12 + $y23 ) / 2;
574 0 my $dx = $x3 - $x1;
575 0 my $dy = $y3 - $y1;
576 0 my $d = _fabs( ( ( $x2 - $x3 ) * $dy - ( $y2 - $y3 ) * $dx ) );
577 0 my $da;
578 0 ▶ if ( $d > $curve_colleps ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d > $curve_colleps) { } 0 0 0
579 0 ▶ if ( $d * $d <= $m_disttolscale * ( $dx * $dx + $dy * $dy ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d * $d <= $m_disttolscale * ($dx * $dx + $dy * $dy)) 0 0 0
580 0 ▶ if ( $m_angletol < $curve_angletoleps ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_angletol < $curve_angletoleps) 0 0 0
581 0 push @m_points, ( $x123, $y123 );
582 0 return;
583 }
584 0 $da = _fabs(
585 atan2( $y3 - $y2, $x3 - $x2 ) - atan2( $y2 - $y1, $x2 - $x1 ) );
586 0 ▶ if ( $da >= $PI ) { $da = 2 * $PI - $da }
Branch: 1 branch
BranchTrueFalseTotal
if ($da >= $PI) 0 0 0
587 0 ▶ if ( $da < $m_angletol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($da < $m_angletol) 0 0 0
588 0 push @m_points, ( $x123, $y123 );
589 0 return;
590 }
591 }
592 }
593 else {
594 0 $da = $dx * $dx + $dy * $dy;
595 0 ▶ if ( $da == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($da == 0) { } 0 0 0
596 0 $d = _calc_sq_distance( $x1, $y1, $x2, $y2 );
597 }
598 else {
599 0 $d = ( ( $x2 - $x1 ) * $dx + ( $y2 - $y1 ) * $dy ) / $da;
600 0 ▶ if ( $d > 0 && $d < 1 ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($d > 0 and $d < 1) 0 0 0
Condition0%0/3
$d > 0 and $d < 1
!ll&&!rl&&r
000
MC/DC0%0/2
$d > 0 and $d < 1
$d > 0 $d < 1
Truth table0%0/3
A and B
A= $d > 0B= $d < 1
ABresult
0X0
100
111
601 0 ▶ if ( $d <= 0 ) { $d = _calc_sq_distance( $x2, $y2, $x1, $y1 ) }
Branch: 2 branches
BranchTrueFalseTotal
if ($d <= 0) { } 0 0 0
elsif ($d >= 1) { } 0 0 0
602 0 elsif ( $d >= 1 ) { $d = _calc_sq_distance( $x2, $y2, $x3, $y3 ) }
603 else {
604 0 $d = _calc_sq_distance( $x2, $y2, $x1 + $d * $dx, $y1 + $d * $dy );
605 }
606 }
607 0 ▶ if ( $d < $m_disttolscale ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < $m_disttolscale) 0 0 0
608 0 push @m_points, ( $x2, $y2 );
609 0 return;
610 }
611 }
612 0 $self->_recbezier3( $x1, $y1, $x12, $y12, $x123, $y123, $level + 1 );
613 0 $self->_recbezier3( $x123, $y123, $x23, $y23, $x3, $y3, $level + 1 );
614 0 return 1;
615 }
616  
617 # Internal: render a cubic Bézier curve by recursive subdivision.
618 # Algorithm translated from the Anti-Grain Geometry library.
619 0 ▶sub _curve4 ($self, $x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) {
Subroutine _curve4: not called CC=1 SCAR=6.9
620 0 @m_points = ();
621 0 push @m_points, ( $x1, $y1 );
622 0 $self->_recbezier4( $x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4, 0 );
623 0 push @m_points, ( $x4, $y4 );
624 0 $self->polygon(@m_points);
625 0 return 1;
626 }
627  
628 0 ▶sub _recbezier4 ($self, $x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4, $level) {
Subroutine _recbezier4: not called CC=38 SCAR=73.0
629 0 my ( $da1, $da2, $k );
630 0 ▶ if ( $level > $curve_reclim ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($level > $curve_reclim) 0 0 0
631 0 my $x12 = ( $x1 + $x2 ) / 2.0;
632 0 my $y12 = ( $y1 + $y2 ) / 2.0;
633 0 my $x23 = ( $x2 + $x3 ) / 2.0;
634 0 my $y23 = ( $y2 + $y3 ) / 2.0;
635 0 my $x34 = ( $x3 + $x4 ) / 2.0;
636 0 my $y34 = ( $y3 + $y4 ) / 2.0;
637 0 my $x123 = ( $x12 + $x23 ) / 2.0;
638 0 my $y123 = ( $y12 + $y23 ) / 2.0;
639 0 my $x234 = ( $x23 + $x34 ) / 2.0;
640 0 my $y234 = ( $y23 + $y34 ) / 2.0;
641 0 my $x1234 = ( $x123 + $x234 ) / 2.0;
642 0 my $y1234 = ( $y123 + $y234 ) / 2.0;
643 0 my $dx = $x4 - $x1;
644 0 my $dy = $y4 - $y1;
645 0 my $d2 = _fabs( ( ( $x2 - $x4 ) * $dy - ( $y2 - $y4 ) * $dx ) );
646 0 my $d3 = _fabs( ( ( $x3 - $x4 ) * $dy - ( $y3 - $y4 ) * $dx ) );
647 0 my $tmp = ( int( $d2 > $curve_colleps ) << 1 ) + int( $d3 > $curve_colleps );
648 0 ▶ if ( $tmp == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp == 0) 0 0 0
649 0 $k = $dx * $dx + $dy * $dy;
650 0 ▶ if ( $k == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 0) { } 0 0 0
651 0 $d2 = _calc_sq_distance( $x1, $y1, $x2, $y2 );
652 0 $d3 = _calc_sq_distance( $x4, $y4, $x3, $y3 );
653 }
654 else {
655 0 $k = 1.0 / $k;
656 0 $da1 = $x2 - $x1;
657 0 $da2 = $y2 - $y1;
658 0 $d2 = $k * ( $da1 * $dx + $da2 * $dy );
659 0 $da1 = $x3 - $x1;
660 0 $da2 = $y3 - $y1;
661 0 $d3 = $k * ( $da1 * $dx + $da2 * $dy );
662 0 ▶ if ( $d2 > 0 && $d2 < 1 && $d3 > 0 && $d3 < 1 ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 > 0 and $d2 < 1 and $d3 > 0 and $d3 < 1) 0 0 0
Condition0%0/9
$d2 > 0 and $d2 < 1 and $d3 > 0 and $d3 < 1
!ll&&!rl&&r
000
$d2 > 0 and $d2 < 1 and $d3 > 0
!ll&&!rl&&r
000
$d2 > 0 and $d2 < 1
!ll&&!rl&&r
000
MC/DC0%0/4
$d2 > 0 and $d2 < 1 and $d3 > 0 and $d3 < 1
$d2 > 0 $d2 < 1 $d3 > 0 $d3 < 1
Truth table0%0/5
A and B and C and D
A= $d2 > 0B= $d2 < 1C= $d3 > 0D= $d3 < 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
663 0 ▶ if ( $d2 <= 0 ) { $d2 = _calc_sq_distance( $x2, $y2, $x1, $y1 ) }
Branch: 2 branches
BranchTrueFalseTotal
if ($d2 <= 0) { } 0 0 0
elsif ($d2 >= 1) { } 0 0 0
664 0 elsif ( $d2 >= 1 ) { $d2 = _calc_sq_distance( $x2, $y2, $x4, $y4 ) }
665 else {
666 0 $d2 = _calc_sq_distance( $x2, $y2,
667 $x1 + $d2 * $dx, $y1 + $d2 * $dy );
668 }
669 0 ▶ if ( $d3 <= 0 ) { $d3 = _calc_sq_distance( $x3, $y3, $x1, $y1 ) }
Branch: 2 branches
BranchTrueFalseTotal
if ($d3 <= 0) { } 0 0 0
elsif ($d3 >= 1) { } 0 0 0
670 0 elsif ( $d3 >= 1 ) { $d3 = _calc_sq_distance( $x3, $y3, $x4, $y4 ) }
671 else {
672 0 $d3 = _calc_sq_distance( $x3, $y3,
673 $x1 + $d3 * $dx, $y1 + $d3 * $dy );
674 }
675 }
676 0 ▶ if ( $d2 > $d3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 > $d3) { } 0 0 0
677 0 ▶ if ( $d2 < $m_disttolscale ) { push @m_points, ( $x2, $y2 ); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < $m_disttolscale) 0 0 0
678 }
679 else {
680 0 ▶ if ( $d3 < $m_disttolscale ) { push @m_points, ( $x3, $y3 ); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($d3 < $m_disttolscale) 0 0 0
681 }
682 }
683 0 ▶ if ( $tmp == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp == 1) 0 0 0
684 0 ▶ if ( $d3 * $d3 <= $m_disttolscale * ( $dx * $dx + $dy * $dy ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d3 * $d3 <= $m_disttolscale * ($dx * $dx + $dy * $dy)) 0 0 0
685 0 ▶ if ( $m_angletol < $curve_angletoleps ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_angletol < $curve_angletoleps) 0 0 0
686 0 push @m_points, ( $x23, $y23 );
687 0 return;
688 }
689 0 $da1 = _fabs(
690 atan2( $y4 - $y3, $x4 - $x3 ) - atan2( $y3 - $y2, $x3 - $x2 ) );
691 0 ▶ if ( $da1 >= $PI ) { $da1 = 2 * $PI - $da1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($da1 >= $PI) 0 0 0
692 0 ▶ if ( $da1 < $m_angletol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($da1 < $m_angletol) 0 0 0
693 0 push @m_points, ( $x2, $y2 );
694 0 push @m_points, ( $x3, $y3 );
695 0 return;
696 }
697 0 ▶ if ( $m_cusp_limit != 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_cusp_limit != 0) 0 0 0
698 0 ▶ if ( $da1 > $m_cusp_limit ) { push @m_points, ( $x3, $y3 ); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($da1 > $m_cusp_limit) 0 0 0
699 }
700 }
701 }
702 0 ▶ if ( $tmp == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp == 2) 0 0 0
703 0 ▶ if ( $d2 * $d2 <= $m_disttolscale * ( $dx * $dx + $dy * $dy ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 * $d2 <= $m_disttolscale * ($dx * $dx + $dy * $dy)) 0 0 0
704 0 ▶ if ( $m_angletol < $curve_angletoleps ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_angletol < $curve_angletoleps) 0 0 0
705 0 push @m_points, ( $x23, $y23 );
706 0 return;
707 }
708 0 $da1 = _fabs(
709 atan2( $y3 - $y2, $x3 - $x2 ) - atan2( $y2 - $y1, $x2 - $x1 ) );
710 0 ▶ if ( $da1 >= $PI ) { $da1 = 2 * $PI - $da1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($da1 >= $PI) 0 0 0
711 0 ▶ if ( $da1 < $m_angletol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($da1 < $m_angletol) 0 0 0
712 0 push @m_points, ( $x2, $y2 );
713 0 push @m_points, ( $x3, $y3 );
714 0 return;
715 }
716 0 ▶ if ( $m_cusp_limit != 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_cusp_limit != 0) 0 0 0
717 0 ▶ if ( $da1 > $m_cusp_limit ) { push @m_points, ( $x2, $y2 ); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($da1 > $m_cusp_limit) 0 0 0
718 }
719 }
720 }
721 0 ▶ if ( $tmp == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp == 3) 0 0 0
722 0 ▶ if ( ( $d2 + $d3 ) * ( $d2 + $d3 ) <=
Branch: 1 branch
BranchTrueFalseTotal
if (($d2 + $d3) * ($d2 + $d3) <= $m_disttolscale * ($dx * $dx + $dy * $dy)) 0 0 0
723 $m_disttolscale * ( $dx * $dx + $dy * $dy ) )
724 {
725 0 ▶ if ( $m_angletol < $curve_angletoleps ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_angletol < $curve_angletoleps) 0 0 0
726 0 push @m_points, ( $x23, $y23 );
727 0 return;
728 }
729 0 $k = atan2( $y3 - $y2, $x3 - $x2 );
730 0 $da1 = _fabs( $k - atan2( $y2 - $y1, $x2 - $x1 ) );
731 0 $da2 = _fabs( atan2( $y4 - $y3, $x4 - $x3 ) - $k );
732 0 ▶ if ( $da1 >= $PI ) { $da1 = 2 * $PI - $da1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($da1 >= $PI) 0 0 0
733 0 ▶ if ( $da2 >= $PI ) { $da2 = 2 * $PI - $da2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($da2 >= $PI) 0 0 0
734 0 ▶ if ( $da1 + $da2 < $m_angletol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($da1 + $da2 < $m_angletol) 0 0 0
735 0 push @m_points, ( $x23, $y23 );
736 0 return;
737 }
738 0 ▶ if ( $m_cusp_limit != 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_cusp_limit != 0) 0 0 0
739 0 ▶ if ( $da1 > $m_cusp_limit ) { push @m_points, ( $x2, $y2 ); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($da1 > $m_cusp_limit) 0 0 0
740 0 ▶ if ( $da2 > $m_cusp_limit ) { push @m_points, ( $x3, $y3 ); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($da2 > $m_cusp_limit) 0 0 0
741 }
742 }
743 }
744 0 $self->_recbezier4( $x1, $y1, $x12, $y12, $x123, $y123, $x1234, $y1234, $level + 1 );
745 0 $self->_recbezier4( $x1234, $y1234, $x234, $y234, $x34, $y34, $x4, $y4, $level + 1 );
746 0 return 1;
747 }
748  
749  
750 # ===========================================================================
751 # ARCS
752 # ===========================================================================
753  
754 # Draw a circular arc centred at ($x,$y), radius $r, from $start to $finish degrees.
755 # The number of segments is derived automatically from the radius and arc span.
756 0 ▶sub arc ($self, $x, $y, $r, $start, $finish) {
Subroutine arc: not called CC=4 SCAR=30.0
Pod undocumented
757 0 ▶ if ( !defined $finish ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $finish) 0 0 0
758 0 $self->_croak('bad arc - need x, y, r, start, finish');
759 0 return 0;
760 }
761 0 my $full_n = circle_segments($r);
762 0 my $steps = int( _fabs( $full_n * ( $finish - $start ) / 360.0 ) );
763 0 ▶ $steps = 8 if $steps < 8;
Branch: 1 branch
BranchTrueFalseTotal
if $steps < 8 0 0 0
764 0 my @points = ();
765 0 my $s = radians($start);
766 0 my $f = radians($finish);
767 0 my $inc = ( $f - $s ) / $steps;
768 0 for my $i ( 0 .. $steps ) {
769 0 my $curs = $s + $i * $inc;
770 0 push @points, $x + $r * cos $curs;
771 0 push @points, $y + $r * sin $curs;
772 }
773 0 $self->polygon(@points);
774 0 return 1;
775 }
776  
777 # Join two lines with a circular arc fillet.
778 # Miller, "Joining Two Lines with a Circular Arc Fillet," Graphics Gems III.
779 0 ▶sub arcto ($self, $x2, $y2, $x4, $y4, $r) {
Subroutine arcto: not called CC=3 SCAR=24.8
Pod undocumented
780 0 ▶ if ( !defined $r ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r) 0 0 0
781 0 $self->_croak('bad arcto - need x1, y1, x2, y2, r');
782 0 return 0;
783 }
784 0 my ( $x1, $y1 ) = $self->currentpoint();
785 0 my ( $p1x, $p1y, $p2x, $p2y, $p3x, $p3y, $p4x, $p4y, $xc, $yc, $pa, $aa ) =
786 $self->_fillet( $x1, $y1, $x2, $y2, $x2, $y2, $x4, $y4, $r );
787 0 ▶ if ( defined $p1x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p1x) 0 0 0
788 0 $self->line( $p2x, $p2y );
789 0 $self->arc( $xc, $yc, $r, $pa, $pa + $aa );
790 0 $self->line( $p4x, $p4y );
791 0 return 1;
792 }
793 0 return 0;
794 }
795  
796  
797 # ===========================================================================
798 # CIRCLE AND ELLIPSE
799 # ===========================================================================
800  
801 0 ▶sub circle ($self, $x, $y, $r) {
Subroutine circle: not called CC=2 SCAR=17.9
Pod undocumented
802 0 ▶ if ( !defined $r ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r) 0 0 0
803 0 $self->_croak('bad circle - need x, y, r');
804 0 return 0;
805 }
806 0 $self->ellipse( $x, $y, $r, $r );
807 0 return 1;
808 }
809  
810 0 ▶sub ellipse ($self, $x, $y, $a, $b) {
Subroutine ellipse: not called CC=2 SCAR=17.9
Pod undocumented
811 0 ▶ if ( !defined $b ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $b) 0 0 0
812 0 $self->_croak('bad ellipse - need x, y, a, b');
813 0 return 0;
814 }
815 0 $self->polygon( ellipse_points( $x, $y, $a, $b ) );
816 0 return 1;
817 }
818  
819 # CIRCLE / ELLIPSE SEGMENT CALCULATION
820 # Return the number of line segments needed to approximate a circle of
821 # radius $r with a maximum deviation of $max_err from the true circle.
822 # The derivation: for a regular n-gon inscribed in a circle of radius r,
823 # the sagitta (max inward error) of each chord is r(1 - cos(Ï€/n)).
824 # Setting that equal to max_err and solving for n gives:
825 # n = ceil( π / acos(1 - max_err/r) )
826 # $max_err defaults to 0.25 user units if omitted.
827 # Returns an even number >= 8.
828 0 ▶sub circle_segments ($r, $max_err = undef) {
Subroutine circle_segments: not called CC=10 SCAR=47.0
Pod undocumented
829 0 ▶ return 4 if $r <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r <= 0 0 0 0
830 0 ▶ $max_err //= 0.25;
Condition0%0/2
$max_err //= "0.25"
l!l
00
MC/DC0%0/1
$max_err //= "0.25"
$max_err
Truth table0%0/2
A
A= $max_err
Aresult
11
00
831 0 ▶ $max_err = $r if $max_err > $r;
Branch: 1 branch
BranchTrueFalseTotal
if $max_err > $r 0 0 0
832 0 ▶ if ( $max_err / $r >= 0.5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_err / $r >= "0.5") 0 0 0
833 0 return 8; # octagon is close enough for very coarse tolerance
834 }
835 0 my $ratio = 1.0 - $max_err / $r;
836 0 ▶ $ratio = 1.0 if $ratio > 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $ratio > 1 0 0 0
837 0 ▶ $ratio = -1.0 if $ratio < -1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $ratio < -1 0 0 0
838 0 my $half_theta = acos($ratio);
839 0 ▶ if ( $half_theta <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($half_theta <= 0) 0 0 0
840 # Extremely small error: one segment per unit of circumference
841 0 return int( ceil( 2 * $PI * $r ) );
842 }
843 0 my $n = int( ceil( $PI / $half_theta ) );
844 0 ▶ $n = 8 if $n < 8;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 8 0 0 0
845 0 $n += $n % 2; # keep even
846 0 return $n;
847 }
848  
849 # Return the number of segments for an ellipse with semi-axes $a and $b.
850 # Uses the larger radius as the governing dimension.
851 0 ▶sub ellipse_segments ($a, $b, $max_err = undef) {
Subroutine ellipse_segments: not called CC=3 SCAR=24.8
Pod undocumented
852 0 ▶ my $r = ( $a > $b ) ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
$a > $b ? : 0 0 0
853 0 return circle_segments( $r, $max_err );
854 }
855  
856 # Return a flat, closed list of (x,y,...,x0,y0) vertices for a circle.
857 # The list is closed (first point repeated at the end) so it can be
858 # passed directly to polygon(). $max_err controls rendering accuracy.
859 0 ▶sub circle_points ($cx, $cy, $r, $max_err = undef) {
Subroutine circle_points: not called CC=3 SCAR=24.8
Pod undocumented
860 0 my $n = circle_segments( $r, $max_err );
861 0 my @pts;
862 0 for my $i ( 0 .. $n - 1 ) {
863 0 my $t = 2.0 * $PI * $i / $n;
864 0 push @pts, $cx + $r * cos($t), $cy + $r * sin($t);
865 }
866 0 push @pts, $pts[0], $pts[1]; # close the polygon
867 0 return @pts;
868 }
869  
870 # Return a flat, closed list of (x,y,...,x0,y0) vertices for an ellipse.
871 # The list is closed (first point repeated at the end) so it can be
872 # passed directly to polygon(). $max_err controls rendering accuracy.
873 0 ▶sub ellipse_points ($cx, $cy, $a, $b, $max_err = undef) {
Subroutine ellipse_points: not called CC=3 SCAR=24.8
Pod undocumented
874 0 my $n = ellipse_segments( $a, $b, $max_err );
875 0 my @pts;
876 0 for my $i ( 0 .. $n - 1 ) {
877 0 my $t = 2.0 * $PI * $i / $n;
878 0 push @pts, $cx + $a * cos($t), $cy + $b * sin($t);
879 }
880 0 push @pts, $pts[0], $pts[1]; # close the polygon
881 0 return @pts;
882 }
883  
884  
885 # ===========================================================================
886 # TRANSFORMATIONS (CTM)
887 # ===========================================================================
888  
889 # Rotate about a point ($rxx,$ryy) in user coordinates, or the origin if omitted.
890 # Code from Hearn's "Computer Graphics with C".
891 0 ▶sub rotate ($self, $a, $rxx = undef, $ryy = undef) {
Subroutine rotate: not called CC=5 SCAR=34.0
Pod undocumented
892 0 my @m;
893 0 my ( $rx, $ry );
894 0 ▶ if ( !defined $a ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a) 0 0 0
895 0 $self->_croak('bad rotate - need 1 or 3 parameters');
896 0 return 0;
897 }
898 0 ▶ if ( !defined $rxx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rxx) { } 0 0 0
899 0 ( $rx, $ry ) = $self->_u_to_p( 0, 0 );
900 }
901 else {
902 0 ( $rx, $ry ) = $self->_u_to_p( $rxx, $ryy );
903 }
904 0 $a = radians($a);
905 0 @m = ( [1, 0, 0], [0, 1, 0], [0, 0, 1] );
906 0 $m[0][0] = cos $a;
907 0 $m[0][1] = -sin $a;
908 0 $m[0][2] = $rx * ( 1 - cos $a ) + $ry * sin $a;
909 0 $m[1][0] = sin $a;
910 0 $m[1][1] = cos $a;
911 0 $m[1][2] = $ry * ( 1 - cos $a ) - $rx * sin $a;
912 0 $self->_premulmat( \@m, \@{ $self->{CTM} } );
913 0 return 1;
914 }
915  
916 # Reset the CTM to the identity matrix.
917 0 ▶sub initmatrix ($self) {
Subroutine initmatrix: not called CC=1 SCAR=6.9
Pod undocumented
918 0 $self->{CTM} = [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ];
919 0 return 1;
920 }
921  
922 # Move the point ($tx,$ty) in user space to the origin.
923 0 ▶sub translate ($self, $tx, $ty) {
Subroutine translate: not called CC=2 SCAR=17.9
Pod undocumented
924 0 ▶ if ( !defined $ty ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ty) 0 0 0
925 0 $self->_croak('wrong number of args for translate');
926 0 return 0;
927 }
928 0 $self->moveto( $tx, $ty );
929 0 $self->translateC();
930 0 return 1;
931 }
932  
933 # Move the current page location (in user coords) to the origin.
934 0 ▶sub translateC ($self) {
Subroutine translateC: not called CC=1 SCAR=6.9
Pod undocumented
935 0 my ( $x, $y ) = $self->currentpoint();
936 0 my ( $v, $w ) = $self->_u_to_p( $x, $y );
937 0 $self->{CTM}[0][2] = $v;
938 0 $self->{CTM}[1][2] = $w;
939 0 $self->currentpoint( 0, 0 );
940 0 return 1;
941 }
942  
943 # Scale about ($rxx,$ryy) or the origin. $sy defaults to $sx if omitted.
944 0 ▶sub scale ($self, $sx, $sy = undef, $rxx = undef, $ryy = undef) {
Subroutine scale: not called CC=8 SCAR=42.8
Pod undocumented
945 0 my ( $rx, $ry );
946 0 my @ma;
947 0 ▶ if ( defined $rxx && !defined $ryy ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rxx and not defined $ryy) 0 0 0
Condition0%0/3
defined $rxx and not defined $ryy
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rxx and not defined $ryy
defined $rxx not defined $ryy
Truth table0%0/3
A and not(B)
A= defined $rxxB= not defined $ryy
ABresult
0X0
100
111
948 0 $self->_croak('bad scaling - need 1, 2 or 4 parameters');
949 0 return 0;
950 }
951 0 ▶ if ( !defined $sy ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $sy) { } 0 0 0
elsif (not defined $rxx) { } 0 0 0
952 0 $sy = $sx;
953 0 ( $rx, $ry ) = $self->_u_to_p( 0, 0 );
954 }
955 elsif ( !defined $rxx ) {
956 0 ( $rx, $ry ) = $self->_u_to_p( 0, 0 );
957 }
958 else {
959 0 ( $rx, $ry ) = $self->_u_to_p( $rxx, $ryy );
960 }
961 0 @ma = ( [1, 0, 0], [0, 1, 0], [0, 0, 1] );
962 0 $ma[0][0] = $sx;
963 0 $ma[0][2] = ( 1 - $sx ) * $rx;
964 0 $ma[1][1] = $sy;
965 0 $ma[1][2] = ( 1 - $sy ) * $ry;
966 0 $self->_premulmat( \@ma, \@{ $self->{CTM} } );
967 0 return 1;
968 }
969  
970 # Shear in the X direction by $deg degrees.
971 0 ▶sub skewX ($self, $deg) {
Subroutine skewX: not called CC=2 SCAR=17.9
Pod undocumented
972 0 ▶ if ( !defined $deg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $deg) 0 0 0
973 0 $self->_croak('wrong number of args for skewX');
974 0 return 0;
975 }
976 0 my $tana = tan radians($deg);
977 0 my @matrix = ( [1, $tana, 0], [0, 1, 0], [0, 0, 1] );
978 0 $self->_premulmat( \@matrix, \@{ $self->{CTM} } );
979 0 return 1;
980 }
981  
982 # Shear in the Y direction by $deg degrees.
983 0 ▶sub skewY ($self, $deg) {
Subroutine skewY: not called CC=2 SCAR=17.9
Pod undocumented
984 0 ▶ if ( !defined $deg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $deg) 0 0 0
985 0 $self->_croak('wrong number of args for skewY');
986 0 return 0;
987 }
988 0 my $tana = tan radians($deg);
989 0 my @matrix = ( [1, 0, 0], [$tana, 1, 0], [0, 0, 1] );
990 0 $self->_premulmat( \@matrix, \@{ $self->{CTM} } );
991 0 return 1;
992 }
993  
994 # Premultiply matrix $a into matrix $b (result stored in $b).
995 0 ▶sub _premulmat ($self, $aref, $bref) {
Subroutine _premulmat: not called CC=5 SCAR=34.0
996 0 my @a = @{$aref};
997 0 my @b = @{$bref};
998 0 my @tmp;
999 0 foreach my $r ( 0 .. 2 ) {
1000 0 foreach my $c ( 0 .. 2 ) {
1001 0 $tmp[$r][$c] =
1002 $a[$r][0] * $b[0][$c] +
1003 $a[$r][1] * $b[1][$c] +
1004 $a[$r][2] * $b[2][$c];
1005 }
1006 }
1007 0 foreach my $r ( 0 .. 2 ) {
1008 0 foreach my $c ( 0 .. 2 ) {
1009 0 $bref->[$r][$c] = $tmp[$r][$c];
1010 }
1011 }
1012 0 return 1;
1013 }
1014  
1015 # Apply the CTM to an array of $npts points stored as a flat (x0,y0,x1,y1,...) list.
1016 0 ▶sub _transform ($self, $npts, $ptsref) {
Subroutine _transform: not called CC=2 SCAR=17.9
1017 0 foreach my $k ( 0 .. $npts - 1 ) {
1018 my $tmp =
1019 $self->{CTM}->[0][0] * $ptsref->[ 2 * $k ] +
1020 $self->{CTM}->[0][1] * $ptsref->[ 2 * $k + 1 ] +
1021 0 $self->{CTM}->[0][2];
1022 $ptsref->[ 2 * $k + 1 ] =
1023 $self->{CTM}->[1][0] * $ptsref->[ 2 * $k ] +
1024 $self->{CTM}->[1][1] * $ptsref->[ 2 * $k + 1 ] +
1025 0 $self->{CTM}->[1][2];
1026 0 $ptsref->[ 2 * $k ] = $tmp;
1027 }
1028 0 return 1;
1029 }
1030  
1031  
1032 # ===========================================================================
1033 # ARC / FILLET GEOMETRY HELPERS
1034 # ===========================================================================
1035  
1036 # Signed cross product of two 2-D vectors.
1037 0 ▶sub _cross2 ($self, $v1x, $v1y, $v2x, $v2y) {
Subroutine _cross2: not called CC=1 SCAR=6.9
1038 0 return ( $v1x * $v2y - $v2x * $v1y );
1039 }
1040  
1041 # Angle subtended by two vectors: cos(a) = u·v / (‖u‖·‖v‖).
1042 0 ▶sub _dot2 ($self, $ux, $uy, $vx, $vy) {
Subroutine _dot2: not called CC=2 SCAR=17.9
1043 0 my $d = sqrt( ( $ux*$ux + $uy*$uy ) * ( $vx*$vx + $vy*$vy ) );
1044 0 ▶ return 0.0 if $d == 0.0;
Branch: 1 branch
BranchTrueFalseTotal
if $d == 0 0 0 0
1045 0 return acos( ( $ux*$vx + $uy*$vy ) / $d );
1046 }
1047  
1048 # Find a,b,c for Ax + By + C = 0 through p1 and p2.
1049 0 ▶sub _linecoefs ($self, $p1x, $p1y, $p2x, $p2y) {
Subroutine _linecoefs: not called CC=1 SCAR=6.9
1050 0 my $c = ( $p2x * $p1y ) - ( $p1x * $p2y );
1051 0 my $a = $p2y - $p1y;
1052 0 my $b = $p1x - $p2x;
1053 0 return ( $a, $b, $c );
1054 }
1055  
1056 # Return signed distance from line Ax + By + C = 0 to point P.
1057 0 ▶sub _linetopoint ($self, $a, $b, $c, $px, $py) {
Subroutine _linetopoint: not called CC=2 SCAR=17.9
1058 0 my $d = sqrt( $a*$a + $b*$b );
1059 0 ▶ return 0.0 if $d == 0.0;
Branch: 1 branch
BranchTrueFalseTotal
if $d == 0 0 0 0
1060 0 return ( $a*$px + $b*$py + $c ) / $d;
1061 }
1062  
1063 # Given line ax + by + c = 0 and point p, find the foot of the perpendicular from p.
1064 0 ▶sub _pointperp ($self, $a, $b, $c, $px, $py) {
Subroutine _pointperp: not called CC=2 SCAR=17.9
1065 0 my ( $x, $y ) = ( 0.0, 0.0 );
1066 0 my $d = $a*$a + $b*$b;
1067 0 my $cp = $a*$py - $b*$px;
1068 0 ▶ if ( $d != 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d != 0) 0 0 0
1069 0 $x = ( -$a*$c - $b*$cp ) / $d;
1070 0 $y = ( $a*$cp - $b*$c ) / $d;
1071 }
1072 0 return ( $x, $y );
1073 }
1074  
1075 # Compute a circular arc fillet between lines L1 (p1..p2) and L2 (p3..p4) with radius $r.
1076 # Returns the 8 clipped endpoint coordinates plus the arc centre and angle, or undef on failure.
1077 # Miller, "Joining Two Lines with a Circular Arc Fillet," Graphics Gems III.
1078 0 ▶sub _fillet ($self, $p1x, $p1y, $p2x, $p2y, $p3x, $p3y, $p4x, $p4y, $r) {
Subroutine _fillet: not called CC=7 SCAR=40.3
1079 0 my ( $a1, $b1, $c1, $a2, $b2, $c2, $c1p, $c2p, $d1, $d2, $xa, $xb, $ya, $yb, $d, $rr );
1080 0 my ( $mpx, $mpy, $pcx, $pcy, $gv1x, $gv1y, $gv2x, $gv2y, $xc, $yc, $pa, $aa );
1081 0 ( $a1, $b1, $c1 ) = $self->_linecoefs( $p1x, $p1y, $p2x, $p2y );
1082 0 ( $a2, $b2, $c2 ) = $self->_linecoefs( $p3x, $p3y, $p4x, $p4y );
1083 0 ▶ if ( ( $a1 * $b2 ) == ( $a2 * $b1 ) ) { return (undef) } # parallel or coincident
Branch: 1 branch
BranchTrueFalseTotal
if ($a1 * $b2 == $a2 * $b1) 0 0 0
1084 0 $mpx = ( $p3x + $p4x ) / 2.0;
1085 0 $mpy = ( $p3y + $p4y ) / 2.0;
1086 0 $d1 = $self->_linetopoint( $a1, $b1, $c1, $mpx, $mpy );
1087 0 ▶ if ( $d1 == 0.0 ) { return (undef) x 12 }
Branch: 1 branch
BranchTrueFalseTotal
if ($d1 == 0) 0 0 0
1088 0 $mpx = ( $p1x + $p2x ) / 2.0;
1089 0 $mpy = ( $p1y + $p2y ) / 2.0;
1090 0 $d2 = $self->_linetopoint( $a2, $b2, $c2, $mpx, $mpy );
1091 0 ▶ if ( $d2 == 0.0 ) { return (undef) x 12 }
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 == 0) 0 0 0
1092 0 ▶ $rr = ( $d1 <= 0.0 ) ? -$r : $r;
Branch: 1 branch
BranchTrueFalseTotal
$d1 <= 0.0 ? : 0 0 0
1093 0 $c1p = $c1 - $rr * sqrt( $a1*$a1 + $b1*$b1 );
1094 0 ▶ $rr = ( $d2 <= 0.0 ) ? -$r : $r;
Branch: 1 branch
BranchTrueFalseTotal
$d2 <= 0.0 ? : 0 0 0
1095 0 $c2p = $c2 - $rr * sqrt( $a2*$a2 + $b2*$b2 );
1096 0 $d = $a1*$b2 - $a2*$b1;
1097 0 $xc = ( $c2p*$b1 - $c1p*$b2 ) / $d;
1098 0 $yc = ( $c1p*$a2 - $c2p*$a1 ) / $d;
1099 0 $pcx = $xc;
1100 0 $pcy = $yc;
1101 0 ( $xa, $ya ) = $self->_pointperp( $a1, $b1, $c1, $pcx, $pcy );
1102 0 ( $xb, $yb ) = $self->_pointperp( $a2, $b2, $c2, $pcx, $pcy );
1103 0 $p2x = $xa; $p2y = $ya;
1104 0 $p3x = $xb; $p3y = $yb;
1105 0 $gv1x = $xa - $xc; $gv1y = $ya - $yc;
1106 0 $gv2x = $xb - $xc; $gv2y = $yb - $yc;
1107 0 $pa = atan2( $gv1y, $gv1x );
1108 0 $aa = $self->_dot2( $gv1x, $gv1y, $gv2x, $gv2y );
1109 0 ▶ if ( $self->_cross2( $gv1x, $gv1y, $gv2x, $gv2y ) < 0.0 ) { $aa = -$aa }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cross2($gv1x, $gv1y, $gv2x, $gv2y) < 0) 0 0 0
1110 0 return ( $p1x, $p1y, $p2x, $p2y, $p3x, $p3y, $p4x, $p4y,
1111 $xc, $yc, $pa * $R2D, $aa * $R2D );
1112 }
1113  
1114  
1115 # ===========================================================================
1116 # FLOATING-POINT UTILITIES
1117 # ===========================================================================
1118  
1119 # Floating-point absolute value.
1120 0 ▶sub _fabs ($val) {
Subroutine _fabs: not called CC=2 SCAR=17.9
1121 0 ▶ return ( $val >= 0.0 ) ? $val : -$val;
Branch: 1 branch
BranchTrueFalseTotal
$val >= 0.0 ? : 0 0 0
1122 }
1123  
1124 # Floating-point equality within $EPSILON.
1125 0 ▶sub _feq ($x, $y) {
Subroutine _feq: not called CC=2 SCAR=17.9
1126 0 ▶ return ( _fabs( $x - $y ) < $EPSILON ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_fabs($x - $y) < $EPSILON ? : 0 0 0
1127 }
1128  
1129 # Square of the Euclidean distance between two points.
1130 0 ▶sub _calc_sq_distance ($x1, $y1, $x2, $y2) {
Subroutine _calc_sq_distance: not called CC=1 SCAR=6.9
1131 0 return ( $x2 - $x1 )**2 + ( $y2 - $y1 )**2;
1132 }
1133  
1134  
1135 # ===========================================================================
1136 # UNIT CONVERSION
1137 # ===========================================================================
1138  
1139 # Convert a value in PostScript points to the current drawing unit.
1140 0 ▶sub _pt_unitfied ($self, $p) {
Subroutine _pt_unitfied: not called CC=1 SCAR=6.9
1141 0 return $p * $inches_to_unit{ $self->{units} } / $inches_to_unit{pt};
1142 }
1143  
1144 # Convert a value in inches to the current drawing unit.
1145 0 ▶sub _in_unitfied ($self, $p) {
Subroutine _in_unitfied: not called CC=1 SCAR=6.9
1146 0 return $p * $inches_to_unit{ $self->{units} };
1147 }
1148  
1149  
1150 # ===========================================================================
1151 # ANGLE CONVERSION (public utility functions)
1152 # ===========================================================================
1153  
1154 # Convert degrees to radians.
1155 0 ▶sub radians ($deg) { return $deg * $D2R }
Subroutine radians: not called CC=1 SCAR=6.9
Pod undocumented
1156  
1157 # Convert radians to degrees.
1158 0 ▶sub degrees ($rad) { return $rad / $D2R }
Subroutine degrees: not called CC=1 SCAR=6.9
Pod undocumented
1159  
1160  
1161 # ===========================================================================
1162 # SEGMENT INTERSECTION (Cramer's Rule)
1163 # ===========================================================================
1164  
1165 # Return the parametric intersection parameter t of two line segments,
1166 # or 0 if they do not intersect. Touching segments are not considered to intersect.
1167 #
1168 # Theory (parametric line representation):
1169 # P = P1 + k(P2-P1) Q = P3 + l(P4-P3)
1170 # Intersection when P=Q:
1171 # k = [ (x4-x3)(y1-y3) - (y4-y3)(x1-x3) ] / denom
1172 # l = [ (x2-x1)(y1-y3) - (y2-y1)(x1-x3) ] / denom
1173 # where denom = (y4-y3)(x2-x1) - (x4-x3)(y2-y1)
1174 # Intersection is valid when both k and l are in (0,1).
1175 0 ▶sub _getsegintersect ($self, $p0x, $p0y, $p1x, $p1y, $p2x, $p2y, $p3x, $p3y) {
Subroutine _getsegintersect: not called CC=6 SCAR=37.4
1176 0 my ( $s02x, $s02y, $s10x, $s10y, $s32x, $s32y, $s_numer, $t_numer, $denom );
1177 0 $s10x = $p1x - $p0x;
1178 0 $s10y = $p1y - $p0y;
1179 0 $s32x = $p3x - $p2x;
1180 0 $s32y = $p3y - $p2y;
1181 0 $denom = $s10x * $s32y - $s32x * $s10y;
1182 0 ▶ return 0 if $denom == 0; # collinear
Branch: 1 branch
BranchTrueFalseTotal
if $denom == 0 0 0 0
1183 0 my $denomPositive = ( $denom > 0 );
1184 0 $s02x = $p0x - $p2x;
1185 0 $s02y = $p0y - $p2y;
1186 0 $s_numer = $s10x * $s02y - $s10y * $s02x;
1187 0 ▶ return 0 if ( ( $s_numer < 0 ) == $denomPositive );
Branch: 1 branch
BranchTrueFalseTotal
if $s_numer < 0 == $denomPositive 0 0 0
1188 0 $t_numer = $s32x * $s02y - $s32y * $s02x;
1189 0 ▶ return 0 if ( ( $t_numer < 0 ) == $denomPositive );
Branch: 1 branch
BranchTrueFalseTotal
if $t_numer < 0 == $denomPositive 0 0 0
1190 0 ▶ return 0 if ( ( $s_numer > $denom ) == $denomPositive
Branch: 1 branch
BranchTrueFalseTotal
if $s_numer > $denom == $denomPositive or $t_numer > $denom == $denomPositive 0 0 0
Condition0%0/3
$s_numer > $denom == $denomPositive or $t_numer > $denom == $denomPositive
l!l&&r!l&&!r
000
MC/DC0%0/2
$s_numer > $denom == $denomPositive or $t_numer > $denom == $denomPositive
$s_numer > $denom == $denomPositive $t_numer > $denom == $denomPositive
Truth table0%0/3
A or B
A= $s_numer > $denom == $denomPositiveB= $t_numer > $denom == $denomPositive
ABresult
1X1
011
000
1191 || ( $t_numer > $denom ) == $denomPositive );
1192 0 return $t_numer / $denom;
1193 }
1194  
1195  
1196 # ===========================================================================
1197 # LIANG-BARSKY LINE CLIPPING
1198 # ===========================================================================
1199  
1200 # Clip a line segment to an axis-aligned rectangle.
1201 #
1202 # ($x1,$y1,$x2,$y2,$info) =
1203 # $obj->_LiangBarsky($botx,$boty,$topx,$topy, $x0src,$y0src,$x1src,$y1src);
1204 #
1205 # $info values:
1206 # 1 entire segment inside boundary
1207 # 2 entirely outside (returns -1,-1,-1,-1,2)
1208 # 3 start inside, end clipped
1209 # 4 end inside, start clipped
1210 # 5 both endpoints outside but interior intersects
1211 #
1212 # Algorithm by Daniel White (skytopia.com), bug-fixed and translated to Perl.
1213 0 ▶sub _LiangBarsky ($self, $botx, $boty, $topx, $topy, $x0src, $y0src, $x1src, $y1src) {
Subroutine _LiangBarsky: not called CC=22 SCAR=62.3
1214 0 my $t0 = 0.0;
1215 0 my $t1 = 1.0;
1216 0 my $xdelta = $x1src - $x0src;
1217 0 my $ydelta = $y1src - $y0src;
1218 0 my ( $p, $q, $r );
1219 0 my $info = 0;
1220 0 foreach my $edge ( 0 .. 3 ) {
1221 0 ▶ if ( $edge == 0 ) { $p = -$xdelta; $q = -( $botx - $x0src ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($edge == 0) 0 0 0
1222 0 ▶ if ( $edge == 1 ) { $p = $xdelta; $q = $topx - $x0src }
Branch: 1 branch
BranchTrueFalseTotal
if ($edge == 1) 0 0 0
1223 0 ▶ if ( $edge == 2 ) { $p = -$ydelta; $q = -( $boty - $y0src ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($edge == 2) 0 0 0
1224 0 ▶ if ( $edge == 3 ) { $p = $ydelta; $q = $topy - $y0src }
Branch: 1 branch
BranchTrueFalseTotal
if ($edge == 3) 0 0 0
1225 0 ▶ if ( $p == 0 && $q < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p == 0 and $q < 0) 0 0 0
Condition0%0/3
$p == 0 and $q < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$p == 0 and $q < 0
$p == 0 $q < 0
Truth table0%0/3
A and B
A= $p == 0B= $q < 0
ABresult
0X0
100
111
1226 0 return ( -1, -1, -1, -1, 2 ); # parallel and outside
1227 }
1228 0 ▶ if ( $p < 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p < 0) { } 0 0 0
elsif ($p > 0) { } 0 0 0
1229 0 $r = 1.0 * $q / $p;
1230 0 ▶ return ( -1, -1, -1, -1, 2 ) if $r > $t1;
Branch: 1 branch
BranchTrueFalseTotal
if $r > $t1 0 0 0
1231 0 ▶ $t0 = $r if $r > $t0; # clip start
Branch: 1 branch
BranchTrueFalseTotal
if $r > $t0 0 0 0
1232 }
1233 elsif ( $p > 0 ) {
1234 0 $r = 1.0 * $q / $p;
1235 0 ▶ return ( -1, -1, -1, -1, 2 ) if $r < $t0;
Branch: 1 branch
BranchTrueFalseTotal
if $r < $t0 0 0 0
1236 0 ▶ $t1 = $r if $r < $t1; # clip end
Branch: 1 branch
BranchTrueFalseTotal
if $r < $t1 0 0 0
1237 }
1238 }
1239 0 ▶ $info = 1 if $t0 == 0.0 && $t1 == 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $t0 == 0 and $t1 == 1 0 0 0
Condition0%0/3
$t0 == 0 and $t1 == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$t0 == 0 and $t1 == 1
$t0 == 0 $t1 == 1
Truth table0%0/3
A and B
A= $t0 == 0B= $t1 == 1
ABresult
0X0
100
111
1240 0 ▶ $info = 3 if $t0 == 0.0 && $t1 != 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $t0 == 0 and $t1 != 1 0 0 0
Condition0%0/3
$t0 == 0 and $t1 != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$t0 == 0 and $t1 != 1
$t0 == 0 $t1 != 1
Truth table0%0/3
A and B
A= $t0 == 0B= $t1 != 1
ABresult
0X0
100
111
1241 0 ▶ $info = 4 if $t0 != 0.0 && $t1 == 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $t0 != 0 and $t1 == 1 0 0 0
Condition0%0/3
$t0 != 0 and $t1 == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$t0 != 0 and $t1 == 1
$t0 != 0 $t1 == 1
Truth table0%0/3
A and B
A= $t0 != 0B= $t1 == 1
ABresult
0X0
100
111
1242 0 ▶ $info = 5 if $t0 != 0.0 && $t1 != 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $t0 != 0 and $t1 != 1 0 0 0
Condition0%0/3
$t0 != 0 and $t1 != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$t0 != 0 and $t1 != 1
$t0 != 0 $t1 != 1
Truth table0%0/3
A and B
A= $t0 != 0B= $t1 != 1
ABresult
0X0
100
111
1243 return (
1244 0 $x0src + $t0 * $xdelta,
1245 $y0src + $t0 * $ydelta,
1246 $x0src + $t1 * $xdelta,
1247 $y0src + $t1 * $ydelta,
1248 $info,
1249 );
1250 }
1251  
1252  
1253 9 1;
1254  
1255 __END__
« lib/Graphics/Penplotter/GcodeXY/Split.pm ↑ Summary lib/Graphics/Penplotter/GcodeXY/Hatch.pm »