CCW or CW ordering of the points of a generic polygon

In summary, the article discusses ways to calculate the area and the centroid location of a polygon, but does not mention that the centroid location in general is the arithmetic mean of the vertex coordinates.
  • #1
Estanho
14
2
Hi,
Suppose I have a data structure that models a polygon, by storing all the nodes of the given polygon, and their connections (i.e. edges).
I understand that sorting a set of nodes that may form a concave hull is ill-defined, as there could be many polygons that can be formed with those. But I'm asking about the case where the polygon is already defined by the edges. I'm attaching a drawing of such a polygon.
Is there an efficient and generic way to order the nodes of any such polygon, which may be concave, in ccw or cw?
 

Attachments

  • polygon.png
    polygon.png
    2.3 KB · Views: 605
Technology news on Phys.org
  • #2
Estanho said:
Hi,
Suppose I have a data structure that models a polygon, by storing all the nodes of the given polygon, and their connections (i.e. edges).
I understand that sorting a set of nodes that may form a concave hull is ill-defined, as there could be many polygons that can be formed with those. But I'm asking about the case where the polygon is already defined by the edges. I'm attaching a drawing of such a polygon.
Is there an efficient and generic way to order the nodes of any such polygon, which may be concave, in ccw or cw?
It depends on what you want to do with these points after they're ordered.

If the points are ordered CCW with respect to the origin, it becomes much easier to calculate the area of the closed polygon, for instance.
 
  • #3
The algorithm I use is the following:
(1) Find the centroid of the polygon - just the arithmetic mean of the points.
(2) Calculate the angle from the centroid to each point. I use the c++ atan2 function: angle = atan2(pointlist->y - cy, pointlist->x - cx).
(3) Order the points in increasing order by angle.

This works if the polygon is not too pathological, although it will probably fail if the centroid is outside the polygon.
 
  • #4
phyzguy said:
The algorithm I use is the following:
(1) Find the centroid of the polygon - just the arithmetic mean of the points.
(2) Calculate the angle from the centroid to each point. I use the c++ atan2 function: angle = atan2(pointlist->y - cy, pointlist->x - cx).
(3) Order the points in increasing order by angle.

This works if the polygon is not too pathological, although it will probably fail if the centroid is outside the polygon.
You have stumbled upon the concept of winding numbers - an important concept in complex analysis. It is explained in Ahlfors (https://editorialdinosaurio.files.wordpress.com/2012/03/ahlfors_-_complex_analysis.pdf) page 114 ff.
 
  • #5
phyzguy said:
The algorithm I use is the following:
(1) Find the centroid of the polygon - just the arithmetic mean of the points.
(2) Calculate the angle from the centroid to each point. I use the c++ atan2 function: angle = atan2(pointlist->y - cy, pointlist->x - cx).
(3) Order the points in increasing order by angle.

This works if the polygon is not too pathological, although it will probably fail if the centroid is outside the polygon.
The centroid location for a triangle is the arithmetic mean of the coordinates of the vertices, but I don't think this can be extended to a general polygon where n > 3.

This article contains formulas for calculating the area and the location of the centroid of a closed polygon of an arbitrary number of sides, n ≥ 3:

https://en.wikipedia.org/wiki/Polygon

The article doesn't mention that the centroid location in general is the arithmetic mean of the vertex coordinates.

These formulas and more can be derived by applying Green's Theorem in the plane to the integrals which define the area and first moment of area of a polygonal region. By careful description of the region under consideration, polygons with "holes" or similar non-convex regions can be evaluated, so long as there are no overlapping sides to the region.
 

FAQ: CCW or CW ordering of the points of a generic polygon

What is CCW and CW ordering of points in a polygon?

CCW (counter-clockwise) and CW (clockwise) ordering refer to the direction in which the vertices (points) of a polygon are listed or ordered. In CCW ordering, the vertices are listed in a counterclockwise direction, while in CW ordering, they are listed in a clockwise direction.

Why is it important to know the CCW or CW ordering of points in a polygon?

The CCW or CW ordering of points in a polygon is important because it determines the orientation of the polygon, which can affect various geometric calculations and algorithms. For example, the orientation of a polygon can determine whether it is considered to be inside or outside of another polygon, which is important in collision detection and other applications.

How do you determine the CCW or CW ordering of points in a polygon?

The CCW or CW ordering of points in a polygon can be determined by using the "right-hand rule." This rule states that if you curl the fingers of your right hand in the direction of the vertices, your thumb will point in the direction of the normal vector of the polygon. If the normal vector points in the same direction as the viewing direction (typically from left to right), then the polygon is considered to have CCW ordering. If the normal vector points in the opposite direction, then the polygon is considered to have CW ordering.

Can a polygon have both CCW and CW ordering of points?

No, a polygon can only have one ordering of points. However, the ordering can be reversed by simply reversing the order in which the points are listed. For example, if a polygon has CCW ordering, reversing the order of the points will result in CW ordering.

Are there any conventions for the CCW or CW ordering of points in a polygon?

Yes, there are some common conventions for the CCW or CW ordering of points in a polygon. One convention is to list the vertices in a counterclockwise direction for convex polygons and in a clockwise direction for concave polygons. Another convention is to list the vertices in the same order as they appear in the polygon's definition or equation. However, these conventions are not always followed, so it is important to clarify the ordering when working with polygons.

Similar threads

Replies
15
Views
2K
Replies
9
Views
2K
Replies
8
Views
2K
Replies
9
Views
11K
Replies
15
Views
1K
Back
Top