Line clipping to arbitary 2D polygon

后端 未结 2 1701
梦毁少年i
梦毁少年i 2021-01-12 13:44

If I get a line segment which was long enough to cross a given polygon, which could be concave or convex polygon. How did I find the all the intersected light segments which

2条回答
  •  日久生厌
    2021-01-12 14:00

    • Step one: find all intersection points, in any order. For polygon, you need to find intersection of red line and line of each segment. Just solve system of two linear equations. If solution is constrained to polygon segment limits, you have an intersection.
    • Step two: sort found points by position on red line. You know that first and last point are outer ones. "Outerness" flips with each point - outer-inner-outer and so on. Between two adjacent outer points you have inner line segment (green). Edit: not true... Start with point #0, segment #0 - #1 is inner, next is outer, next is inner again and so on.

    If region is not polygon, but is given by some implicit function, you need to find where that function is equal to red line (approach depends on function, of course).

提交回复
热议问题