Graph/Util.pm

filter: Statementstmt 100% 61 / 61 Branchbran 62.5% 5 / 8 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 85.1% 80 / 94 SCAR 20.9
CC
8
Cov
90%
intersect_line_and_circle
5.1
line_from_points
3.1
intersect_lines
2.0
CRAP 8.1
« Graph/Pie.pm ↑ Summary Graph/Area.pm »
1 =head1 NAME
2  
3 Imager::Graph::Util - simple geometric functions
4  
5 =head1 SYNOPSIS
6  
7 my @abc = line_from_points($x1, $y1, $x2, $y2);
8 my @p = intersect_lines(@abc1, @abc2);
9 my @points = intersect_line_and_circle(@abc1, $cx, $cy, $radius);
10  
11 =head1 DESCRIPTION
12  
13 Provides some simple geometric functions intended for use in drawing
14 graphs.
15  
16 =over
17  
18 =item line_from_points($x1, $y1, $x2, $y2)
19  
20 Returns the coefficients of a line in the Ax + By + C = 0 form.
21  
22 Returns the list (A, B, C), or an empty list if they are the same
23 point.
24  
25 =item intersect_lines(@abc1, @abc2)
26  
27 Returns the point of intersection of the 2 lines, each given in
28 Ax+By+C=0 form. Returns either the point (x, y) or an empty list.
29  
30 =item intersect_line_and_circle(@abc, $cx, $cy, $radius)
31  
32 Returns the points or point of intersection of the given line and
33 circle.
34  
35 =back
36  
37 =head1 INTERNALS
38  
39 =over
40  
41 =cut
42  
43 package Imager::Graph::Util;
44 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 5 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 5 @ISA = qw(Exporter);
47 5 require Exporter;
48 5 @EXPORT = qw(intersect_lines intersect_line_and_circle
49 line_from_points);
50 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 5 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 sub line_from_points {
54 496 ▶ my ($x1, $y1, $x2, $y2) = @_;
Subroutine line_from_points: called CC=3 SCAR=11.2
Pod documented
55  
56 496 my $A = $y1 - $y2;
57 496 my $B = $x2 - $x1;
58 496 my $C = $x1 * $y2 - $y1 * $x2;
59  
60 496 ▶ return () if $A == 0 && $B == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $A == 0 and $B == 0 0 496 496
Condition66%2/3
$A == 0 and $B == 0
!ll&&!rl&&r
3061900
MC/DC0%0/2
$A == 0 and $B == 0
$A == 0 $B == 0
Truth table66%2/3
A and B
A= $A == 0B= $B == 0
ABresult
0X0
100
111
61  
62 496 return ($A, $B, $C);
63 }
64  
65 sub intersect_lines {
66 239 ▶ my ($a1, $b1, $c1, $a2, $b2, $c2) = @_;
Subroutine intersect_lines: called CC=2 SCAR=6.9
Pod documented
67  
68 239 DEBUG and !defined($a1) and croak('$a1 undefined');
69 239 DEBUG and !defined($b1) and croak('$b1 undefined');
70 239 DEBUG and !defined($c1) and croak('$c1 undefined');
71 239 DEBUG and !defined($a2) and croak('$a2 undefined');
72 239 DEBUG and !defined($b2) and croak('$b2 undefined');
73 239 DEBUG and !defined($c2) and croak('$c2 undefined');
74  
75 239 my $divisor = $a2 * $b1 - $a1 * $b2;
76 239 ▶ return () if $divisor == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $divisor == 0 2 237 239
77  
78 237 my $x = ($b2 * $c1 - $b1 * $c2) / $divisor;
79 237 my $y = ($a1 * $c2 - $a2 * $c1) / $divisor;
80  
81 237 return ($x, $y);
82 }
83  
84 =item intersect_line_and_circle()
85  
86 The implementation is a little heavy on math. Perhaps there was a
87 better way to implement it.
88  
89 Starting with the equations of a line and that of a circle:
90  
91 (1) Ax + By + C = 0
92 (2) (x - x1)**2 + (y - y1)**2 = R ** 2
93 (3) Ax = -By - C # re-arrange (1)
94 (4) A**2 (x - x1)**2 + A**2 (y - y1)**2 = R**2 A**2 # (2) * A**2
95 (5) (Ax - Ax1)**2 + (Ay - Ay1)**2 = R**2 A**2 # move it inside
96 (6) (-By - C - Ax1)**2 + (Ay - Ay1)**2 = R**2 A**2 # sub (3) into (5)
97  
98 Expand and convert to standard quadratic form, and similary for x.
99  
100 Be careful :)
101  
102 =cut
103  
104 sub intersect_line_and_circle {
105 204 ▶ my ($a, $b, $c, $cx, $cy, $r) = @_;
Subroutine intersect_line_and_circle: called CC=5 SCAR=16.3
Pod documented
106  
107 204 DEBUG and !defined($a) and croak('$a undefined');
108 204 DEBUG and !defined($b) and croak('$b undefined');
109 204 DEBUG and !defined($c) and croak('$c undefined');
110 204 DEBUG and !defined($cx) and croak('$cx undefined');
111 204 DEBUG and !defined($cy) and croak('$cy undefined');
112 204 DEBUG and !defined($r) and croak('$r undefined');
113  
114 # I should probably optimize the following
115 204 my $qya = $b * $b + $a * $a;
116 204 my $qyb = 2 * $b * $c + 2 * $a * $b * $cx - 2 * $a * $a * $cy;
117 204 my $qyc = $c * $c + 2 * $a * $c * $cx + $a * $a * $cy * $cy
118 + $a * $a * $cx * $cx - $r * $r * $a * $a;
119  
120 204 my $qxa = $b * $b + $a * $a;
121 204 my $qxb = 2 * $a * $c + 2 * $a * $b * $cy - 2 * $b * $b * $cx;
122 204 my $qxc = $c * $c + 2 * $b * $c * $cy + $b * $b * $cx * $cx
123 + $b * $b * $cy * $cy - $r * $r * $b * $b;
124  
125 204 my $dety = $qyb * $qyb - 4 * $qya * $qyc;
126 204 my $detx = $qxb * $qxb - 4 * $qxa * $qxc;
127  
128 204 ▶ return () if $dety < 0 || $detx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dety < 0 or $detx < 0 0 204 204
Condition33%1/3
$dety < 0 or $detx < 0
l!l&&r!l&&!r
00204
MC/DC0%0/2
$dety < 0 or $detx < 0
$dety < 0 $detx < 0
Truth table33%1/3
A or B
A= $dety < 0B= $detx < 0
ABresult
1X1
011
000
129
130 204 my $detyroot = sqrt($dety);
131 204 my $detxroot = sqrt($detx);
132  
133 204 my $y1 = (- $qyb - $detyroot) / ( 2 * $qya);
134 204 my $x1 = (- $qxb - $detxroot) / ( 2 * $qxa);
135  
136 204 DEBUG and abs($a * $x1 + $b * $y1 + $c) > 0.00001
137 and print "(x1 $x1, y1 $y1) not on line\n";
138 204 DEBUG and abs(($x1-$cx)*($x1-$cx)+($y1-$cy)*($y1-$cy) - $r*$r) > 0.0001
139 and print "(x1 $x1, y1 $y1) not on circle\n";
140  
141 204 ▶ return ($x1, $y1) if $detxroot == 0 && $detyroot == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $detxroot == 0 and $detyroot == 0 0 204 204
Condition33%1/3
$detxroot == 0 and $detyroot == 0
!ll&&!rl&&r
20400
MC/DC0%0/2
$detxroot == 0 and $detyroot == 0
$detxroot == 0 $detyroot == 0
Truth table33%1/3
A and B
A= $detxroot == 0B= $detyroot == 0
ABresult
0X0
100
111
142
143 204 my $y2 = (- $qyb + $detyroot) / (2 * $qya);
144 204 my $x2 = (- $qxb + $detxroot) / (2 * $qxa);
145
146 204 DEBUG and abs($a * $x2 + $b * $y2 + $c) > 0.00001
147 and print "(x2 $x2, y2 $y2) not on line\n";
148 204 DEBUG and abs(($x2-$cx)*($x2-$cx)+($y2-$cy)*($y2-$cy) - $r*$r) > 0.0001
149 and print "(x2 $x2, y2 $y2) not on circle\n";
150  
151 204 return ($x1, $y1, $x2, $y2);
152 }
153  
154 __END__
« Graph/Pie.pm ↑ Summary Graph/Area.pm »