如何绘制此圆圈而不是布雷森纳姆的圆圈算法

问题描述

int main()
{
    const auto console = ::GetConsoleWindow();
    const auto context = ::GetDC(console);
    constexpr auto red = RGB(255,0);
    constexpr auto yellow = RGB(255,255,0);

    RECT rectClient,rectwindow;
    GetClientRect(console,&rectClient);
    GetwindowRect(console,&rectwindow);
    int posx,posy;
    posx = GetSystemMetrics(SM_CXSCREEN) / 2 - (rectwindow.right - rectwindow.left) / 2;
    posy = GetSystemMetrics(SM_CYSCREEN) / 2 - (rectwindow.bottom - rectwindow.top) / 2;

    const int radius = 150;
        for (int y = -radius; y <= radius; y++)
            for (int x = -radius; x <= radius; x++)
                if (x * x + y * y <= radius * radius)
                    SetPixel(context,posx + x,posy + y,red);
}

它给了我这个结果img

看起来不错,但我在侧面(上,下,右,左)看到了这个奇怪的像素 img

这就是我想要的(我在顶部添加了一些像素,因此看起来更好) enter image description here

解决方法

您的“我想要的”看起来没有锯齿。因此请画出抗锯齿。

如果不满足原始条件,但满足x*x + y*y <= (radius+1)*(radius+1),则需要部分阴影的像素。

进行抗锯齿的另一种方法是,不是测试每个像素的中心,而是测试四个角(x \ + 0.5,y \ +0.5)。如果圆内的角大于零但小于四个,则需要部分阴影的像素。