File Coverage

blib/lib/Imager/Graph/Util.pm
Criterion Covered Total %
statement 58 58 100.0
branch 5 8 62.5
condition 4 9 44.4
subroutine 7 7 100.0
pod 3 3 100.0
total 77 85 90.5


line stmt bran cond sub pod time code
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 4     4   25 use strict;
  4         910  
  4         630  
45 4     4   25 use vars qw(@ISA @EXPORT);
  4         7  
  4         340  
46             @ISA = qw(Exporter);
47             require Exporter;
48             @EXPORT = qw(intersect_lines intersect_line_and_circle
49             line_from_points);
50 4     4   24 use Carp;
  4         6  
  4         436  
51 4     4   23 use constant DEBUG => 0;
  4         261  
  4         6650  
52              
53             sub line_from_points {
54 496     496 1 512 my ($x1, $y1, $x2, $y2) = @_;
55              
56 496         483 my $A = $y1 - $y2;
57 496         434 my $B = $x2 - $x1;
58 496         500 my $C = $x1 * $y2 - $y1 * $x2;
59              
60 496 50 66     1267 return () if $A == 0 && $B == 0;
61              
62 496         1380 return ($A, $B, $C);
63             }
64              
65             sub intersect_lines {
66 239     239 1 270 my ($a1, $b1, $c1, $a2, $b2, $c2) = @_;
67              
68 239         155 DEBUG and !defined($a1) and croak('$a1 undefined');
69 239         155 DEBUG and !defined($b1) and croak('$b1 undefined');
70 239         159 DEBUG and !defined($c1) and croak('$c1 undefined');
71 239         155 DEBUG and !defined($a2) and croak('$a2 undefined');
72 239         154 DEBUG and !defined($b2) and croak('$b2 undefined');
73 239         162 DEBUG and !defined($c2) and croak('$c2 undefined');
74              
75 239         272 my $divisor = $a2 * $b1 - $a1 * $b2;
76 239 100       417 return () if $divisor == 0;
77              
78 237         289 my $x = ($b2 * $c1 - $b1 * $c2) / $divisor;
79 237         258 my $y = ($a1 * $c2 - $a2 * $c1) / $divisor;
80              
81 237         646 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     204 1 251 my ($a, $b, $c, $cx, $cy, $r) = @_;
106              
107 204         151 DEBUG and !defined($a) and croak('$a undefined');
108 204         135 DEBUG and !defined($b) and croak('$b undefined');
109 204         145 DEBUG and !defined($c) and croak('$c undefined');
110 204         140 DEBUG and !defined($cx) and croak('$cx undefined');
111 204         134 DEBUG and !defined($cy) and croak('$cy undefined');
112 204         137 DEBUG and !defined($r) and croak('$r undefined');
113              
114             # I should probably optimize the following
115 204         228 my $qya = $b * $b + $a * $a;
116 204         322 my $qyb = 2 * $b * $c + 2 * $a * $b * $cx - 2 * $a * $a * $cy;
117 204         402 my $qyc = $c * $c + 2 * $a * $c * $cx + $a * $a * $cy * $cy
118             + $a * $a * $cx * $cx - $r * $r * $a * $a;
119              
120 204         183 my $qxa = $b * $b + $a * $a;
121 204         298 my $qxb = 2 * $a * $c + 2 * $a * $b * $cy - 2 * $b * $b * $cx;
122 204         388 my $qxc = $c * $c + 2 * $b * $c * $cy + $b * $b * $cx * $cx
123             + $b * $b * $cy * $cy - $r * $r * $b * $b;
124              
125 204         257 my $dety = $qyb * $qyb - 4 * $qya * $qyc;
126 204         225 my $detx = $qxb * $qxb - 4 * $qxa * $qxc;
127              
128 204 50 33     679 return () if $dety < 0 || $detx < 0;
129            
130 204         237 my $detyroot = sqrt($dety);
131 204         167 my $detxroot = sqrt($detx);
132              
133 204         286 my $y1 = (- $qyb - $detyroot) / ( 2 * $qya);
134 204         249 my $x1 = (- $qxb - $detxroot) / ( 2 * $qxa);
135              
136 204         163 DEBUG and abs($a * $x1 + $b * $y1 + $c) > 0.00001
137             and print "(x1 $x1, y1 $y1) not on line\n";
138 204         126 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 50 33     388 return ($x1, $y1) if $detxroot == 0 && $detyroot == 0;
142            
143 204         251 my $y2 = (- $qyb + $detyroot) / (2 * $qya);
144 204         243 my $x2 = (- $qxb + $detxroot) / (2 * $qxa);
145            
146 204         132 DEBUG and abs($a * $x2 + $b * $y2 + $c) > 0.00001
147             and print "(x2 $x2, y2 $y2) not on line\n";
148 204         127 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         630 return ($x1, $y1, $x2, $y2);
152             }
153              
154             __END__