# create a poligon with the given # of vertices and radius

Hello everyone, To not bore you with details ill go strait to the problem, i want to create a poligon where the player can input the amount of verts it has. for now i just want to create the points in 3d space.

To do this I tried to get the internal angle of the poligon dividing 360 by the # of vertices, then used that angle with the radius to create a vector and obtained its x and y components (all in 2d space so y is actualy z in 3d space) finaly to test it i created a cube at each point

problem is some odd things are happening: the distance from the center is accurate but the distance between a vert and its adjacent points is not always the same as if it had the wrong angle

here is the code:

``````public int verts;

public List <Vector3> points = new List<Vector3>();

void Start ()
{
CreateCircle();
}
void CreateCircle ()
{
float ang = 360/ verts ;
for (int i = 0; i < verts; i++)
{
//calculate the angle between points
float cAng = ang * i;
print ("ang" + ang);
Vector2 ornt = Vector2.zero;
//calculate the points x and z position
float x = Mathf.Abs(radius * Mathf.Cos(cAng));
float z = Mathf.Abs(radius * Mathf.Sin(cAng));

if (cAng <= 90) ornt = new Vector2(1, 1) ;
else if (cAng <= 180) ornt = new Vector2(-1, 1) ;
else if (cAng <= 270) ornt = new Vector2(-1,-1) ;
else if (cAng <= 360) ornt = new Vector2( 1, -1) ;

print ("orient" + ornt);

//add the points to a List
points.Add(new Vector3(x * ornt.x, 0, z * ornt.y));
print("dist" + Vector3.Distance(new Vector3(x, 0, z), Vector3.zero));
}
//visualy display the points
DisplayPoints();
}
void DisplayPoints ()
{
for (int i = 0; i < points.Count; i++)
{
GameObject dspl = GameObject.CreatePrimitive(PrimitiveType.Cube);
dspl.transform.position = points *;*
``````
• `````` }*
``````
• }*

I would realy apreciate any advice, fix or perhaps a better of doing this. and sory for the long post. Have a happy valentines day!

public int verts;

``````....

float ang = 360/ verts ;
``````

With that code, “ang” will always have an integer value, even if it’s a float variable. A division between 2 integers will give an integer. This will create a bigger angle for the last piece in some cases.

You should do:

``````float ang = 360.0f/ verts ;
``````

For the rest of the code, do you really need this?

`````` float x = Mathf.Abs(radius * Mathf.Cos(cAng));
float z = Mathf.Abs(radius * Mathf.Sin(cAng));
if (cAng <= 90) ornt = new Vector2(1, 1) ;
else if (cAng <= 180) ornt = new Vector2(-1, 1) ;
else if (cAng <= 270) ornt = new Vector2(-1,-1) ;
else if (cAng <= 360) ornt = new Vector2( 1, -1) ;

....

points.Add(new Vector3(x * ornt.x, 0, z * ornt.y));
``````

You’re removing the sign of x and z and adding it again. Cos and Sin of the angle should give you the right sign directly.