Test point for its position relative to the convex hull in log(n)

后端 未结 6 1310
清歌不尽
清歌不尽 2021-01-04 05:42

I have a collection of 2D points S and I need to test if an input point q is inside or outside the convex hull of S.

Since it\

6条回答
  •  攒了一身酷
    2021-01-04 06:08

    You should be able to do this using a sweep algorithm (like rasterization, say using a horizontal sweep line). Building up the sorted edges of vertices is n*log(n), but once sorted you could find set the sweep line based on point q and find the edges that the sweep line crosses.

    Rasterization is simplified in the convex case since you don't have to worry about concavities in the sweep line.

    A simple outline is to go around the polygon, constructing edge objects, using the winding to determine left and right sides. All y-values for each point go into a sorted list (or array, or set, or map, whatever).

    Your point q.y is used to look up the edge(s) in left and right sides, then you can simply determine if q.x is between the left and right coordinates. You can compute the convex hull first to make sure your left/right sides are convex.

    (Wow, in searching out raster-scan conversion, I came across the notes from my undergrad class here from the year after I graduated.)

提交回复
热议问题