Are 2 line segments intersecting

suggest change

The function in this example returns true if two line segments are intersecting and false if not.

The example is designed for performance and uses closure to hold working variables.

// point object: {x:, y:}
// p0 & p1 form one segment, p2 & p3 form the second segment
// Returns true if line segments are intersecting
var lineSegmentsIntercept = (function(){ // function as singleton so that closure can be used
    var v1, v2, v3, cross, u1, u2;  // working variables are closed over so they do not need creation
                                    // each time the function is called. This gives a significant performance boost.
    v1 = {x : null, y : null}; // line p0, p1 as vector
    v2 = {x : null, y : null}; // line p2, p3 as vector
    v3 = {x : null, y : null}; // the line from p0 to p2 as vector

    function lineSegmentsIntercept (p0, p1, p2, p3) {
        v1.x = p1.x - p0.x; // line p0, p1 as vector
        v1.y = p1.y - p0.y;
        v2.x = p3.x - p2.x; // line p2, p3 as vector
        v2.y = p3.y - p2.y;

        if((cross = v1.x * v2.y - v1.y * v2.x) === 0){  // cross prod 0 if lines parallel
            return false; // no intersection
        }
        v3 = {x : p0.x - p2.x, y : p0.y - p2.y};  // the line from p0 to p2 as vector
        u2 = (v1.x * v3.y - v1.y * v3.x) / cross; // get unit distance along line p2 p3
        // code point B
        if (u2 >= 0 && u2 <= 1){                   // is intersection on line p2, p3
            u1 = (v2.x * v3.y - v2.y * v3.x) / cross; // get unit distance on line p0, p1
            // code point A
            return (u1 >= 0 && u1 <= 1);           // return true if on line else false
            // code point A end
        }
        return false; // no intersection
        // code point B end
    }
    return lineSegmentsIntercept;  // return function with closure for optimisation.
})();

Usage example

var p1 = {x: 100, y: 0};   // line 1
var p2 = {x: 120, y: 200};
var p3 = {x: 0,   y: 100}; // line 2
var p4 = {x: 100, y: 120};
var areIntersecting = lineSegmentsIntercept (p1, p2, p3, p4); // true

The example is easily modified to return the point of intersection. Replace the code between code point A and A end with

if(u1 >= 0 && u1 <= 1){
    return {
        x : p0.x + v1.x * u1,
        y : p0.y + v1.y * u1,
    };
}

Or if you want the intersection point on the infinite lines, ignoring the segment start and end, replace the code between code point B and B end with

return {
    x : p2.x + v2.x * u2,
    y : p2.y + v2.y * u2,
};

Both modifications return false if there is no intersection, or the point of intersection as {x : xCoord, y : yCoord}. Parallel segments, including coincident ones, return false because their cross product is 0.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents