Coordinates of a Regular Polygon

For my HUD Designer, I’m attempting [and failing] to make a VGUI element that allows you to create a polygon using surface.DrawPoly. However, here’s the issue: when the user creates the polygon, I want the vertices to be however many they enter, let’s say “n”, to form a regular polygon inscribed in a square of side length “s”.

How exactly would I go about finding all the necessary coordinates?

Assuming a regular polygon can always fit inside of a circle perfectly, you just have to use sin and cosine and do the points with 360/n and multiply by a length found using the side length of the polygon. I may be wrong, though.


Example code, not tested.

local coords = {}; // Table of coordinates
local n = 5; // Number of points
local len = 50; // Length of sides
local originX = 0; // X Origin of polygon
local originY = 0; // Y Origin of polygon

local radius = len / (2 * math.sin(math.pi/n));

for i=1,n do

local x = originX + (math.cos(math.Deg2Rad((360/n)*i)) * radius);
local y = originY + (math.sin(math.Deg2Rad((360/n)*i)) * radius);
table.insert(coords, {x, y});



After rereading your post, if you want it to be the size of a square, just change the radius to half of the length of your square.

I will certain see if that works. Thank you very much.
Question though: would the origin be the center of the square in this instance?