why does my circle anti aliasing algorithm in C++ give asymmetrical results?

  antialiasing, c++, opengl, pixel

Some background:

So my plan is to create a stippling algorithm in C++ and I basically just plan on storing a whole bunch of data for each radius of a circle to write onto a texture map in OpenGL I’m not sure if this is the right thing to do but I feel like it

would be quicker than the computer dynamically calculating the radius for each circle especially if lots of circles are the same size, my plan is to create a function that just writes a whole text document full of radiuses up to a certain size and this data will be stored bitwise inside an array of long’s std::array <long> bit = {0x21, 0x0A ect... } so that I can encode 4X4 arrays of values with 2 bits assigned to the antialiasing value of each pixel however to create this database of ant-aliased circles I need to write a function that I keep getting wrong;

The actual question:

So this may seem lazy but I can promise I have tried everything to wrap my head around what I am getting wrong here basically i have written this code to anti=alias by dividing up the pixels into sub pixels however it seems to be returning values greater than 1 which shouldn’t be possible as i have divided each pixel into 100 pixels of size 0.01

float CircleConst::PixelAA(int I, int J)
{
    float aaValue = 0;

    for (float i = (float) I; i < I + 1; i += 0.1f)
    {
        for (float j = (float) J; j < J + 1; j += 0.1f)
        {
            if ((pow((i - center), 2) + pow((j - center), 2) < pow(rad, 2)))
                aaValue += 0.01f;
        }
    }
    return aaValue;
}

also here is the code that writes the actual circle

CircleConst::CircleConst(float Rad)
{
    rad = Rad;
    dataSize = (unsigned int) ceil(2 * rad);


    center = (float) dataSize/2;

    arrData.reserve((int) pow(dataSize, 2));

    for (int i = 0; i < dataSize; i++)
    {
        for (int j = 0; j < dataSize; j++)
        {
            if ( CircleBounds(i, j, rad-1) )
                arrData.push_back(1);
            else if (!CircleBounds(i, j, rad - 1) && CircleBounds(i, j, rad + 1))
            {
                arrData.push_back(PixelAA(i,j));
            }
            else
                arrData.push_back(0);
        }
    }
}

so I noticed without the antialiasing that the way the circle is written is shifted over by one line, but this could be fixed by changing the value of the centre of the circle todataSize/2 - 0.5f but this causes problems later on when the circle is asymmetrical with the antialiasing, here is an example of radius 3.5

0.4 1.0 1.1 1.1 1.1 0.4 0.0
1.0 1.0 1.0 1.0 1.0 1.1 0.2
1.1 1.0 1.0 1.0 1.0 1.0 0.5
1.1 1.0 1.0 1.0 1.0 1.0 0.5
1.1 1.0 1.0 1.0 1.0 1.0 0.2
0.4 1.1 1.0 1.0 1.0 0.5 0.0
0.0 0.2 0.5 0.5 0.2 0.0 0.0

as you can see some of the values are over 1.0 which should not be possible, I’m sure there is an obvious answer to why this is but I’m completely missing it.

apologies for the confusing question, I’m very new to coding so I’m very bad at asking for the right thing.

Source: Windows Questions C++

LEAVE A COMMENT