Test if two lines intersect - JavaScript function

前端 未结 10 1543
谎友^
谎友^ 2020-12-02 09:08

I\'ve tried searching for a javascript function that will detect if two lines intersect each other.

The function will take the x,y values of both the start end point

10条回答
  •  温柔的废话
    2020-12-02 10:05

    I've rewritten Peter Wone's answer to a single function using x/y instead of lat()/long()

    function isIntersecting(p1, p2, p3, p4) {
        function CCW(p1, p2, p3) {
            return (p3.y - p1.y) * (p2.x - p1.x) > (p2.y - p1.y) * (p3.x - p1.x);
        }
        return (CCW(p1, p3, p4) != CCW(p2, p3, p4)) && (CCW(p1, p2, p3) != CCW(p1, p2, p4));
    }
    

提交回复
热议问题