Skip to content

Instantly share code, notes, and snippets.

@AlexDaniel
Created September 28, 2019 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlexDaniel/19dd0c641116c6cae784b66c16e0475f to your computer and use it in GitHub Desktop.
Save AlexDaniel/19dd0c641116c6cae784b66c16e0475f to your computer and use it in GitHub Desktop.
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