From 6ed5abfaa1a26717410601a84d89a57d8c17738c Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 23 Sep 2019 17:06:34 +0100 Subject: Intersection of 2 points --- challenge-027/simon-proctor/perl6/ch-1.p6 | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-027/simon-proctor/perl6/ch-1.p6 diff --git a/challenge-027/simon-proctor/perl6/ch-1.p6 b/challenge-027/simon-proctor/perl6/ch-1.p6 new file mode 100644 index 0000000000..6f68aeee29 --- /dev/null +++ b/challenge-027/simon-proctor/perl6/ch-1.p6 @@ -0,0 +1,29 @@ +#!/usr/bin/env perl6 + +use v6; + +#| Takes 8 points for the lines (a,b) -> (c,d) and (p,q) -> (r,s) +#| Outputs the intersection +sub MAIN( Rat() \a, Rat() \b, Rat() \c, Rat() \d, + Rat() \p, Rat() \q, Rat() \r, Rat() \s ) { + + my \a1 = d - b; + my \b1 = a - c; + my \c1 = a1*(a) + b1*(b); + + my \a2 = s - q; + my \b2 = p - r; + my \c2 = a2*(p)+ b2*(q); + + my \determinant = a1*b2 - a2*b1; + + say "Lines ({a},{b}) -> ({c},{d}) and ({p},{q}) -> ({r},{s})"; + + if ( determinant == 0 ) { + say "Lines are parallel. No intersection"; + } else { + my \x = (b2*c1 - b1*c2)/determinant; + my \y = (a1*c2 - a2*c1)/determinant; + say "Intersection at ({x},{y})"; + } +} -- cgit