vs使用Bresenham 画圆算法的完整工程
时间: 2024-02-09 20:08:56 浏览: 69
下面是在 Visual Studio 中使用 Bresenham 算法绘制圆形的完整工程:
1. 创建一个空的 Win32 控制台应用程序项目,命名为 `BresenhamCircle`。
2. 将以下代码保存为 `BresenhamCircle.cpp` 文件,并将其添加到项目中。
```c++
#include <iostream>
#include <windows.h>
using namespace std;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const char CLASS_NAME[] = "MyWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
"Bresenham Circle",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
int x0 = ps.rcPaint.right / 2;
int y0 = ps.rcPaint.bottom / 2;
int r = 100;
HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 255, 255));
HGDIOBJ hOldPen = SelectObject(hdc, hPen);
int x = r, y = 0;
int decision = 3 - 2 * r;
while (x >= y)
{
// draw the eight octants
SetPixel(hdc, x0 + x, y0 + y, RGB(255, 255, 255));
SetPixel(hdc, x0 + y, y0 + x, RGB(255, 255, 255));
SetPixel(hdc, x0 - y, y0 + x, RGB(255, 255, 255));
SetPixel(hdc, x0 - x, y0 + y, RGB(255, 255, 255));
SetPixel(hdc, x0 - x, y0 - y, RGB(255, 255, 255));
SetPixel(hdc, x0 - y, y0 - x, RGB(255, 255, 255));
SetPixel(hdc, x0 + y, y0 - x, RGB(255, 255, 255));
SetPixel(hdc, x0 + x, y0 - y, RGB(255, 255, 255));
y++;
if (decision <= 0)
{
decision = decision + 4 * y + 6;
}
else
{
x--;
decision = decision + 4 * (y - x) + 10;
}
}
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
```
3. 打开项目属性页,选择“配置属性”->“常规”,将“字符集”设置为“使用 Unicode 字符集”。
4. 在“链接器”->“系统”中,将“子系统”设置为“Windows (/SUBSYSTEM:WINDOWS)”。
5. 点击“确定”按钮保存项目属性设置。
6. 编译和运行程序,应该会看到一个白色的圆形在窗口中心。
阅读全文