In the spring of 2001 I spent a little time wondering about regular polygons and how to draw them, so I wrote a little PHP app to draw an arbitrary regular polygon. It works like this...

It turns out that you can construct a polygon of N vertices by starting with a polygon of N-1 vertices and then sticking a triangle onto it. Also remember that if some angle is composed of two adjacent angles, then the measure of the angle is equal to the sum of the measures of the adjacent angles. Finally, a polygon has to consist of at least 3 vertices (otherwise you'd be talking about a line segment or a point). So, the sum of the interior angles of a polygon of N vertices is equal to pi times N-2. Note: although the general public deals with degrees, the math is a lot easier to do in radians. So, for a regular polygon, the interior angles are all the same, and the measure of the interior angle is equal to (pi * N-2)/N.

Just about any graphics library will give you the ability to specify an array of points and call it a polygon, and all of them will let you draw a line between two points. So, I needed to figure out where on the coordinate plane the vertices lie. Say we draw a circle centered on the origin, of radius R. If we think of our regular polygon, we can think of the vertices as lying on the circle, and the sides as being chords. If we think of the plane in polar coordinates, then what we really need to do is find an angle, phi, which is (2 * pi)/N. Then for each vertex V[i] where i=0..N-1, the polar coordinate is (R, phi * i).

I wrote this in PHP, so the code looks like this:

$phi = (2 * M_PI)/$N;
for ($i=$N-1; $i>=0; $i--)
{
    $theta = $i * $phi;
    $polar_vertices[$i] = $theta; // R is constant, so we don't need to store it
}

But very few graphics libraries will let you specify coordinates in polar form; they want you to give points in cartesian form. Fine -- the (x,y) pair that matches (R, theta) is (R * cos(theta), R * sin(theta)). And here's where doing all this in radians comes in handy -- because the trig functions don't work with degrees; they work with radians.

for ($i=$N-1; $i>=0; $i--)
{
    $x_coord[$i] = $R * cos($polar_vertices[$i]);
    $y_coord[$i] = $R * sin($polar_vertices[$i]);
}

Now we've got a bunch of vertices, but the coordinates are centered on the origin. Now, maybe there's a graphics library out there somewhere that sets the origin at the middle of the drawing area, but the ones I've run into all set the origin at either the top left or bottom left of the drawing area, so that you only deal with positive coordinates. So, we have to do a further transformation of the coordinates -- shift 'em to the right and "up" so that the range of values, instead of going from -R to R, goes from 0 to 2*R.

for ($i=$N-1; $i>=0; $i--)
{
    $x_coord[$i] = ($R * cos($polar_vertices[$i])) + $R;
    $y_coord[$i] = ($R * sin($polar_vertices[$i])) + $R;
}

In considering the relationship between radius and side length, consider how I draw the polygon; it's composed of a bunch of triangles, with the bases being the edges of the polygon and the apexes at the center. Each of those triangles is identical, and happens also to be isosceles. What this means is that if we draw the height, h, of one of these triangles (going from the apex at the center of the polygon to the base which is one of the polygon's sides) then we get two right triangles. The apex angle of the original triangle gets bisected, as does the base. The base angles of the original triangle are equal to half of the interior angle of the polygon. So, we know the measure of the base of the right triangle (half the side length) and the measure of the angles (since the angle measures derive from the number of vertices). Furthermore, if we knew the radius we could calculate the side length like this: (side / 2) = radius * sin(phi/2). Isolating the unknown (the radius) gives us the equation: radius = side / (2 * sin(phi/2)).