Skip to content

Instantly share code, notes, and snippets.

@smkplus
Last active July 24, 2018 05:22
Show Gist options
  • Save smkplus/469877518bf50221b0cdae8f13b84c56 to your computer and use it in GitHub Desktop.
Save smkplus/469877518bf50221b0cdae8f13b84c56 to your computer and use it in GitHub Desktop.

http://www.vcskicks.com/regular-polygon.php

private Vector3[] CalculateVertices(int sides, int radius, int startingAngle, Vector3 center)
{
    if (sides < 3)
        throw new ArgumentException("Polygon must have 3 sides or more.");

    List<Vector3> points = new List<Vector3>();
    float step = 360.0f / sides;

    float angle = startingAngle; //starting angle
    for (double i = startingAngle; i < startingAngle + 360.0; i += step) //go in a full circle
    {
        points.Add(DegreesToXZ(angle, radius, center)); //code snippet from above
        angle += step;
    }

    return points.ToArray();
}
private Vector3 DegreesToXZ(float degrees, float radius, Vector3 origin)
{
	Vector3 result = Vector3.zero;
    double radians = degrees * Math.PI / 180.0;

    result.x = (float)Math.Cos(radians) * radius + origin.x;
    result.z = (float)Math.Sin(-radians) * radius + origin.z;

    return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment