Given a vector of points (possibly out of order), find polygon (not convex hull)

前端 未结 3 1871
小蘑菇
小蘑菇 2021-02-15 11:48

I currently have a vector of points

vector corners;

where I have previously stored the corner points of a given polygon. Given tha

3条回答
  •  星月不相逢
    2021-02-15 12:40

    1. Heuristic to determine the shape

    There is no unique solution so there is no simple algorithm. You could try to somehow mimic your intuition.

    • start by a random point and connect each point to its closest neighbor. Then connect the last point to the first one.
    • start by selecting a point and its closest neighbor and connect them by a line. Now iteratively add another point. Always select the point which minimizes the angle between the last line segment and the newly added line segment.

    Both methods don't really work in general, they don't even guarantee to avoid intersections. You can try to address this by backtracking, if you detect an obvious error (e.g. an intersection) then backtrack to the last point of decision and take the "second best" approach instead, ....

    But again as the solution is not unique, don't expect too much from those heuristics.

    2. Average of the vertices

    The average point for the vertices is easy to compute. Just add all points together and divide through the number of points you just added, this is the average. What you are probably more interested is the center point in the sense of "Center of mass", see below.

    3. Center point

    To determine the center of mass you first have to define the shape. That means you have to do something like step 1.

    An easily implemented method to compute the center point given the polygon is.

    • Put a bounding box around the polygon.
    • Randomly generate points inside the bounding box.
    • For each of those points determine if it is inside the bounding box, if not, then throw it away. To determine if a point is inside an arbitrary polygon use a ray test.
    • For all points that you kept, apply approach 2. The Average point of those points is a good approximation to the center point.

提交回复
热议问题