Detect if line segment intersects square

前端 未结 3 743
面向向阳花
面向向阳花 2020-12-21 04:25

Anyone have a simple algorithm for this? No need for rotation or anything. Just finding if a line segment made from two points intersects a square

3条回答
  •  情深已故
    2020-12-21 05:11

    Here's a way:
    - sort the vertex points of the square by x-coord
    - sort the endpoint of the line by x-coord
    - calculate angle from the minX end of the line to each of the middle two (by x-coord) square vertices
    - calculate angle of the line
    - if the line's angle is within the possible angles, all you have to do is a length check, is maxX end of the line > minX vertex of the square

    This will probably break if the square is directly facing the line, in that case I'd just special-case it by checking the first edge of the square.

提交回复
热议问题