// 四分法画椭圆子函数
void EllipsePoint(HDC hdc, CP2 p, CP2 pt)
{
SetPixel(hdc, ROUND(p._x + pt._x), ROUND(p._y + pt._y), RGB(0, 0, 255));
SetPixel(hdc, ROUND(p._x + pt._x), -ROUND(p._y + pt._y), RGB(0, 0, 255));
SetPixel(hdc, -ROUND(p._x + pt._x), -ROUND(p._y + pt._y), RGB(0, 0, 255));
SetPixel(hdc, -ROUND(p._x + pt._x), ROUND(p._y + pt._y), RGB(0, 0, 255));
}
while (b * b * (x + 1) < a * a * (y - 0.5)) // 区域1绘制
{
if (d1 < 0)
{
d1 += b * b * (2 * x + 3);
}
else
{
d1 += b * b * (2 * x + 3) + a * a * (-2 * y + 2);
y--;
}
x++;
EllipsePoint(hdc, CP2(x, y), pt);
}
d1 += b * b * (2 * x + 3);
d1 += b * b * (2 * x + 3) + a * a * (-2 * y + 2);
d1 = b * b + a * a * (-b + 0.25);
完成代码
// 07-中点画椭圆算法
// 参考1 https://www.bilibili.com/video/BV1kg41167rV/?spm_id_from=333.788.recommend_more_video.2&vd_source=5b4cd3f84aab3b0261aa6b6791252d89
#ifndef UNICODE
#define UNICODE
#endif
#include <math.h>
#include <Windows.h>
#include <stdio.h>
#include <wchar.h>
#include <WinBase.h>
#define PI 3.1415926 // PI
#define ROUND(d) int(d+0.5) // 四舍五入
#define WINDOW_TEXT L"07-中点画椭圆算法"
// 点
class CP2
{
public:
CP2()
:_x(0)
, _y(0)
{
}
CP2(double x, double y)
:_x(x)
, _y(y)
{
}
virtual ~CP2() {};
public:
double _x;
double _y;
};
// 四分法画椭圆子函数
void EllipsePoint(HDC hdc, CP2 p, CP2 pt)
{
SetPixel(hdc, ROUND(p._x + pt._x), ROUND(p._y + pt._y), RGB(0, 0, 255));
SetPixel(hdc, ROUND(p._x + pt._x), -ROUND(p._y + pt._y), RGB(0, 0, 255));
SetPixel(hdc, -ROUND(p._x + pt._x), -ROUND(p._y + pt._y), RGB(0, 0, 255));
SetPixel(hdc, -ROUND(p._x + pt._x), ROUND(p._y + pt._y), RGB(0, 0, 255));
}
// 中点画椭圆算法 ab为椭圆长短轴
void MidPointEllipose(HDC hdc, CP2 pt,double a, double b)
{
double x, y, d1, d2;
x = 0;
y = b;
d1 = b * b + a * a * (-b + 0.25);
EllipsePoint(hdc, CP2(x, y), pt);
while (b * b * (x + 1) < a * a * (y - 0.5)) // 区域1绘制
{
if (d1 < 0)
{
d1 += b * b * (2 * x + 3);
}
else
{
d1 += b * b * (2 * x + 3) + a * a * (-2 * y + 2);
y--;
}
x++;
EllipsePoint(hdc, CP2(x, y), pt);
}
// 区域2 误差项
d2 = b * b * (x + 0.5) * (x + 0.5) + a * a * (y - 1) * (y - 1) - a * a * b * b;
while (y>0) // 区域2绘制
{
if (d2 < 0)
{
d2 += b * b * (2 * x + 2) + a * a * (-2 * y + 3);
x++;
}
else
{
d2 += a * a * (-2 * y + 3);
}
y--;
EllipsePoint(hdc, CP2(x, y), pt);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rc;
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
SetMapMode(hdc, MM_ANISOTROPIC);
SetWindowExtEx(hdc, rc.right, rc.bottom, NULL);
SetViewportExtEx(hdc, rc.right, -rc.bottom, NULL);
SetViewportOrgEx(hdc, rc.right / 2, rc.bottom / 2, NULL);
CP2 pt = CP2(0, 0);
MidPointEllipose(hdc, pt, 200.0, 200.0);
MidPointEllipose(hdc, pt, 80.0, 200.0);
MidPointEllipose(hdc, pt, 200.0, 80.0);
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"CAG";
WNDCLASS wc = { };
wc.style = CS_HREDRAW | CS_VREDRAW; // 重新绘制整个工作区
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
WINDOW_TEXT, // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}