Find the intersection of two lines: Difference between revisions
Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Add Factor) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 153:
5.000 5.000
</pre>
=={{header|Ada}}==
Line 243 ⟶ 242:
5.0000 5.0000
</pre>
=={{header|AWK}}==
<lang AWK>
Line 720:
<pre> 5 5
-1.#IND -1.#IND</pre>
=={{header|Go}}==
<lang go>
Line 1,249 ⟶ 1,250:
print "$ix $iy\n";
</lang>
=={{header|Perl 6}}==▼
{{works with|Rakudo|2016.11}}▼
{{trans|zkl}}▼
<lang perl6>sub intersection (Real $ax, Real $ay, Real $bx, Real $by,▼
Real $cx, Real $cy, Real $dx, Real $dy ) {▼
sub term:<|AB|> { determinate($ax, $ay, $bx, $by) }▼
sub term:<|CD|> { determinate($cx, $cy, $dx, $dy) }▼
my $ΔxAB = $ax - $bx;▼
my $ΔyAB = $ay - $by;▼
my $ΔxCD = $cx - $dx;▼
my $ΔyCD = $cy - $dy;▼
my $x-numerator = determinate( |AB|, $ΔxAB, |CD|, $ΔxCD );▼
my $y-numerator = determinate( |AB|, $ΔyAB, |CD|, $ΔyCD );▼
my $denominator = determinate( $ΔxAB, $ΔyAB, $ΔxCD, $ΔyCD );▼
return 'Lines are parallel' if $denominator == 0;▼
($x-numerator/$denominator, $y-numerator/$denominator);▼
}▼
sub determinate ( Real $a, Real $b, Real $c, Real $d ) { $a * $d - $b * $c }▼
# TESTING▼
say 'Intersection point: ', intersection( 4,0, 6,10, 0,3, 10,7 );▼
say 'Intersection point: ', intersection( 4,0, 6,10, 0,3, 10,7.1 );▼
say 'Intersection point: ', intersection( 0,0, 1,1, 1,2, 4,5 );</lang>▼
{{out}}▼
<pre>Intersection point: (5 5)▼
Intersection point: (5.010893 5.054466)▼
Intersection point: Lines are parallel▼
</pre>▼
=={{header|Phix}}==
Line 1,466 ⟶ 1,431:
<pre>5
5</pre>
(formerly Perl 6)
▲{{works with|Rakudo|2016.11}}
▲{{trans|zkl}}
▲<lang perl6>sub intersection (Real $ax, Real $ay, Real $bx, Real $by,
▲ Real $cx, Real $cy, Real $dx, Real $dy ) {
▲ sub term:<|AB|> { determinate($ax, $ay, $bx, $by) }
▲ sub term:<|CD|> { determinate($cx, $cy, $dx, $dy) }
▲ my $ΔxAB = $ax - $bx;
▲ my $ΔyAB = $ay - $by;
▲ my $ΔxCD = $cx - $dx;
▲ my $ΔyCD = $cy - $dy;
▲ my $x-numerator = determinate( |AB|, $ΔxAB, |CD|, $ΔxCD );
▲ my $y-numerator = determinate( |AB|, $ΔyAB, |CD|, $ΔyCD );
▲ my $denominator = determinate( $ΔxAB, $ΔyAB, $ΔxCD, $ΔyCD );
▲ return 'Lines are parallel' if $denominator == 0;
▲ ($x-numerator/$denominator, $y-numerator/$denominator);
▲}
▲sub determinate ( Real $a, Real $b, Real $c, Real $d ) { $a * $d - $b * $c }
▲# TESTING
▲say 'Intersection point: ', intersection( 4,0, 6,10, 0,3, 10,7 );
▲say 'Intersection point: ', intersection( 4,0, 6,10, 0,3, 10,7.1 );
▲say 'Intersection point: ', intersection( 0,0, 1,1, 1,2, 4,5 );</lang>
▲{{out}}
▲<pre>Intersection point: (5 5)
▲Intersection point: (5.010893 5.054466)
▲Intersection point: Lines are parallel
▲</pre>
=={{header|REXX}}==
Line 1,741 ⟶ 1,743:
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/DAqMtEx/0 (JavaScript)]
or by [https://scastie.scala-lang.org/WQOqakOlQnaBRFBa1PuRYw Scastie (JVM)].
=={{header|Sidef}}==
{{trans|Perl 6}}
|