Asymptotically optimal algorithm to compute if a line intersects a convex polygon

前端 未结 5 1005
北荒
北荒 2021-02-01 05:25

An O(n) algorithm to detect if a line intersects a convex polygon consists in checking if any edge of the polygon intersects the line, and look if the number of intersections is

5条回答
  •  别跟我提以往
    2021-02-01 06:16

    take a random two points A and B from convex poligon.

    • if they are on different side of the line, they intersect
    • if they are on the same side, on both poligon parts (lets say clockwise AB and BA) do:
      • create a line parallel to your line l that goes through A
      • find point at maximal distance from l on the poligon, which is same as finding maximum in function that is first monotonically nondecreasing, and then monotonically nonincreasing. this can be done in O(log n) by ternary search


    if those two furthest points are on different side of your line, line intersects poligon, otherwise it doesn't

    By the way you can also find actual intersection points in O(log n) too.

提交回复
热议问题