Wednesday, April 11, 2012

Regular Polygons in Polar Coordinates

Spaceship designs often have components that look like cylinders of regular polygons.  Not surprisingly, regular polygons and polar coordinates don't seem to play nicely together at first glance, but I'd really like to stick with polar coordinates for the hull extrusion, just to make everything conceptually simple.

That being said, it only took ten minutes or so to derive the formula for the radius of a regular polygon given an angle and the order of the polygon.  Here's what the code looks like:


float PolyRadius(float theta, uint n) {
theta = fmodf(theta, TAU / (float)n);
float angle2 = PI2 * (float)(n - 2) / (float)n;
float angle3 = PI - angle2 - theta;
float sideLength = sinf(angle2) / sinf(angle3);
return sideLength;
}


"Tau" is 2*pi, and "PI2" is pi/2.  The rest should be straightforward.

Here's a pentagonal ship, for no reason in particular...


No comments:

Post a Comment