Sort polygon's points for drawing
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
To draw a polygon correctly, the vertices must be ordered around the boundary, usually clockwise or counterclockwise. The common quick solution is to sort points by angle around a center point, but that works reliably only when the points already describe a simple convex or star-shaped polygon.
What "Sorting Polygon Points" Really Means
This problem is often underspecified. There are at least three different situations:
- you already have polygon vertices but they are rotated or reversed,
- you have the vertices of a convex polygon in random order,
- you have an arbitrary cloud of points and want to invent a polygon from them.
Those are not the same problem.
If the polygon is known to be convex, angle sorting around the centroid is a common and effective solution. If the points are arbitrary, there may be no single correct polygon at all.
The Convex-Polygon Approach
For a convex polygon, a standard method is:
- compute the centroid,
- compute the angle of each point relative to that centroid with
atan2, - sort by that angle.
The resulting order is counterclockwise around the centroid.
Why atan2 Is the Right Tool
atan2(y, x) gives the angle of a point relative to the origin while handling the correct quadrant automatically. That makes it much safer than trying to sort by slope.
If you sort by slope alone, points above and below the center can become mixed incorrectly because the sign and quadrant information is incomplete.
Example for Drawing
Once points are ordered, you can draw edges by connecting each point to the next and finally closing the shape.
That last wraparound step closes the polygon by connecting the final vertex back to the first.
Important Limitation: This Is Not a General Polygon Solver
Angle sorting works well for convex polygons and some star-shaped cases, but it is not a universal method for arbitrary point sets.
Why not:
- a non-convex polygon may not be represented correctly by centroid-based angle order,
- the centroid may even lie outside the intended boundary,
- and arbitrary point sets may admit several different non-self-intersecting polygons or none that match your intent.
So if you are given an arbitrary cloud of points, "sort them for drawing" may actually be the wrong question. You may need:
- a convex hull,
- a triangulation,
- a specific boundary reconstruction algorithm,
- or a domain-specific ordering rule.
When the Points Already Belong to a Known Polygon
If the vertices already come from a polygon and only need normalization, the job is easier. You may only need to:
- rotate the sequence so it starts at a preferred point,
- reverse it to switch clockwise versus counterclockwise,
- or remove duplicates.
In that case, do not over-engineer with centroid sorting if the boundary order is already essentially present.
Handling Clockwise Versus Counterclockwise
Angle sorting usually gives one orientation, but some graphics pipelines or geometry algorithms require a specific winding order.
To reverse the direction:
Winding order matters for tasks such as polygon area calculations, clipping rules, and front-face detection in graphics pipelines.
A Practical Rule of Thumb
Use centroid-plus-angle sorting when:
- the polygon is convex,
- the vertices are simply shuffled,
- and you only need a usable boundary order.
Do not use it blindly when:
- the polygon may be concave,
- the point set is noisy,
- or self-intersections would be unacceptable.
In those cases, a more specific computational-geometry algorithm is required.
Common Pitfalls
The biggest pitfall is assuming angle sorting solves the arbitrary polygon reconstruction problem. It does not.
Another mistake is using the method on concave point sets and then being surprised by crossing edges or an incorrect boundary.
Developers also sometimes forget to close the polygon by connecting the final point back to the first.
Finally, be explicit about winding order. Some systems expect clockwise vertices and others expect counterclockwise vertices.
Summary
- For convex polygons, sorting points by
atan2around the centroid is a common solution. - The method gives a boundary order suitable for drawing and edge construction.
- It is not a general solution for arbitrary or highly concave point sets.
- Always close the polygon by connecting the last point back to the first.
- If the point set is not already a simple polygon, you may need a hull or reconstruction algorithm instead of a simple sort.
Related reading
- Sort the rows according to the order specified in WHERE IN clause
- Sort when only equality is available
- Sorted collection in Java
- Sorted intervals query
- Sorting a permutation with minimum cost
- Sorting algorithm to implement highest total combinations
- Sorting 100 unique numbers by using 40bytes of memory
- Sorting 10GB Data in 1 GB memory. How will I do it?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.