这段代码在硬件级别或更高级别上做了什么?

时间:2015-06-11 03:07:54

标签: c++ winapi gdi+

我正在阅读数学绘制圆圈并且它是有意义的,但要翻译它也是C ++是荒谬的。看来我使用时缺少一些潜在的数学或计算机功能 Win32 API GDI中的SetPixel(hdc, x, y , COLOREF bg);

然而,我设法找到了绘制圆圈的代码,但除了半径浮点值之外,我真的不明白它在任何级别上的作用 并且*运算符与圆绘图有关。

有人可以请完整解释 这是做什么以及你怎么想出来的?

//THIS CODE:
     float radius = 120.0;
                    for(int y =-radius; y<=radius; y++)
                    for(int x =-radius; x<=radius; x++)
                    if(x*x+y*y <= radius*radius)
                    SetPixel(DCWindowHandle, 200+x, 200+y, red);



//SCROLL DOWN HERE TO SEE IT IN USE:

#include <windows.h>
LRESULT CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT ("Marble Engine");
    HWND WindowHandle;
    MSG Message;
    WNDCLASS WindowClass;
    WindowClass.style = CS_HREDRAW | CS_VREDRAW;
    WindowClass.lpfnWndProc = WndProc;
    WindowClass.cbClsExtra = 0;
    WindowClass.cbWndExtra = 0;
    WindowClass.hInstance = hInstance;
    WindowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WindowClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    WindowClass.lpszMenuName = NULL;
    WindowClass.lpszClassName = szAppName;
    if(!RegisterClass (&WindowClass))
    {
        MessageBox(NULL, TEXT("This Program Requires Nothing!"),
        szAppName, MB_ICONERROR);
        return 0;
    }
    WindowHandle = CreateWindow(szAppName,
    TEXT ("The Shit Program!"),
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    NULL,
    NULL,
    hInstance,
    NULL);
    ShowWindow(WindowHandle, iCmdShow);
    UpdateWindow(WindowHandle);
    while(GetMessage(&Message, NULL, 0, 0))
    {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    return Message.wParam;
}
LRESULT CALLBACK WndProc(HWND WindowHandle, UINT Message, WPARAM wParam, LPARAM lParam)
{
    HDC DCWindowHandle;
    PAINTSTRUCT PaintStruct;
    RECT rect;
    TCHAR * text = TEXT("Hello there this is test!");
    COLORREF red = RGB(255, 0, 0);
    switch(Message)
    {
        case WM_CREATE:
        DCWindowHandle = GetDC(WindowHandle);
        ReleaseDC(WindowHandle, DCWindowHandle);
        return 0;
        case WM_PAINT:
        {
            DCWindowHandle = BeginPaint(WindowHandle, &PaintStruct);
            GetClientRect(WindowHandle, &rect);
            DrawText(DCWindowHandle, TEXT("Bother!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);


            float radius = 120.0;

            for(int y =-radius; y<=radius; y++)
            for(int x =-radius; x<=radius; x++)
            if(x*x+y*y <= radius*radius)
            SetPixel(DCWindowHandle, 200+x, 200+y, red);

            EndPaint( WindowHandle, &PaintStruct);
            return 0;
        }
        case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(WindowHandle, Message, wParam , lParam);
}

1 个答案:

答案 0 :(得分:4)

绘制圆圈的效率较低的方法之一。它所做的是逐个遍历正方形中的所有像素,其边长与圆的直径相同。如果从像素到正方形中心的距离小于或等于圆的半径,则将该像素颜色为红色。方块中的所有其他像素都保持不变。