Bennett Kanuka

Using math and software to solve real world problems

Drawing Regular n-gons with Horizontal Bottom

I was re-reading my post on drawing a pentagon in LaTeX and realized I never explained how I got the coordinates of the pentagon. I also didn’t generalize the solution to drawing n-gons. I would like to correct those issues.

We start with the unit circle centered at (0,0). The coordinates of any point on the circle are given by:

Therefore we can find the coordinates of the regular n-gon at:

However, this does not guarantee that the n-gon’s bottom edge will be horizontal (something we’d want for a visually pleasing drawing). To the above formula, we can apply a starting angle $t$ measured from the x-axis.

Changing $t$ will rotate the n-gon’s starting vertex. To guarantee the bottom edge is horizontal, we rotate the starting vertex to the bottom of the unit circle, and then one half of $\frac{2 \pi}{n}$, or

And using the trig identities,

we can simplify the equations to:

If we are interested in drawing an n-gon with circumcircle of radius $r$, centered at $(a,b)$ then we can simply multiply by $r$ and add an offset:

This is easily written as a Python function:

import numpy as np

def ngon(n, r=1, a=0, b=0):
vertecies = []
for k in range(n):
x = a + r*np.sin(np.pi/n + (k*2*np.pi)/n)
y = b - r*np.cos(np.pi/n + (k*2*np.pi)/n)
vertecies.append((x,y),)
return vertecies