Line circle intersection: Difference between revisions

added Perl 6 programming solution
(→‎{{header|Go}}: Doh, messed up my own examples! Minor output tidying whilst we're here.)
(added Perl 6 programming solution)
Line 185:
a segment starting at (7, 4) and ending at (11, 8) is/are:
[(8, 5)]
</pre>
 
=={{header|Perl 6}}==
Extend solution space to 3D. Reference: this [https://stackoverflow.com/questions/1073336/ SO question and answers]
<lang perl6>#!/usr/bin/env perl6
 
sub LineCircularOBJintersection(@P1, @P2, @Centre, \Radius) {
my @d = @P2 »-« @P1 ; # d
my @f = @P1 »-« @Centre ; # c
 
my \a = [+] @d»²; # d dot d
my \b = 2 * ([+] @f »*« @d); # 2 * f dot d
my \c = ([+] @f»²) - Radius²; # f dot f - r²
my \Δ = b²-(4*a*c); # discriminant
 
if (Δ < 0) {
return [];
} else {
my (\t1,\t2) = (-b - Δ.sqrt)/(2*a), (-b + Δ.sqrt)/(2*a);
if ( t1 ≥ 0 and t1 ≤ 1 ) or ( t2 ≥ 0 and t2 ≤ 1 ) {
return @P1 »+« ( @P2 »-« @P1 ) »*» t1, @P1 »+« ( @P2 »-« @P1 ) »*» t2
} else {
return []
}
}
}
 
my \DATA = [
[ <-10 11 > , < 10 -9 > , < 3 -5 > , 3 ],
[ <-10 11 > , <-11 12> , < 3 -5 > , 3],
[ <3 -2 > , <7 -2> , < 3 -5 > , 3],
[ <3 -2 > , <7 -2> , < 0 0> , 4],
[ <0 -3 > , <0 6> , < 0 0> , 4],
[ <6 3 > , <10 7> , < 4 2> , 5],
[ <7 4 > , <11 18> , < 4 2> , 5],
[ <3.63 2 −2.26 > , <0.77 2 3.46 > , <1 4 0 > , 4]
];
 
for DATA {
my @solution = LineCircularOBJintersection $_[0] , $_[1] , $_[2], $_[3];
say "For data set: ", $_;
say "Solution(s) is/are: ", @solution.Bool ?? @solution !! "None";
}</lang>
{{out}}
<pre>For data set: [(-10 11) (10 -9) (3 -5) 3]
Solution(s) is/are: [(3 -2) (6 -5)]
For data set: [(-10 11) (-11 12) (3 -5) 3]
Solution(s) is/are: None
For data set: [(3 -2) (7 -2) (3 -5) 3]
Solution(s) is/are: [(3 -2) (3 -2)]
For data set: [(3 -2) (7 -2) (0 0) 4]
Solution(s) is/are: [(-3.4641016151377544 -2) (3.4641016151377544 -2)]
For data set: [(0 -3) (0 6) (0 0) 4]
Solution(s) is/are: [(0 -4) (0 4)]
For data set: [(6 3) (10 7) (4 2) 5]
Solution(s) is/are: [(1 -2) (8 5)]
For data set: [(7 4) (11 18) (4 2) 5]
Solution(s) is/are: [(5.030680985703315 -2.892616550038399) (7.459885052032535 5.60959768211387)]
For data set: [(3.63 2 −2.26) (0.77 2 3.46) (1 4 0) 4]
Solution(s) is/are: [(3.62828568570857 2 -2.2565713714171394) (0.7717143142914296 2 3.456571371417141)]
</pre>
 
351

edits