06-中点画圆算法

参考1 

案例6:中点画圆算法

// 八分法画圆子函数
void CirclePoint(HDC hdc, CP2 p, CP2 pt)
{
    SetPixel(hdc,  ROUND(p._y + pt._y),  ROUND(p._x + pt._x), RGB( 0, 0,255)); // 1 象限
    SetPixel(hdc,  ROUND(p._x + pt._x),  ROUND(p._y + pt._y), RGB( 0, 0,255)); // 2 象限
    SetPixel(hdc,  ROUND(p._y + pt._y), -ROUND(p._x + pt._x), RGB( 0, 0,255)); // 3 象限
    SetPixel(hdc,  ROUND(p._x + pt._x), -ROUND(p._y + pt._y), RGB( 0, 0,255)); // 4 象限
    SetPixel(hdc, -ROUND(p._x + pt._x), -ROUND(p._y + pt._y), RGB( 0, 0,255)); // 5 象限
    SetPixel(hdc, -ROUND(p._y + pt._y), -ROUND(p._x + pt._x), RGB( 0, 0,255)); // 6 象限
    SetPixel(hdc, -ROUND(p._y + pt._y),  ROUND(p._x + pt._x), RGB( 0, 0,255)); // 7 象限
    SetPixel(hdc, -ROUND(p._x + pt._x),  ROUND(p._y + pt._y), RGB( 0, 0,255)); // 8 象限
}

 

// 中点画圆算法
void MidPointCircle(HDC hdc,CP2 pt,int r)
{
    int e = 1 - r;
    int x = 0, y = r;
    for (; x <= y; x++)
    {
        CirclePoint(hdc, CP2(x,y), pt);
        if (e<0)
        {
            e += 2 * x + 3;
        }
        else 
        {
            e += 2 * (x - y) + 5;
            y--;
        }
    }
}

完成代码

// 06-中点画圆算法
// 参考1https://www.bilibili.com/video/BV1jY4y1A716/?spm_id_from=333.788.recommend_more_video.0&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)  // 四舍五入
// 点
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 CirclePoint(HDC hdc, CP2 p, CP2 pt)
{
    SetPixel(hdc,  ROUND(p._y + pt._y),  ROUND(p._x + pt._x), RGB( 0, 0,255)); // 1 象限
    SetPixel(hdc,  ROUND(p._x + pt._x),  ROUND(p._y + pt._y), RGB( 0, 0,255)); // 2 象限
    SetPixel(hdc,  ROUND(p._y + pt._y), -ROUND(p._x + pt._x), RGB( 0, 0,255)); // 3 象限
    SetPixel(hdc,  ROUND(p._x + pt._x), -ROUND(p._y + pt._y), RGB( 0, 0,255)); // 4 象限
    SetPixel(hdc, -ROUND(p._x + pt._x), -ROUND(p._y + pt._y), RGB( 0, 0,255)); // 5 象限
    SetPixel(hdc, -ROUND(p._y + pt._y), -ROUND(p._x + pt._x), RGB( 0, 0,255)); // 6 象限
    SetPixel(hdc, -ROUND(p._y + pt._y),  ROUND(p._x + pt._x), RGB( 0, 0,255)); // 7 象限
    SetPixel(hdc, -ROUND(p._x + pt._x),  ROUND(p._y + pt._y), RGB( 0, 0,255)); // 8 象限
}

// 中点画圆算法
void MidPointCircle(HDC hdc,CP2 pt,int r)
{
    int e = 1 - r;
    int x = 0, y = r;
    for (; x <= y; x++)
    {
        CirclePoint(hdc, CP2(x,y), pt);
        if (e<0)
        {
            e += 2 * x + 3;
        }
        else 
        {
            e += 2 * (x - y) + 5;
            y--;
        }
    }
}


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

        // 大圆
        int r = 300;
        CP2 pt = CP2(0, 0);
        MidPointCircle(hdc, pt,r);
        
        // 小圆
        int r2 = 150;
        for (int alpha = 0; alpha < 360; alpha+=30)
        {
            double x = r2 * cos(alpha*PI/180);
            double y = r2 * sin(alpha*PI/180);

            MidPointCircle(hdc, CP2(x,y), r2);
        }

        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"06-中点画圆算法";
    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
        L"06-中点画圆算法",               // 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值