中点椭圆算法的实现

        中点椭圆算法和中点画圆算法的原理挺相似的。决策参数的计算过程也相近。本人在codeblocks下用win32 api写了简单的演示程序,但是发现了一些问题,稍后提出。

#include <windows.h>
#include <math.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void EllipsePlotPoints (HDC hdc, int xc, int yc, int x, int y);
void EllipseMidpoint (HDC hdc, int xc, int yc, int rx, int ry);

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static POINT ptBeg, ptEnd;
    static int xc, yc, rx, ry;
    PAINTSTRUCT ps;
    HDC hdc;
    switch (message)                  /* handle the messages */
    {
        case WM_LBUTTONDOWN:
            ptBeg.x = LOWORD (lParam);
            ptBeg.y = HIWORD (lParam);
            xc = yc = rx = ry = 0;
            ptEnd = ptBeg;
            break;

        case WM_MOUSEMOVE:
            if (wParam & MK_LBUTTON)
            {
                hdc = GetDC (hwnd);
                SelectObject (hdc, GetStockObject(BLACK_PEN));
                SetROP2 (hdc, R2_NOTXORPEN);

                EllipseMidpoint (hdc, xc, yc, rx, ry);

                ptEnd.x = LOWORD (lParam);
                ptEnd.y = HIWORD (lParam);

                xc = 0.5 *(ptBeg.x + ptEnd.x);
                yc = 0.5*(ptBeg.y + ptEnd.y);
                rx = fabs (ptEnd.x - ptBeg.x);
                ry = fabs (ptEnd.y -ptBeg.y);

                EllipseMidpoint (hdc, xc, yc, rx, ry);

                ReleaseDC (hwnd, hdc);
            }
            break;

        case WM_LBUTTONUP:
            InvalidateRect (hwnd, NULL, TRUE);
            break;

        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            EllipseMidpoint (hdc, xc, yc, rx, ry);
            EndPaint (hwnd, &ps);
            break;

        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;

        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

void EllipsePlotPoints (HDC hdc, int xc, int yc, int x, int y)
{
    SetPixel (hdc, xc + x, yc + y, 0);
    SetPixel (hdc, xc -x, yc + y, 0);
    SetPixel (hdc, xc + x, yc - y, 0);
    SetPixel (hdc, xc - x, yc - y, 0);
}

inline int Rounding (const float a) {return int (a + 0.5);}

void EllipseMidpoint (HDC hdc, int xc, int yc, int rx, int ry)
{
    int rx2 = rx*rx, ry2 = ry*ry;
    int twoRx2 = 2*rx2, twoRy2 = 2*ry2;
    int p;
    int x = 0, y = ry;
    int px = 0, py = twoRx2*y;

    EllipsePlotPoints (hdc, xc, yc, x, y);

    p = Rounding (ry2 - rx2*ry + 0.25*rx2);
    while (px < py)
    {
        x ++;
        px += twoRy2;
        if (p < 0)
        {
            p += px + ry2;
        }
        else
        {
            y --;
            py -= twoRx2;
            p += px - py + ry2;
        }
        EllipsePlotPoints (hdc, xc, yc, x, y);
    }

    p = Rounding ( ry2*(x + 0.5)*(x + 0.5) + rx2*(y - 1)*(y - 1) - rx2*ry2);
    while (y > 0)
    {
        y --;
        py -= twoRx2;
        if (p > 0)
        {
            p += rx2 - py;
        }
        else
        {
            x ++;
            px += twoRy2;
            p += px - py + rx2;
        }
        EllipsePlotPoints(hdc, xc, yc, x, y);
    }
}

       解释一下,那个Rounding函数其实就是用来四舍五入的。一般来说,画个小椭圆是没啥问题的。注意,是“小椭圆”!画”大椭圆“就真的遇到问题了。下面两张图,有对比有真相。

中点椭圆算法的实现_第1张图片


中点椭圆算法的实现_第2张图片

    真相出来了。为啥椭圆离心率比较大的时候,椭圆横轴的两个顶点处就变成尖嘴了呢?

你可能感兴趣的:(中点椭圆算法的实现)