Trashtalk
Trashtalk

Reputation: 341

Mid-Point Circle Drawing Rounding Problem

I'm trying to implement the Mid-point drawing algorithm in C# to scan from a point outward. I have the following code:

private bool[,] squares;

public Bitmap(int br, int h)
{   
    squares = new bool[br, h];
    MakeCircle(9, 10, 10);
    MakeCircle(8, 10, 10);
    MakeCircle(7, 10, 10);
    MakeCircle(6, 10, 10);
    MakeCircle(5, 10, 10);
    MakeCircle(4, 10, 10);
    MakeCircle(3, 10, 10);
    MakeCircle(2, 10, 10);
    MakeCircle(1, 10, 10);
    MakeCircle(0, 10, 10);
}

public void SelectSymmetry(int x, int y, int x0, int y0)
{
    TryPlace(x + x0, y + y0);
    TryPlace(-x + x0, y + y0);
    TryPlace(x + x0, -y + y0);
    TryPlace(-x + x0, -y + y0);
    TryPlace(y + x0, x + y0);
    TryPlace(-y + x0, x + y0);
    TryPlace(y + x0, -x + y0);
    TryPlace(-y + x0, -x + y0);
}

public void TryPlace(int x, int y)
{
    if (x >= 0 && x < vakjes.GetLength(0) && y >= 0 && y < vakjes.GetLength(1))
    {
        squares[x, y] = true;
    }
}

public void MakeCircle(int radius, int x0, int y0)
{
    int x = 0;
    int y = radius;
    double d = 5 / 4.0 - radius;
    SelectSymmetry(x, y, x0, y0);
    while (x < y)
    {
        x++;
        if (d < 0)
        {
            d += 2 * x + 1;
        }
        else
        {
            y--;
            d += 2 * (x - y) + 1;
        }
        SelectSymmetry(x, y, x0, y0);
    }
}

This creates the following image: Filled in circle with missing pixels And that's the problem. As far as I can tell no 2 circles occupy the same pixel, but there are pixels that aren't filled by any circle. Is there a way to alter my code so these pixels belong to a circle? Any help would be much appreciated.

P.S. Sorry for the code dump, but I felt this was the most direct way of showing my problem.

Upvotes: 0

Views: 150

Answers (1)

Matt Timmermans
Matt Timmermans

Reputation: 59154

Just don't try to make a disk out of circles. Instead of calling MakeCircle 10 times, call this once:

public void MakeDisk(int radius, int x0, int y0)
{
    int x = 0;
    int y = radius;
    double d = 5 / 4.0 - radius;
    for (int ty=y; ty>=x; --ty) {
        SelectSymmetry(x, ty, x0, y0);
    }
    while (x < y)
    {
        x++;
        if (d < 0)
        {
            d += 2 * x + 1;
        }
        else
        {
            y--;
            d += 2 * (x - y) + 1;
        }
        for (int ty=y; ty>=x; --ty) {
            SelectSymmetry(x, ty, x0, y0);
        }
    }
}

Upvotes: 1

Related Questions