line intersection code example
Example 1: line line intersection
const intersects = (lineSeg1, lineSeg2) => {
const x1 = lineSeg2.x1;
const y1 = lineSeg2.y1;
const x2 = lineSeg2.x2;
const y2 = lineSeg2.y2;
const x3 = lineSeg1.x1;
const y3 = lineSeg1.y1;
const x4 = lineSeg1.x2;
const y4 = lineSeg1.y2;
const den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (!den) return;
const t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
const u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;
return t >= 0 && t <= 1 && u >= 0 && u <= 1 ? {x: x1 + t * (x2 - x1), y: y1 + t * (y2 - y1) } : false;
}
// Or if you'd rather
const intersects = (lineSeg1, lineSeg2) => {
den = (lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2);
if (!den) return;
t = ((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / den;
u = -((lineSeg2.x1 - lineSeg2.x2) * (lineSeg2.y1 - lineSeg1.y1) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg2.x1 - lineSeg1.x1)) / den;
return t >= 0 && t <= 1 && u >= 0 && u <= 1 ? {x: lineSeg2.x1 + t * (lineSeg2.x2 - lineSeg2.x1), y: lineSeg2.y1 + t * (lineSeg2.y2 - lineSeg2.y1) } : false;
}
// Or if you really want
const intersects = (lineSeg1, lineSeg2) => {
if (!((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) return;
return (((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) >= 0 && (((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) <= 1 && (-((lineSeg2.x1 - lineSeg2.x2) * (lineSeg2.y1 - lineSeg1.y1) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg2.x1 - lineSeg1.x1)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) >= 0 && (-((lineSeg2.x1 - lineSeg2.x2) * (lineSeg2.y1 - lineSeg1.y1) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg2.x1 - lineSeg1.x1)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) <= 1 ? { x: lineSeg2.x1 + (((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) * (lineSeg2.x2 - lineSeg2.x1), y: lineSeg2.y1 + (((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) * (lineSeg2.y2 - lineSeg2.y1) } : false;
}
// And just in case you want a reallly really long line
const intersects = (lineSeg1, lineSeg2) => (!((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) ? false : (((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) >= 0 && (((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) <= 1 && (-((lineSeg2.x1 - lineSeg2.x2) * (lineSeg2.y1 - lineSeg1.y1) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg2.x1 - lineSeg1.x1)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) >= 0 && (-((lineSeg2.x1 - lineSeg2.x2) * (lineSeg2.y1 - lineSeg1.y1) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg2.x1 - lineSeg1.x1)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) <= 1 ? { x: lineSeg2.x1 + (((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) * (lineSeg2.x2 - lineSeg2.x1), y: lineSeg2.y1 + (((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / ((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) * (lineSeg2.y2 - lineSeg2.y1) } : false;
Example 2: intersection lines
from sympy import Point, Line
p1, p2, p3 = Point(0, 0), Point(1, 1), Point(7, 7)
l2 = Line(Point(1, 4), Point(4, 1))
showIntersection = l1.intersection(l2)
print(showIntersection)