[Solved] polygon coordinates


Imagine a circle of radius r. It is like a regular polygon with an infinite number of sides.

Trigonometry tells us:

x = r * cos(a);
y = r * sin(a);

We know there are 360 degrees or 2pi radians in a circle. So to draw it we would start with angle = 0, calculate that co-ord, step to the next angle and calculate that point, then draw a line between the two.

There are only so many points we can calculate around the edge of the circle, eventually it won’t make any difference. If the circle is small enough, even 8 sides will look round.

To draw an 8 sided circle we want 8 points evenly spaced around the circle. Divide the circle into 8 angles, each one is 2 * pi / 8 radians.

So:

angle = 0.0;
step = 2 * pi / 8;

for ( n = 0; n < 8; n++ ) {
    x = radius * cos(angle);
    y = radius * sin(angle);
    angle += step;
}

Now you can draw an octagon, change it to draw the general case.

solved polygon coordinates