-
-
Save timo/079fc2fbf40272ed0bc1687b29d2d45a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@*ARGS = <5 4 3 2 1 0 -1 -2>; | |
multi sub MAIN( Int:D $x1, Int:D $y1, Int:D $x2, Int:D $y2, Int:D $x3, Int:D $y3, Int:D $x4, Int:D $y4 ) | |
{ | |
with intersection( $x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4 ) -> ($x, $y) | |
{ | |
say "The intersection is at $x, $y."; | |
} | |
else | |
{ | |
say .exception.message; | |
} | |
} | |
multi sub intersection( Int \x1, Int \y1, Int \x2, Int \y2, Int \x3, Int \y3, Int \x4, Int \y4 ) | |
{ | |
try { | |
return | |
( (x1 * y2 - y1 * x2 ) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3*x4) ) div | |
( (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4) ), | |
( (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 -y2) * (x3*y4 - y3 * x4) ) div | |
( (x1 - x2) * (y3 - y4) - (y1 - y2) * (x2 - x4) ); | |
CATCH { when X::Numeric::DivideByZero { return fail "Lines are parallel or identical" } } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment