polygon
point sorting
graphics algorithm
computational geometry
polygon drawing

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.

Practice algorithms

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:

  1. you already have polygon vertices but they are rotated or reversed,
  2. you have the vertices of a convex polygon in random order,
  3. 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:

  1. compute the centroid,
  2. compute the angle of each point relative to that centroid with atan2,
  3. sort by that angle.
python
1import math
2
3def sort_polygon_points(points):
4    cx = sum(x for x, _ in points) / len(points)
5    cy = sum(y for _, y in points) / len(points)
6
7    return sorted(
8        points,
9        key=lambda p: math.atan2(p[1] - cy, p[0] - cx)
10    )
11
12
13points = [(1, 1), (0, 0), (1, 0), (0, 1)]
14print(sort_polygon_points(points))

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.

python
1def edges_from_points(points):
2    ordered = sort_polygon_points(points)
3    edges = []
4
5    for i in range(len(ordered)):
6        a = ordered[i]
7        b = ordered[(i + 1) % len(ordered)]
8        edges.append((a, b))
9
10    return edges
11
12
13for edge in edges_from_points(points):
14    print(edge)

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:

python
ordered = sort_polygon_points(points)
clockwise = list(reversed(ordered))
print(clockwise)

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 atan2 around 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.