hwo do i script a pixelated circle?

so i want to make a procedurally generated map, that its made out of cubes with 2 units of size, and i want to make it so the player can choose the shape of the arena and the size, and i want to know how i can make it so the game instantiates the wall prefab (cube with 2 units of size) in 2 dimensions, just like this circle Imgur: The magic of the Internet with the black lines being the radius, the red line being the walls(aka the instantiated gameobject) and the green point being the origin

you dont need to give me the full code, i just want to know what calculations should i make.

This should get you started:

    public int Radius = 10;
    public Sprite Wall;

    // Use this for initialization
    void Start ()
    {
        DrawCircle();

    }

    private void DrawCircle()
    {
        int radiusSqrd = Radius * Radius;

        for (int x = -Radius; x <= Radius; x++)
        {
            int y = (int) (Mathf.Sqrt(radiusSqrd - x * x) + 0.5f);

            //This draws one half of the the circle
            GameObject go = new GameObject(x + ", " + y);

            go.transform.position = new Vector2(x, y);
            go.transform.parent = transform;

            SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
            sr.sprite = Wall;



            //This draws the other half of the circle
            //Because squaring includes a ±
            GameObject go2 = new GameObject(x + ", " + -y);

            go2.transform.position = new Vector2(x, -y);

            SpriteRenderer sr2 = go2.AddComponent<SpriteRenderer>();
            sr2.sprite = Wall;

            go2.transform.parent = transform;


        }
    }

This is the source, I just modified it for unity.
http://groups.csail.mit.edu/graphics/classes/6.837/F98/Lecture6/circle.html

Also more on the maths of a circle:

The code I sent you has ‘holes’ and that is because if we were to solve for those missing coordinated, we will find that x has to be a rational number (technically this is wrong, but a circle in math does not have any straight edges), and integer data types are not capable of rational numbers. Moreover, the for loop is incremented by 1, therefore skipping any value that will result in the correct placing of the gameobjects.

There may be a better way to do this, however I am approaching from a mathematical perspective. Hopefully someone could fill in the gaps, or provide a more efficient way of generating circles.

1 Like

thank you very much, i’m trying to modify the code to make it draw the circle in a 4 way symmetry(already done it), and also trying to solve the gap part…

kind of did it, the way it works now is, it makes the circle with a 8 way symmetry, so no more of those gaps, and in the end it checks if the game objects will not be on the same tile, by checking if x and z are equal to zero, and if it is equal to zero, it only instantiates the object by 4

known issues:
with certain values either the tiles overlap, or it creates a gap that is less than one tile meaning that the radius of the circle is not symmetric on 8 ways, only on 4.

public class test : MonoBehaviour {

    public int Radius;
    private int x;
    private int z;
    private int oldZ;
    private bool dif2;
    private bool dif1;
    public float waitTime;
    public GameObject wallTile;
    public int size;

    // Use this for initialization
    void Start()
    {
        x = 0;
        z = Radius;
        oldZ = z;
        dif1 = true;
        if (size >= 0)
        {
            StartCoroutine(DrawCircle());
            Debug.Log("size must be greater than 0");
        }
    }

    IEnumerator DrawCircle()
    {
        int r2 = Radius * Radius;


        Instantiate(wallTile, new Vector3(Radius-2, 0, 0), transform.rotation);
        Instantiate(wallTile, new Vector3(-Radius+2, 0, 0), transform.rotation);
        Instantiate(wallTile, new Vector3(0, 0, Radius-2), transform.rotation);
        Instantiate(wallTile, new Vector3(0, 0, -Radius+2), transform.rotation);
     

        while (dif1 == true)
        {


            bool difZ = false;

            x += size;
            z = (int)(Mathf.Sqrt(r2 - x * x) + 0f);
         
            dif1 = z >= x;

            difZ = oldZ - z == size || oldZ - z == 0;

            if (difZ == false)
                {
                    z -= size/2;
                    dif2 = (z - x) >= 0;
                }

            if (dif2 == false && dif1 == false)
            {
                StopCoroutine(DrawCircle());
            }
            if (dif2 == true && dif1 == true && x%2==0) //x%2==0 if it is odd
            {
                Clone();

                yield return new WaitForSeconds(waitTime);
            }
        }
    }

    private void Clone()
    {

     
        transform.position = new Vector3(x, 0, z);

        Debug.Log(z-x);

        Instantiate(wallTile, new Vector3(transform.position.x, 0, transform.position.z), transform.rotation);
        Instantiate(wallTile, new Vector3(transform.position.x, 0, -transform.position.z), transform.rotation);
        Instantiate(wallTile, new Vector3(-transform.position.x, 0, -transform.position.z), transform.rotation);
        Instantiate(wallTile, new Vector3(-transform.position.x, 0, transform.position.z), transform.rotation);
        if ((z - x) != 0)
        {
            Instantiate(wallTile, new Vector3(transform.position.z, 0, transform.position.x), transform.rotation);
            Instantiate(wallTile, new Vector3(transform.position.z, 0, -transform.position.x), transform.rotation);
            Instantiate(wallTile, new Vector3(-transform.position.z, 0, -transform.position.x), transform.rotation);
            Instantiate(wallTile, new Vector3(-transform.position.z, 0, transform.position.x), transform.rotation);
        }
        oldZ = z;
    }
}