blib/lib/Math/Intersection/Circle/Line.pm

filter: Statementstmt 100% 94 / 94 Branchbran 82.3% 28 / 34 Conditioncond 70.1% 40 / 57 MC/DCmcdc 48.4% 16 / 33 Subroutinesub 100% 23 / 23 Podpod 100% 12 / 12 totaltotal 84.1% 213 / 253 CC 40 SCAR 17.0
CC
40
Cov
88%
near4
16.0
intersectionCircles
8.0
intersectionLines
5.3
CRAP 5.5
↑ Summary Build »
1 =pod
2  
3 =encoding utf8
4  
5 =head1 Name
6  
7 Math::Intersection::Circle::Line - Find the points at which circles and lines
8 intersect.
9  
10 =head1 Synopsis
11  
12 use Math::Intersection::Circle::Line q(:all);
13 use Test::More q(no_plan);
14 use utf8;
15  
16 # A line segment across a circle is never longer than the diameter
17  
18 if (1) # Random circle and random line
19 {my ($x, $y, $r, $𝘅, $𝘆, $𝕩, $𝕪) = map {rand()} 1..7;
20 intersectionCircleLine # Find intersection of a circle and a line
21 {return ok 1 unless @_ == 4; # Ignore line unless it crosses circle
22 ok &vectorLength(@_) <= 2*$r; # Length if line segment is less than or equal to that of a diameter
23 } $x, $y, $r, $𝘅, $𝘆, $𝕩, $𝕪; # Circle and line to be intersected
24 }
25  
26 # The length of a side of a hexagon is the radius of a circle inscribed through
27 # its vertices
28  
29 if (1)
30 {my ($x, $y, $r) = map {rand()} 1..3; # Random circle
31 my @p = intersectionCircles {@_} $x, $y, $r, $x+$r, $y, $r; # First step of one radius
32 my @𝗽 = intersectionCircles {@_} $x, $y, $r, $p[0], $p[1], $r; # Second step of one radius
33 my @q = !&near($x+$r, $y, @𝗽[0,1]) ? @𝗽[0,1] : @𝗽[2,3]; # away from start point
34 my @𝗾 = intersectionCircles {@_} $x, $y, $r, $q[0], $q[1], $r; # Third step of one radius
35 ok &near2(@𝗾[0,1], $x-$r, $y) or # Brings us to a point
36 &near2(@𝗾[2,3], $x-$r, $y); # opposite to the start point
37 }
38  
39 # Circle through three points chosen at random has the same centre regardless of
40 # the pairing of the points
41  
42 sub circleThrough3
43 {my ($x, $y, $𝘅, $𝘆, $𝕩, $𝕪) = @_; # Three points
44 &intersectionLines
45 (sub # Intersection of bisectors is the centre of the circle
46 {my @r =(&vectorLength(@_, $x, $y), # Radii from centre of circle to each point
47 &vectorLength(@_, $𝘅, $𝘆),
48 &vectorLength(@_, $𝕩, $𝕪));
49 ok &near(@r[0,1]); # Check radii are equal
50 ok &near(@r[1,2]);
51 @_ # Return centre
52 }, rotate90AroundMidPoint($x, $y, $𝘅, $𝘆), # Bisectors between pairs of points
53 rotate90AroundMidPoint($𝕩, $𝕪, $𝘅, $𝘆));
54 }
55  
56 if (1)
57 {my (@points) = map {rand()} 1..6; # Three points chosen at random
58 ok &near2(circleThrough3(@points), circleThrough3(@points[2..5, 0..1])); # Circle has same centre regardless
59 ok &near2(circleThrough3(@points), circleThrough3(@points[4..5, 0..3])); # of the pairing of the points
60 }
61  
62 =cut
63  
64 package Math::Intersection::Circle::Line;
65  
66 #-------------------------------------------------------------------------------
67 # Locate the points at which lines and circles cross in two dimensions
68 # Philip R Brenan at gmail dot com, Appa Apps Ltd, 2015
69 #-------------------------------------------------------------------------------
70  
71 1 ▶use v5.18;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 #-------------------------------------------------------------------------------
78 # Our definition of nearness
79 #-------------------------------------------------------------------------------
80  
81 1 our $near = 1e-6; # Define nearness
82  
83 548 ▶sub near($;$) {return abs(($_[1]//0) - $_[0]) < $near} # Values this close are considered identical
Subroutine near: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[1] // 0
l!l
51731
MC/DC100%1/1
$_[1] // 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
84  
85 sub near2($$;$$) # Check that we are near enough
86 28 ▶ {my ($a, $b, $A, $B) = @_;
Subroutine near2: called CC=4 SCAR=15.8
Pod documented
87 28 ▶ near($A//0, $a) &&
Branch: 1 branch
BranchTrueFalseTotal
&near(scalar($A // 0), $a) and &near(scalar($B // 0), $b) 28 0 28
Condition50%2/4
$A // 0
l!l
280
$B // 0
l!l
280
MC/DC0%0/1
$A // 0
$A
MC/DC0%0/1
$B // 0
$B
Truth table50%1/2
A
A= $A
Aresult
11
00
Truth table50%1/2
A
A= $B
Aresult
11
00
88 near($B//0, $b)
89 }
90  
91 sub near4($$$$;$$$$) # Check that we are near enough
92 18 ▶ {my ($a, $b, $c, $d, $A, $B, $C, $D) = @_;
Subroutine near4: called CC=8 SCAR=27.7
Pod documented
93 18 ▶ near($A//0, $a) &&
Branch: 1 branch
BranchTrueFalseTotal
&near(scalar($A // 0), $a) and &near(scalar($B // 0), $b) and &near(scalar($C // 0), $c) and &near(scalar($D // 0), $d) 18 0 18
Condition42%6/14
&near(scalar($A // 0), $a) and &near(scalar($B // 0), $b) and &near(scalar($C // 0), $c)
!ll&&!rl&&r
0018
&near(scalar($A // 0), $a) and &near(scalar($B // 0), $b)
!ll&&!rl&&r
0018
$A // 0
l!l
180
$B // 0
l!l
180
$C // 0
l!l
180
$D // 0
l!l
180
MC/DC0%0/3
&near(scalar($A // 0), $a) and &near(scalar($B // 0), $b) and &near(scalar($C // 0), $c)
&near(scalar($A // 0), $a) &near(scalar($B // 0), $b) &near(scalar($C // 0), $c)
MC/DC0%0/1
$A // 0
$A
MC/DC0%0/1
$B // 0
$B
MC/DC0%0/1
$C // 0
$C
MC/DC0%0/1
$D // 0
$D
Truth table25%1/4
A and B and C
A= &near(scalar($A // 0), $a)B= &near(scalar($B // 0), $b)C= &near(scalar($C // 0), $c)
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $A
Aresult
11
00
Truth table50%1/2
A
A= $B
Aresult
11
00
Truth table50%1/2
A
A= $C
Aresult
11
00
Truth table50%1/2
A
A= $D
Aresult
11
00
94 near($B//0, $b) &&
95 near($C//0, $c) &&
96 near($D//0, $d)
97 }
98  
99 #-------------------------------------------------------------------------------
100 # Length of a vector
101 #-------------------------------------------------------------------------------
102  
103 sub vectorLength($$;$$) # Length of a vector
104 239 ▶ {my ($x, $y, $𝘅, $𝘆) = @_;
Subroutine vectorLength: called CC=3 SCAR=11.0
Pod documented
105 239 ▶ sqrt(($x-($𝘅//0))**2+($y-($𝘆//0))**2)
Condition100%4/4
$𝘅 // 0
l!l
13226
$𝘆 // 0
l!l
13226
MC/DC100%1/1
$𝘅 // 0
$𝘅
MC/DC100%1/1
$𝘆 // 0
$𝘆
Truth table100%2/2
A
A= $𝘅
Aresult
11
00
Truth table100%2/2
A
A= $𝘆
Aresult
11
00
106 }
107  
108 #-------------------------------------------------------------------------------
109 # Average of two vectors
110 #-------------------------------------------------------------------------------
111  
112 sub midPoint($$$$)
113 10 ▶ {my ($x, $y, $𝘅, $𝘆) = @_;
Subroutine midPoint: called CC=1 SCAR=0.0
Pod documented
114 10 (($x+$𝘅) / 2, ($y+$𝘆) / 2)
115 }
116  
117 #-------------------------------------------------------------------------------
118 # Rotations
119 #-------------------------------------------------------------------------------
120  
121 11 ▶sub rotate90CW ($$) {my ($x, $y) = @_; (+$y, -$x)} # Clockwise
Subroutine rotate90CW: called CC=1 SCAR=0.0
Pod documented
122 2 ▶sub rotate90CCW($$) {my ($x, $y) = @_; (-$y, +$x)} # Counter clockwise
Subroutine rotate90CCW: called CC=1 SCAR=0.0
Pod documented
123  
124 sub rotate90AroundMidPoint($$$$)
125 9 ▶ {my ($x, $y, $𝘅, $𝘆) = @_;
Subroutine rotate90AroundMidPoint: called CC=1 SCAR=0.0
Pod documented
126 9 my ($𝕩, $𝕪) = map {$_/2} rotate90CW($𝘅 - $x, $𝘆 - $y);
127 9 my ($X, $Y) = &midPoint(@_);
128 9 ($X - $𝕩, $Y - $𝕪, $X + $𝕩, $Y + $𝕪)
129 }
130  
131 #-------------------------------------------------------------------------------
132 # Intersection of a circle A, with a circle B.
133 #
134 # Known: coordinates of the centre and radius of each circle x, y, r, 𝘅, 𝘆, 𝗿
135 #
136 # Two different circles either do not intersect, or if they do, they intersect
137 # at one or two points. If they intersect at two points, the intersections are
138 # mirror images of each othe in the line that connects the centres of the two
139 # circles.
140 #
141 # Let 𝗟 be the line joining the two centres with length 𝗹 = a + 𝗮 where a is the
142 # distance from (x, y) along 𝗟 to the point closest to the intersections. Then:
143 #
144 # r*r-a*a == 𝗿*𝗿-𝗮*𝗮
145 # r*r-𝗿*𝗿 == a*a-𝗮*𝗮
146 # == a*a-𝗮*𝗮 = (a+𝗮)(a-𝗮) == 𝗹*(a-𝗮) == 𝗹*(a - (𝗹 - a)) = 2*a*𝗹 - 𝗹*𝗹
147 #
148 # a == (r*r-𝗿*𝗿 + 𝗹*𝗹)/ (2*𝗹)
149 #
150 # The distance 𝗮 at right angles to 𝗟 to an intersection is sqrt(r*r-a*a)
151 #
152 # The unit vector 𝕕 == (𝕩, 𝕪) along line 𝗟 from (x,y) to (𝘅, 𝘆) is the unit in
153 # direction: (𝘅-x, 𝘆-y)
154 #
155 # The unit vectors d, 𝗱 at right angles to 𝗟 are (-𝕪, 𝕩) and (𝕪, -𝕩)
156 #-------------------------------------------------------------------------------
157  
158 sub intersectionCircles(&$$$$$$)
159 76 ▶ {my ($sub, # Sub routine to process intersection
Subroutine intersectionCircles: called CC=8 SCAR=20.8
Pod documented
160 $x, $y, $r, # First circle centre, radius
161 $𝘅, $𝘆, $𝗿) = @_; # Second circle centre, radius
162  
163 76 ▶ near($x, $𝘅) and near($y, $𝘆) and near($r, $𝗿) and # Complain if the two circles are in fact the same circle within the definition of nearness
Branch: 1 branch
BranchTrueFalseTotal
&near($x, $𝘅) and &near($y, $𝘆) and &near($r, $𝗿) and confess("Duplicate circles! Please provide two distinct circles.") 1 75 76
Condition100%6/6
&near($x, $𝘅) and &near($y, $𝘆) and &near($r, $𝗿)
!ll&&!rl&&r
7321
&near($x, $𝘅) and &near($y, $𝘆)
!ll&&!rl&&r
6673
MC/DC100%3/3
&near($x, $𝘅) and &near($y, $𝘆) and &near($r, $𝗿)
&near($x, $𝘅) &near($y, $𝘆) &near($r, $𝗿)
Truth table100%4/4
A and B and C
A= &near($x, $𝘅)B= &near($y, $𝘆)C= &near($r, $𝗿)
ABCresult
0XX0
10X0
1100
1111
164 confess "Duplicate circles! Please provide two distinct circles.";
165  
166 75 my ($𝕏, $𝕐) = ($𝘅 - $x, $𝘆 - $y); # Vector between centres
167 75 my $𝗹 = vectorLength($𝕏, $𝕐); # Distance between centres
168 75 ▶ return &$sub() if $𝗹 > $r + $𝗿 or $𝗹 < abs($r - $𝗿); # The circles are too far apart or too close to intersect
Branch: 1 branch
BranchTrueFalseTotal
if $𝗹 > $r + $𝗿 or $𝗹 < abs $r - $𝗿 3 72 75
Condition100%3/3
$𝗹 > $r + $𝗿 or $𝗹 < abs $r - $𝗿
l!l&&r!l&&!r
1272
MC/DC100%2/2
$𝗹 > $r + $𝗿 or $𝗹 < abs $r - $𝗿
$𝗹 > $r + $𝗿 $𝗹 < abs $r - $𝗿
Truth table100%3/3
A or B
A= $𝗹 > $r + $𝗿B= $𝗹 < abs $r - $𝗿
ABresult
1X1
011
000
169  
170 72 my ($𝕩, $𝕪) = ($𝕏 / $𝗹, $𝕐 / $𝗹); # Unit vector between centres
171 72 my $a = ($r*$r - $𝗿*$𝗿 + $𝗹*$𝗹)/ (2*$𝗹); # Length of the common side
172  
173 72 ▶ return &$sub($x+$𝕩*$a, $y+$𝕪*$a) if near($𝗹, $r + $𝗿) or # The circles touch at one point if within the definition of nearness
Branch: 1 branch
BranchTrueFalseTotal
if &near($𝗹, $r + $𝗿) or &near($𝗹, abs $r - $𝗿) 22 50 72
Condition100%3/3
&near($𝗹, $r + $𝗿) or &near($𝗹, abs $r - $𝗿)
l!l&&r!l&&!r
13950
MC/DC100%2/2
&near($𝗹, $r + $𝗿) or &near($𝗹, abs $r - $𝗿)
&near($𝗹, $r + $𝗿) &near($𝗹, abs $r - $𝗿)
Truth table100%3/3
A or B
A= &near($𝗹, $r + $𝗿)B= &near($𝗹, abs $r - $𝗿)
ABresult
1X1
011
000
174 near($𝗹, abs($r - $𝗿));
175  
176 50 my $𝗮 = sqrt($r*$r-$a*$a);
177 50 &$sub($x+$𝕩*$a-$𝕪*$𝗮, $y+$𝕪*$a+$𝕩*$𝗮, # The circles touch at two points
178 $x+$𝕩*$a+$𝕪*$𝗮, $y+$𝕪*$a-$𝕩*$𝗮);
179 }
180  
181 #-------------------------------------------------------------------------------
182 # Position on a line closest to a specified point
183 #
184 # Known: two points on the line 𝗟 such that: 𝗹 = (𝘅, 𝘆), 𝕝 = (𝕩, 𝕪) and the
185 # specified point 𝗽 = (x, y). We want to find 𝗰 the point on 𝗟 closest to 𝗽.
186 #
187 # A circle with centre 𝗹 through 𝗽 will intersect a circle with centre 𝕝 through
188 # 𝗽 at 𝗾. 𝗰 is then the average of 𝗽 and 𝗾.
189 #-------------------------------------------------------------------------------
190  
191 sub intersectionLinePoint(&$$$$$$)
192 54 ▶ {my ($sub, # Sub routine to process intersection
Subroutine intersectionLinePoint: called CC=3 SCAR=11.1
Pod documented
193 $𝘅, $𝘆, $𝕩, $𝕪, # Two points on line 𝗹
194 $x, $y) = @_; # The point 𝗽
195  
196 54 ▶ near($𝘅, $𝕩) and near($𝘆, $𝕪) and confess "Points on line are too close!";
Branch: 1 branch
BranchTrueFalseTotal
&near($𝘅, $𝕩) and &near($𝘆, $𝕪) and confess("Points on line are too close!") 0 54 54
Condition66%2/3
&near($𝘅, $𝕩) and &near($𝘆, $𝕪)
!ll&&!rl&&r
5040
MC/DC0%0/2
&near($𝘅, $𝕩) and &near($𝘆, $𝕪)
&near($𝘅, $𝕩) &near($𝘆, $𝕪)
Truth table66%2/3
A and B
A= &near($𝘅, $𝕩)B= &near($𝘆, $𝕪)
ABresult
0X0
100
111
197  
198 54 my $𝗿 = vectorLength($𝘅 - $x, $𝘆 - $y); # Radius of first circle
199 54 my $𝕣 = vectorLength($𝕩 - $x, $𝕪 - $y); # Radius of second circle
200 intersectionCircles
201 54 ▶ {return &$sub(@_) if @_ == 2; # Point is on line
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 16 38 54
202 38 my ($x, $y, $𝘅, $𝘆) = @_;
203 38 &$sub(($x+$𝘅) / 2, ($y+$𝘆) / 2)
204 54 } $𝘅, $𝘆, $𝗿, $𝕩, $𝕪, $𝕣;
205 }
206  
207 #-------------------------------------------------------------------------------
208 # Intersection of two lines
209 #
210 # Given two lines l specified by two points 𝗹 = (𝘅, 𝘆), 𝕝 = (𝕩, 𝕪) and
211 # L specified by two points 𝗟 = (𝗫, 𝗬), 𝕃 = (𝕏, 𝕐)
212 # We want to find 𝗰 the point where the two lines intersect (if they do)
213 #
214 # Let the closest point to 𝗟 on l be 𝗮 and the closest point to 𝗮 on 𝗟 by 𝗯.
215 # L𝗮𝗯 is similar to L𝗮𝗰.
216 #-------------------------------------------------------------------------------
217  
218 sub intersectionLines(&$$$$$$$$)
219 17 ▶ {my ($sub, # Sub routine to process intersection
Subroutine intersectionLines: called CC=5 SCAR=16.7
Pod documented
220 $𝘅, $𝘆, $𝕩, $𝕪, # Two points on line l
221 $𝗫, $𝗬, $𝕏, $𝕐) = @_; # Two points on line L
222  
223 17 ▶ near($𝘅, $𝕩) and near($𝘆, $𝕪) and confess "Points on first line are too close!";
Branch: 1 branch
BranchTrueFalseTotal
&near($𝘅, $𝕩) and &near($𝘆, $𝕪) and confess("Points on first line are too close!") 0 17 17
Condition33%1/3
&near($𝘅, $𝕩) and &near($𝘆, $𝕪)
!ll&&!rl&&r
1700
MC/DC0%0/2
&near($𝘅, $𝕩) and &near($𝘆, $𝕪)
&near($𝘅, $𝕩) &near($𝘆, $𝕪)
Truth table33%1/3
A and B
A= &near($𝘅, $𝕩)B= &near($𝘆, $𝕪)
ABresult
0X0
100
111
224 17 ▶ near($𝗫, $𝕏) and near($𝘆, $𝕪) and confess "Points on second line are too close!";
Branch: 1 branch
BranchTrueFalseTotal
&near($𝗫, $𝕏) and &near($𝘆, $𝕪) and confess("Points on second line are too close!") 0 17 17
Condition33%1/3
&near($𝗫, $𝕏) and &near($𝘆, $𝕪)
!ll&&!rl&&r
1700
MC/DC0%0/2
&near($𝗫, $𝕏) and &near($𝘆, $𝕪)
&near($𝗫, $𝕏) &near($𝘆, $𝕪)
Truth table33%1/3
A and B
A= &near($𝗫, $𝕏)B= &near($𝘆, $𝕪)
ABresult
0X0
100
111
225  
226 intersectionLinePoint # Find 𝗮
227 17 ▶ {my ($𝗮x, $𝗮y) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
228  
229 17 ▶ if (near($𝗮x, $𝗫) and near($𝗮y, $𝗬)) # The two lines might be collinear
Branch: 1 branch
BranchTrueFalseTotal
if (&near($𝗮x, $𝗫) and &near($𝗮y, $𝗬)) 1 16 17
Condition100%3/3
&near($𝗮x, $𝗫) and &near($𝗮y, $𝗬)
!ll&&!rl&&r
1511
MC/DC100%2/2
&near($𝗮x, $𝗫) and &near($𝗮y, $𝗬)
&near($𝗮x, $𝗫) &near($𝗮y, $𝗬)
Truth table100%3/3
A and B
A= &near($𝗮x, $𝗫)B= &near($𝗮y, $𝗬)
ABresult
0X0
100
111
230 {intersectionLinePoint
231 1 ▶ {my ($𝕒x, $𝕒y) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.1
232 1 ▶ near($𝕒x, $𝕏) and near($𝕒y, $𝕐) and
Branch: 1 branch
BranchTrueFalseTotal
&near($𝕒x, $𝕏) and &near($𝕒y, $𝕐) and confess("Collinear lines! Please provide two distinct lines.") 1 0 1
Condition33%1/3
&near($𝕒x, $𝕏) and &near($𝕒y, $𝕐)
!ll&&!rl&&r
001
MC/DC0%0/2
&near($𝕒x, $𝕏) and &near($𝕒y, $𝕐)
&near($𝕒x, $𝕏) &near($𝕒y, $𝕐)
Truth table33%1/3
A and B
A= &near($𝕒x, $𝕏)B= &near($𝕒y, $𝕐)
ABresult
0X0
100
111
233 confess "Collinear lines! Please provide two distinct lines.";
234 1 } $𝘅, $𝘆, $𝕩, $𝕪, $𝕏, $𝕐;
235 }
236  
237 intersectionLinePoint # Find 𝗯
238 16 ▶ {my ($𝗯x, $𝗯y) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
239 16 my $Lb = vectorLength($𝗫 - $𝗯x, $𝗬 - $𝗯y); # Distance from 𝗟 to 𝗯
240 16 ▶ return &$sub() if near($Lb); # Parallel lines
Branch: 1 branch
BranchTrueFalseTotal
if &near($Lb) 2 14 16
241 14 my $La = vectorLength($𝗫 - $𝗮x, $𝗬 - $𝗮y); # Distance from 𝗟 to 𝗮
242 14 my $s = ($La / $Lb) ** 2; # Scale factor for 𝗟𝗯
243 14 &$sub($𝗫 + $s * ($𝗯x - $𝗫), $𝗬 + $s * ($𝗯y - $𝗬)) # Point of intersection
244 16 } $𝗫, $𝗬, $𝕏, $𝕐, $𝗮x, $𝗮y; # Find 𝗯 on second line
245 17 } $𝘅, $𝘆, $𝕩, $𝕪, $𝗫, $𝗬; # Find 𝗮 on first line
246 }
247 #-------------------------------------------------------------------------------
248 # Intersection of a circle with a line
249 #
250 # Given a circle specified by its centre: (x, y), and radius: r
251 # and a line that passes through points: ($𝘅, $𝘆) and ($𝕩, $𝕪)
252 # find the two points at which the line crosses the circle or the single point
253 # at which the line touches the circle or report that there are no points in
254 # common.
255 #
256 # If the line crosses the circle we can draw an isoceles triangle from the
257 # centre of the circle to the points of intersection, with the line forming the
258 # base of said triangle. The centre of the base is the closest point on the
259 # line to the centre of the circle. The line is at right angles to the line
260 # from the centre of the circle to the centre of the base.
261 #-------------------------------------------------------------------------------
262  
263 sub intersectionCircleLine(&$$$$$$$)
264 14 ▶ {my ($sub, # Sub routine to process intersection
Subroutine intersectionCircleLine: called CC=4 SCAR=13.9
Pod documented
265 $x, $y, $r, # Circle centre, radius
266 $𝘅, $𝘆, $𝕩, $𝕪) = @_; # Line goes through these two points
267  
268 14 ▶ near($𝘅, $𝕩) and near($𝘆, $𝕪) and confess "Points on line are too close!";
Branch: 1 branch
BranchTrueFalseTotal
&near($𝘅, $𝕩) and &near($𝘆, $𝕪) and confess("Points on line are too close!") 1 13 14
Condition100%3/3
&near($𝘅, $𝕩) and &near($𝘆, $𝕪)
!ll&&!rl&&r
851
MC/DC100%2/2
&near($𝘅, $𝕩) and &near($𝘆, $𝕪)
&near($𝘅, $𝕩) &near($𝘆, $𝕪)
Truth table100%3/3
A and B
A= &near($𝘅, $𝕩)B= &near($𝘆, $𝕪)
ABresult
0X0
100
111
269 13 ▶ near($r) and confess "Radius is too small!";
Branch: 1 branch
BranchTrueFalseTotal
&near($r) and confess("Radius is too small!") 1 12 13
270  
271 intersectionLinePoint
272 12 ▶ {my ($X, $Y) = @_; # Midpoint on line
Subroutine __ANON__: called CC=5 SCAR=16.1
273 12 ▶ if (near($x, $X) and near($y, $Y)) # Line passes through centre of circle
Branch: 1 branch
BranchTrueFalseTotal
if (&near($x, $X) and &near($y, $Y)) 7 5 12
Condition100%3/3
&near($x, $X) and &near($y, $Y)
!ll&&!rl&&r
237
MC/DC100%2/2
&near($x, $X) and &near($y, $Y)
&near($x, $X) &near($y, $Y)
Truth table100%3/3
A and B
A= &near($x, $X)B= &near($y, $Y)
ABresult
0X0
100
111
274 7 {my ($𝗫, $𝗬) = ($𝕩 - $𝘅, $𝕪 - $𝘆); # Vector along line
275 7 my $D = vectorLength($𝗫, $𝗬); # Length of vector along line
276 7 my $s = $r/$D; # Length from midpoint along line to circumference relative to length from centre to midpoint
277 7 return &$sub($x + $s * $𝗫, $y + $s * $𝗬, $x - $s * $𝗫, $y - $s * $𝗬); # Intersection points
278 }
279 5 my ($𝗫, $𝗬) = ($X - $x, $Y - $y); # Centre to midpoint
280 5 my $𝗗 = vectorLength($𝗫, $𝗬); # Distance to midpoint
281 5 ▶ return &$sub() if $𝗗 > $r; # Midpoint outside circle
Branch: 1 branch
BranchTrueFalseTotal
if $𝗗 > $r 1 4 5
282 4 ▶ return &$sub($X, $Y) if near($𝗗, $r); # Tangent
Branch: 1 branch
BranchTrueFalseTotal
if &near($𝗗, $r) 1 3 4
283 3 my $𝔻 = sqrt($r*$r - $𝗗*$𝗗); # Length from midpoint along line to circumference
284 3 my $s = $𝔻/$𝗗; # Length from midpoint along line to circumference relative to length from centre to midpoint
285 3 &$sub($X - $s * $𝗬, $Y + $s * $𝗫, $X + $s * $𝗬, $Y - $s * $𝗫) # Intersection points
286 12 } $𝘅, $𝘆, $𝕩, $𝕪, $x, $y; # Find point on line closest to centre of circle
287 }
288  
289 #-------------------------------------------------------------------------------
290 # Intersection of a circle with a line - alternative method to confirm tests
291 #
292 # Known 𝘅, 𝘆, r, X, x, Y, y find l,t
293 #
294 # Circle(t) Line(l) from (X,Y) to (x,y)
295 # 𝘅+𝗿*cos(t) = X+l*(X-x)
296 # 𝘆+𝗿*sin(t) = Y+l*(Y-y)
297 #
298 # 𝗿*cos(t) = X+l*(X-x)-𝘅 let 𝗫 = X - x, 𝕏 = X - 𝘅 => 𝗿*cos(t) == 𝕏 + l𝗫
299 # 𝗿*sin(t) = Y+l*(Y-y)-𝘆 let 𝗬 = Y - y, 𝕐 = Y - 𝘆 => 𝗿*sin(t) == 𝕐 + l𝗬
300 #
301 # rr = 𝕏𝕏 + ll𝗫𝗫 + l2𝕏𝗫 + 𝕐𝕐 + ll𝗬𝗬 + l2𝕐𝗬
302 # 0 = ll(a = 𝗫𝗫 + 𝗬𝗬) + l(b = 2(𝕏𝗫 + 𝕐𝗬)) + (c = 𝕏𝕏 + 𝕐𝕐 - rr)
303 #
304 # Quadratic equation axx+bx+c has roots:
305 #
306 # x1, x2 = -b/2a +- sqrt(bb-4ac)/2a
307 #-------------------------------------------------------------------------------
308  
309 # sub intersectionCircleLine2(&$$$$$$$)
310 # {my ($sub, # Sub routine to process intersection
311 # $𝘅, $𝘆, $𝗿, # Circle centre, radius
312 # $X, $Y, $x, $y) = @_; # Line goes through these two points
313 # near($𝘅, $X) and near($𝘆, $Y) and confess "Points on line are too close!";
314 # near($𝗿) and confess "Radius is too small!";
315 #
316 # my $𝗫 = $X - $x; my $𝕏 = $X - $𝘅;
317 # my $𝗬 = $Y - $y; my $𝕐 = $Y - $𝘆;
318 #
319 # my $a = $𝗫*$𝗫 + $𝗬*$𝗬; # Quadratic coefficients
320 # my $b = 2 * ($𝕏*$𝗫 + $𝕐*$𝗬);
321 # my $c = ($𝕏*$𝕏 + $𝕐*$𝕐 - $𝗿*$𝗿);
322 #
323 # my $𝗱 = $b * $b - 4 * $a * $c; # Determinant
324 #
325 # return &$sub() if $𝗱 < 0; # No point of contact
326 #
327 # my $𝗰 = -$b / (2 * $a); # Centre of solution
328 #
329 # if (near($𝗱)) # Tangent within definition of nearness
330 # {my $l = $𝗰;
331 # return &$sub($X + $l * $𝗫, $Y + $l * $𝗬);
332 # }
333 # # Two crossing points
334 # my $d = sqrt($𝗱) / (2 * $a);
335 # my ($l, $𝗹) = ($𝗰 + $d, $𝗰 - $d);
336 # &$sub($X + $l * $𝗫, $Y + $l * $𝗬, $X + $𝗹 * $𝗫, $Y + $𝗹 * $𝗬)
337 # }
338 #
339  
340 # Export details
341  
342 1 require 5;
343 1 require Exporter;
344  
345 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
346  
347 1 @ISA = qw(Exporter);
348 1 @EXPORT = qw(intersectionCircles intersectionCircleLine intersectionLines intersectionLinePoint);
349 1 @EXPORT_OK = qw(midPoint near near2 near4 rotate90CW rotate90CCW rotate90AroundMidPoint vectorLength);
350 1 $EXPORT_TAGS{all} = [@EXPORT, @EXPORT_OK];
351 1 $VERSION = '1.002'; # Thursday 20 Aug 2015
352  
353 =head1 Description
354  
355 Find the points at which circles and lines intersect.
356  
357 Fast and easy to use these functions are written in 100% Pure Perl.
358  
359 =head2 intersectionCircles 𝘀𝘂𝗯 circle1, circle2
360  
361 Find the points at which two circles intersect. Complains if the two circles
362 are identical.
363  
364 𝘀𝘂𝗯 specifies a subroutine to be called with the coordinates of the
365 intersection points if there are any or an empty parameter list if there are
366 no points of intersection.
367  
368 A circle is specified by supplying a list of three numbers:
369  
370 (𝘅, 𝘆, 𝗿)
371  
372 where (𝘅, 𝘆) are the coordinates of the centre of the circle and (𝗿) is its
373 radius.
374  
375 Returns what ever is returned by 𝘀𝘂𝗯.
376  
377 =head2 intersectionCircleLine 𝘀𝘂𝗯 circle, line
378  
379 Find the points at which a circle and a line intersect.
380  
381 𝘀𝘂𝗯 specifies a subroutine to be called with the coordinates of the
382 intersection points if there are any or an empty parameter list if there are
383 no points of intersection.
384  
385 A circle is specified by supplying a list of three numbers:
386  
387 (𝘅, 𝘆, 𝗿)
388  
389 where (𝘅, 𝘆) are the coordinates of the centre of the circle and (𝗿) is its
390 radius.
391  
392 A line is specified by supplying a list of four numbers:
393  
394 (x, y, 𝘅, 𝘆)
395  
396 where (x, y) and (𝘅, 𝘆) are the coordinates of two points on the line.
397  
398 Returns what ever is returned by 𝘀𝘂𝗯.
399  
400 =head2 intersectionLines 𝘀𝘂𝗯 line1, line2
401  
402 Finds the point at which two lines intersect.
403  
404 𝘀𝘂𝗯 specifies a subroutine to be called with the coordinates of the
405 intersection point or an empty parameter list if the two lines do not
406 intersect.
407  
408 Complains if the two lines are collinear.
409  
410 A line is specified by supplying a list of four numbers:
411  
412 (x, y, 𝘅, 𝘆)
413  
414 where (x, y) and (𝘅, 𝘆) are the coordinates of two points on the line.
415  
416 Returns what ever is returned by 𝘀𝘂𝗯.
417  
418 =head2 intersectionLinePoint 𝘀𝘂𝗯 line, point
419  
420 Find the point on a line closest to a specified point.
421  
422 𝘀𝘂𝗯 specifies a subroutine to be called with the coordinates of the
423 intersection points if there are any.
424  
425 A line is specified by supplying a list of four numbers:
426  
427 (x, y, 𝘅, 𝘆)
428  
429 where (x, y) and (𝘅, 𝘆) are the coordinates of two points on the line.
430  
431 A point is specified by supplying a list of two numbers:
432  
433 (𝘅, 𝘆)
434  
435 where (𝘅, 𝘆) are the coordinates of the point.
436  
437 Returns what ever is returned by 𝘀𝘂𝗯.
438  
439 =head2 $Math::Intersection::Circle::Line::near
440  
441 As a finite computer cannot represent an infinite plane of points it is
442 necessary to make the plane discrete by merging points closer than the
443 distance contained in this variable, which is set by default to 1e-6.
444  
445 =head1 Export
446  
447 The following functions are exported by default:
448  
449 =over
450  
451 =item C<intersectionCircles()>
452  
453 =item C<intersectionCircleLine()>
454  
455 =item C<intersectionLines()>
456  
457 =item C<intersectionLinePoint()>
458  
459 =back
460  
461 Optionally some useful helper functions can also be exported either by
462 specifying the tag :all or by naming the required functions individually:
463  
464 =over
465  
466 =item C<midPoint()>
467  
468 =item C<near()>
469  
470 =item C<near2()>
471  
472 =item C<near4()>
473  
474 =item C<rotate90CW()>
475  
476 =item C<rotate90CCW ()>
477  
478 =item C<rotate90AroundMidPoint()>
479  
480 =item C<vectorLength()>
481  
482 =back
483  
484 =head1 Installation
485  
486 Standard Module::Build process for building and installing modules:
487  
488 perl Build.PL
489 ./Build
490 ./Build test
491 ./Build install
492  
493 Or, if you're on a platform (like DOS or Windows) that doesn't require
494 the "./" notation, you can do this:
495  
496 perl Build.PL
497 Build
498 Build test
499 Build install
500  
501 =head1 Author
502  
503 Philip R Brenan at gmail dot com
504  
505 http://www.appaapps.com
506  
507 =head1 Copyright
508  
509 Copyright (c) 2015 Philip R Brenan.
510  
511 This module is free software. It may be used, redistributed and/or
512 modified under the same terms as Perl itself.
513  
514 =cut
↑ Summary Build »