Determining ordering of vertices to form a quadrilateral

点点圈 提交于 2019-12-03 09:12:17

You may be interested in methods of convex hull computing, such as Graham scan.

If your shape is convex, you can go in winding order around the barycentre of your points (i.e. the centre of gravity, or "average"):

B = (X_1 + X_2 + X_3 + X_4) / 4

Both coordinates of each vertex will be either above or below the corresponding coordinate of the barycentre:

 (-,+)                   (+,+)
   X                       X

              B
      X
    (-,-)               X
                      (+,-)

So starting at any point, just move to one point for which only one of the two signs changes, but not both.

If your shape is not convex, you could first triangulate it with an interior edge, apply the vertex ordering with consistent orientation for each triangle, and then merge the edges by cancelling out the pairwise-opposite interiors.

Note that for a non-convex set of points (i.e. a set where one point is contained in the open interior of the convex hull of the set), there may be more than one quadrilateral with those points as vertices (think of all the ways of joining the inner vertex to two of the outer ones).

TMS

You can just google for sorting points in clockwise order and you get e.g.:

Sort four points in clockwise order
Sort points in clockwise order?

There is a solution which does not require computing a "center", involves only a few multiplications and additions, and gracefully handles degenerate cases (such as all points collinear).

Consider a quadrilateral with corners ABCD (ie there are lines AB, BC, CD, and DA).

Consider the four triangles ABC, ABD, ACD, BCD

There is a simple formula for working out the area of each triangle, see http://www.cs.princeton.edu/~rs/AlgsDS07/16Geometric.pdf page 9, if the vertices are (Ax, Ay), (Bx, By), (Cx, Cy)

Area = (Bx - Ax)(Cy - Ay) - (By - Ay)(Cx - Ax)

If the area if positive the points are counter clockwise, negative they are clockwise, zero they are collinear.

It is possible for a non-intersecting quadrilateral to have three collinear points. So one of the triangles ABC, ABD, ACD, BCD could have area zero. But if two of them have zero area, it means ABCD must be collinear and so all four triangles have zero area. In this case, a nonintersecting arrangement is impossible.

So calculate the areas corresponding to ABC, ABD, and ACD (you only need to consider three triangles).

If two of them have zero area, all three will have zero area, the four points ABCD are collinear.

If one of them has zero area, pick the other two areas.

If none of them has zero area, just pick any two of the three areas.

If the quadrilateral does not intersect, then these two triangles must wind the same way, ie both clockwise or both counterclockwise. Which means the product of the two areas must be positive (a positive by a positive or a negative by a negative). So simply form the product of the two areas, both of which are nonzero, to get a non zero result. If the product of the areas is greater than zero, the quadrilateral does not self-intersect, if it is less than zero it does self-intersect.

A morton-curve or z-curve will deliver it. But I suggest a hilbert-curve or a moore-curve because of better space filling attributes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!