【问题标题】:What does this code do exactly at the hardware level or above?该代码在硬件级别或更高级别上究竟做了什么?
【发布时间】:2015-08-26 13:30:45
【问题描述】:

我正在阅读绘制圆的数学知识,它是有道理的,但将其翻译成 C++ 也是荒谬的。使用时似乎缺少一些基础数学或计算机功能 SetPixel(hdc, x, y , COLOREF bg); 在 Win32 API GDI 中。

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

谁能解释一下 这是做什么的,你是怎么想出来的?

//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);
}

【问题讨论】:

  • 我真的不在乎你为了自己的乐趣在代码中放了什么。如果您要在此处发布代码,则无需包含四个字母的脏话。

标签: c++ winapi gdi+


【解决方案1】:

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

【讨论】:

  • 直径是半径的两倍。半径为 120。代码中的x*x+y*y &lt;= radius*radius 表达式用于计算x, y 处像素的距离是否小于或等于圆的半径。它通过使用勾股定理来做到这一点:en.wikipedia.org/wiki/Pythagorean_theorem
  • C++ 方程x*x+y*y &lt;= r*r 可以读作“x 乘以 x 加上 y 乘以 y 小于或等于 r 乘以 r”。
  • @DominicHughes:在 Windows 上它被称为Ellipse。但你没有说那是你想要的。
  • 当然,如果我们能用英语告诉计算机我们想要什么,那就太好了。唯一的问题是我们不知道该怎么做!尽管进行了广泛的研究,但让计算机理解自然语言仍然是一个未解决的问题。
  • 如果你想画一个八角形,你需要在数学上做得更好。与其抱怨电脑让你失望,不如照照镜子,认识到你还有很多东西要学。
猜你喜欢
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 2017-06-11
  • 2012-07-23
  • 2016-09-10
  • 2023-03-15
  • 2012-10-17
  • 2021-06-04
相关资源
最近更新 更多