aboutsummaryrefslogtreecommitdiff
path: root/challenge-027/simon-proctor/perl6/ch-1.p6
blob: 6f68aeee29392fe96c47c7c0c1437058a5bcc6e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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})";
    }
}