Determining ordering of vertices to form a quadrilateral

前端 未结 5 975
离开以前
离开以前 2021-02-10 18:24

Suppose I have 4 vertices in 2D space. Does anything know of an efficient algorithm which will give me an ordering of the vertices which corresponds to a simple quadrilateral?

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-10 19:00

    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.

提交回复
热议问题