Test if two lines intersect - JavaScript function

前端 未结 10 1545
谎友^
谎友^ 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 09:46

    First, find intersection coordinates - here it's described in detail: http://www.mathopenref.com/coordintersection.html

    Then check if the x-coordinate for intersection falls within the x ranges for one of the lines (or do the same with y-coordinate, if you prefer), i.e. if xIntersection is between lineAp1x and lineAp2x, then they intersect.

提交回复
热议问题