# Creating a Polygon with n sides

• 05-17-2010
seeplusplus
Creating a Polygon with n sides
I need help with coming up with the algorithm that is required to create a ngon. The user gives the center of the polygon (x, y), the distance to the vertices (r), and the number of sides.

So for say a pentagon i would get the users information and i can from there go to the first point of the polygon (x+r,y) (this must be one of the point of the polygon) so the pentagon is going to be sideways.

I can get the side length by taking the inner angle (360 / n) which in this case is 72 degrees. i convert that to radians. then i also find degree of the other two equal angles by taking (180 - 72)/2 which gives 54 and convert that to radians. I then use the formula

So now i have the side length i just cant figure out the next x and y coordinates to go to. could someone help explain to me how i would go about solving this? the c++ code would be nice but that i can do on my own after i conquer this problem.

cheers.
• 05-17-2010
tabstop
Polar -> rectangular is the way to go. So if we pretend for the moment that the center of the ngon is at the origin, then you'll have the corners at (r,0), (r,72), (r,144), (r,216), and (r,288). You can then translate back to where you're supposed to be by adding the center (x,y) to each of those.