Skip to content

Instantly share code, notes, and snippets.

@timo

timo/foo.p6 Secret

Forked from AlexDaniel/foo.p6
Last active September 28, 2019 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/079fc2fbf40272ed0bc1687b29d2d45a to your computer and use it in GitHub Desktop.
Save timo/079fc2fbf40272ed0bc1687b29d2d45a to your computer and use it in GitHub Desktop.
@*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