1function line_intersect(x1, y1, x2, y2, x3, y3, x4, y4)
2{
3 var ua, ub, denom = (y4 - y3)*(x2 - x1) - (x4 - x3)*(y2 - y1);
4 if (denom == 0) {
5 return null;
6 }
7 ua = ((x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3))/denom;
8 ub = ((x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3))/denom;
9 return {
10 x: x1 + ua * (x2 - x1),
11 y: y1 + ua * (y2 - y1),
12 seg1: ua >= 0 && ua <= 1,
13 seg2: ub >= 0 && ub <= 1
14 };
15}