There are several approaches to determine whether a list of polygon points is ordered clockwise or counterclockwise.
In this tutorial, we’ll create a simple algorithm to determine the polygon points’ orientation. Furthermore, we’ll revise and use some formulas for the area computation. We assume having a basic knowledge of polygons, and we’ll also use basic definitions of linear algebra.
2. Order of Polygon Points
Let’s recall the way the polygon is stored in memory. Assume is our polygon. It consists of points. Therefore, we can represent as , where . So the shape of is represented as the list of points.
We’ll also define another polygon . It’s important to note it consists of the same points in a reversed order. and are two different polygons, which look absolutely the same and have equal shapes and areas.
What differs between them is their point orientation. If the points in are oriented clockwise, then point orientation in is counterclockwise and vice versa:
We’ll use this information about point orientation further in the tutorial. Moreover, as previously mentioned, the areas of and are numerically equal, but the signs of the areas are different.
3. Area Computation
There is a simple linear-time algorithm for the 2D polygon area computation. The algorithm introduces a formula for the area. We’ll briefly discuss the key aspects of the polygon area calculation, as well as use this formula in our point orientation algorithm.
The area of a shape can either be positive or negative. If the shape is represented as a list of points, then it depends on the orientation of the points. Let’s briefly remember the formulas for calculating the areas of triangles and polygons.
3.1. Area of a Triangle
Now let’s suppose a triangle is defined by 3 points , , and :
The area of this triangle can be computed with a simple formula from linear algebra: .
If we shift our triangle to make point be , the area of the triangle won’t change:
However, the formula of area computation will be a bit simplified:
3.2. Area of Polygon
Now we can calculate the area of a polygon using the formula for the triangle area. The idea of the approach is simple. It uses the decomposition technique. We can decompose the polygon of points to triangles. To do this, we choose the arbitrary point , usually .
After that, we can compute the areas of triangles. Assume we have a polygon represented with the set of points . We form a triangle by picking points , , and with , . Then we sum up all the areas to get the area of the entire polygon.
The final formula that computes the area of the polygon:
, where with .
It’s important to remember that the area of the polygon can be either positive or negative. It depends on the chosen point for decomposition and order of the vertices.
Let’s define a simple Signum function . It takes an arbitrary number as a parameter and returns only three values. If , . If , . Finally, if , . We may notice this function gives us information about the sign of the number.
Now let’s introduce an algorithm for getting the points orientation of polygon . First, we compute the signed area by the above formula to get the number . Second, we compute . Finally, we compare with zero. If , then the points are oriented counterclockwise.
Similarly, if , then the points have a clockwise orientation. We wouldn’t expect the case when . That would mean the area of the polygon is 0. In that case, it’s probable that the polygon was not correctly defined.
5. Time and Space Complexity
The time complexity of the above algorithm is , where is the number of points of the polygon. It takes linear time to compute the area. After that, it takes time to learn whether the area is positive or not. Thus, the final complexity is .
The space complexity is because we only need one variable to store the area.
In this article, we’ve introduced an algorithm, which helps to determine polygon point orientation in linear time. Furthermore, we’ve recalled a simple approach to compute the area of a polygon. There exist several other techniques for determining point orientation; however, all of them are based on this simple idea.