How to test if a line segment intersects an axis-aligned rectange in 2D?

前端 未结 12 768
执念已碎
执念已碎 2020-11-30 07:33

How to test if a line segment intersects an axis-aligned rectange in 2D? The segment is defined with its two ends: p1, p2. The rectangle is defined with top-left and bottom-

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 08:01

    Since your rectangle is aligned, Liang-Barsky might be a good solution. It is faster than Cohen-Sutherland, if speed is significant here.

    Siggraph explanation
    Another good description
    And of course, Wikipedia

提交回复
热议问题