line line intersection

Solutions on MaxInterview for line line intersection by the best coders in the world

showing results for - "line line intersection"
Camilo
17 Jan 2020
1const intersects = (lineSeg1, lineSeg2) => {
2	const x1 = lineSeg2.x1;
3    const y1 = lineSeg2.y1;
4    const x2 = lineSeg2.x2;
5    const y2 = lineSeg2.y2;
6
7    const x3 = lineSeg1.x1;
8    const y3 = lineSeg1.y1;
9    const x4 = lineSeg1.x2;
10    const y4 = lineSeg1.y2;
11
12    const den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
13    if (!den) return;
14
15    const t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
16    const u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;
17    return t >= 0 && t <= 1 && u >= 0 && u <= 1 ? {x: x1 + t * (x2 - x1), y: y1 + t * (y2 - y1) } : false;
18}
19
20// Or if you'd rather
21const intersects = (lineSeg1, lineSeg2) => {
22    den = (lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2);
23    if (!den) return;
24    t = ((lineSeg2.x1 - lineSeg1.x1) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg1.y1) * (lineSeg1.x1 - lineSeg1.x2)) / den;
25    u = -((lineSeg2.x1 - lineSeg2.x2) * (lineSeg2.y1 - lineSeg1.y1) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg2.x1 - lineSeg1.x1)) / den;
26    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;
27}
28
29// Or if you really want
30const intersects = (lineSeg1, lineSeg2) => {
31	if (!((lineSeg2.x1 - lineSeg2.x2) * (lineSeg1.y1 - lineSeg1.y2) - (lineSeg2.y1 - lineSeg2.y2) * (lineSeg1.x1 - lineSeg1.x2))) return;
32    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;
33}
34
35// And just in case you want a reallly really long line
36const 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;
Lea
21 Mar 2018
1# import sympy and Point, Line
2from sympy import Point, Line
3  
4p1, p2, p3 = Point(0, 0), Point(1, 1), Point(7, 7)
5l2 = Line(Point(1, 4), Point(4, 1))
6
7# using intersection() method
8showIntersection = l1.intersection(l2)
9
10print(showIntersection)
11