[1]


Task

Find the point of intersection of two lines in 2D.



The 1st line passes though    (4,0)    and    (6,10) .

The 2nd line passes though    (0,3)    and    (10,7) .




with javascript_semantics
enum X, Y
 
function abc(sequence s,e)
-- yeilds {a,b,c}, corresponding to ax+by=c
    atom a = e[Y]-s[Y], b = s[X]-e[X], c = a*s[X]+b*s[Y]
    return {a,b,c}
end function
 
procedure intersect(sequence s1, e1, s2, e2)
    atom {a1,b1,c1} = abc(s1,e1),
         {a2,b2,c2} = abc(s2,e2),
         delta = a1*b2 - a2*b1,
         x = b2*c1 - b1*c2,
         y = a1*c2 - a2*c1
    ?iff(delta=0?"parallel lines/do not intersect"
                :{x/delta, y/delta})
end procedure
 
intersect({4,0},{6,10},{0,3},{10,7})        -- {5,5}
intersect({4,0},{6,10},{0,3},{10,7.1})      -- {5.010893246,5.054466231}
intersect({0,0},{0,0},{0,3},{10,7})         -- "parallel lines/do not intersect"
intersect({0,0},{1,1},{1,2},{4,5})          -- "parallel lines/do not intersect"
intersect({1,-1},{4,4},{2,5},{3,-2})        -- {2.5,1.5}
