Extending a line segment to fit into a bounding box

前端 未结 5 1647
误落风尘
误落风尘 2021-01-12 03:25

I have a line segment defined by two pointFs, along with a 2D bounding rectangle. I want to extend the line segment as much as possible in both directions so t

5条回答
  •  悲&欢浪女
    2021-01-12 04:10

    Define the rectangle as four lines.

    Find the intersection between your line and each of the four lines. (How's your highschool geometry?)

    Of these four intersection points, determine which points are within the bounds of the rectangle. (find the intersection points where both the x and y values are within the rectangles range).

    Your algorithm must also allow for the following edge cases:

    • The line is parallel with either the vertical of horizontal edge of the rectangle
    • The line actually intersects with a corner of the rectangle.

提交回复
热议问题