⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 circle.cpp

📁 画圆的算法 和演示图。希望可以帮助到你
💻 CPP
字号:
#include <windows.h>
#include <stdio.h>
#include <math.h>
typedef struct
{
  HWND *lphWnd;
  DWORD dwExStyle;      // extended window style
  LPCTSTR lpClassName;  // pointer to registered class name
  LPCTSTR lpWindowName; // pointer to window name
  DWORD dwStyle;        // window style
  int x;                // horizontal position of window
  int y;                // vertical position of window
  int nWidth;           // window width
  int nHeight;          // window height
  HMENU hMenu;          // handle to menu, or child-window identifier
}childstru;

HANDLE hInst;
HFONT hFont;
HWND MainhWnd,button1,button2,button3,button4;
HDC mDc;
HBITMAP hbmp;
char *titletext="两种画圆算法",*classname="HeLiang";
BOOL InitApplication(HANDLE hInstance);
BOOL InitInstance(HANDLE hInstance,int nCmdShow);
//------------------------------------------------
childstru CldWin[]=
{
    {
        &button1,
        0,// extended window style
        "Button",// pointer to registered class name
        "Midpoint",// pointer to window name
        WS_VISIBLE| WS_CHILD |BS_PUSHBUTTON,// window style
        0,// horizontal position of window
        0,// vertical position of window
        80,// window width
        21,// window height
        NULL,// handle to menu, or child-window identifier
    },
    {
        &button2,
        0,// extended window style
        "Button",// pointer to registered class name
        "Bresenham",// pointer to window name
        WS_VISIBLE| WS_CHILD |BS_PUSHBUTTON,// window style
        0,// horizontal position of window
        0,// vertical position of window
        80,// window width
        21,// window height
        NULL,// handle to menu, or child-window identifier
    },
    {
        &button3,
        0,// extended window style
        "Button",// pointer to registered class name
        "Clear",// pointer to window name
        WS_VISIBLE| WS_CHILD |BS_PUSHBUTTON,// window style
        0,// horizontal position of window
        0,// vertical position of window
        80,// window width
        21,// window height
        NULL,// handle to menu, or child-window identifier
    },
    {
        &button4,
        0,// extended window style
        "Button",// pointer to registered class name
        "Line",// pointer to window name
        WS_VISIBLE| WS_CHILD |BS_PUSHBUTTON,// window style
        0,// horizontal position of window
        0,// vertical position of window
        80,// window width
        21,// window height
        NULL,// handle to menu, or child-window identifier
    },
    {
        NULL,
        0,// extended window style
        NULL,// pointer to registered class name
        NULL,// pointer to window name
        0,// window style
        0,// horizontal position of window
        0,// vertical position of window
        0,// window width
        0,// window height
        NULL,// handle to menu, or child-window identifier
    }
};

//------------------------------------------------
void __fastcall MidLine(HDC hDc,int x0,int y0,int x1,int y1,COLORREF color)
{
    int a,b,dt1,dt2,d,x,y,ystp=1;
    if(abs(x1-x0)>abs(y1-y0))
    {
        if(x0>x1)
        {
            x=x0;x0=x1;x1=x;
            y=y0;y0=y1;y1=y;
        }
        a=y0-y1;
        b=x1-x0;
        if(a>0){a=-a;ystp=-1;}else ystp=1;
        d=(a<<1)+b;
        dt1=a<<1;
        dt2=(a+b)<<1;
        x=x0;
        y=y0;

        SetPixel(hDc,x,y,color);
        while(x<x1)
        {
            if(d<0)
            {
                x++;y+=ystp;d+=dt2;
            }else
            {
                x++;
                d+=dt1;
            }
            SetPixel(hDc,x,y,color);
        }

    }else
    {
        if(y0>y1)
        {
            x=x0;x0=x1;x1=x;
            y=y0;y0=y1;y1=y;
        }
        a=x0-x1;
        b=y1-y0;
        if(a>0){a=-a;ystp=-1;}else ystp=1;
        d=(a<<1)+b;
        dt1=a<<1;
        dt2=(a+b)<<1;
        x=x0;
        y=y0;

        SetPixel(hDc,x,y,color);
        while(y<y1)
        {
            if(d<0)
            {
                y++;x+=ystp;d+=dt2;
            }else
            {
                y++;
                d+=dt1;
            }
            SetPixel(hDc,x,y,color);
        }
    }
}
//------------------------------------------------
void __fastcall cirpixel(HDC hDc,int x0,int y0,int x,int y,COLORREF color)
{
    SetPixel(hDc,x0+x,y0+y,color);
    SetPixel(hDc,x0+y,y0+x,color);
    SetPixel(hDc,x0-x,y0+y,color);
    SetPixel(hDc,x0+y,y0-x,color);
    SetPixel(hDc,x0+x,y0-y,color);
    SetPixel(hDc,x0-y,y0+x,color);
    SetPixel(hDc,x0-x,y0-y,color);
    SetPixel(hDc,x0-y,y0-x,color);
}
//------------------------------------------------
void __fastcall bpixel(HDC hDc,int x0,int y0,int x,int y,COLORREF color)
{
    SetPixel(hDc,x0+x,y0+y,color);
    SetPixel(hDc,x0-x,y0+y,color);
    SetPixel(hDc,x0+x,y0-y,color);
    SetPixel(hDc,x0-x,y0-y,color);
}
//------------------------------------------------
void __fastcall BresenhamCircle(HDC hDc,int x0,int y0,int r,COLORREF color)
{
    int x,y,dt,dt1,dt2,dr;
    x=0;y=r;dt=2-r-r;
    while(y>=0)
    {
        bpixel(hDc,x0,y0,x,y,color);
        if(dt<0)
        {
            dt1=dt+dt+y+y-1;
            if(dt1<=0)dr=1;
            else dr=2;
        }
        else if(dt>0)
        {
            dt2=dt+dt-x-x-1;
            if(dt2<=0)dr=2;
            else dr=3;
        }else dr=2;
        switch(dr)
        {
            case 1: x++;dt+=x+x+1;
                break;
            case 2: x++;y--;dt+=x+x-y-y-1;
                break;
            case 3: y--;dt+=1-y-y;
                break;
        }
    }
}
//------------------------------------------------
void __fastcall MidpointCircle(HDC hDc,int x,int y,int r,COLORREF color)
{
    int xx,yy,dtx,dty,d;
    xx=0;yy=r;dtx=3,dty=2-r-r;d=1-r;
    cirpixel(hDc,x,y,xx,yy,color);
    while(xx<yy)
    {
        if(d<0)
        {
            d+=dtx;dtx+=2;xx++;
        }else
        {

            d+=dtx+dty;dtx+=2;dty+=2;xx++;yy--;
        }
        cirpixel(hDc,x,y,xx,yy,color);
    }

}

//------------------------------------------------
long PASCAL MainWnProc(HWND hWnd,unsigned int Message,unsigned int wParam,long lParam)
{
    static HANDLE hDc;
    RECT rect;
    PAINTSTRUCT ps;
    WNDPROC prevWinProc;
    int i;
    switch(Message)
    {
         case WM_CREATE:
            SendMessage(hWnd,WM_SETFONT,(WPARAM)hFont,MAKELPARAM(true, 0));
            i=0;
            while(CldWin[i].lphWnd!=NULL)
            {
                *(CldWin[i].lphWnd)=CreateWindowEx(CldWin[i].dwExStyle,CldWin[i].lpClassName,
                    CldWin[i].lpWindowName,CldWin[i].dwStyle,CldWin[i].x,CldWin[i].y,
                    CldWin[i].nWidth,CldWin[i].nHeight,hWnd,CldWin[i].hMenu,hInst,NULL);

                SendMessage(*(CldWin[i].lphWnd),WM_SETFONT,(WPARAM)hFont,MAKELPARAM(true, 0));
                i++;
            }

           return 0;
         case WM_PAINT:
            hDc=BeginPaint(hWnd,&ps);
            EndPaint(hWnd,&ps);
            return 0;
        case WM_SIZE:
            GetClientRect(hWnd,&rect);
            MoveWindow(button1,rect.right-240,rect.bottom-30,70,21,true);
            MoveWindow(button2,rect.right-160,rect.bottom-30,70,21,true);
            MoveWindow(button3,rect.right-80,rect.bottom-30,70,21,true);
            MoveWindow(button4,rect.right-320,rect.bottom-30,70,21,true);
            InvalidateRect(hWnd,NULL,true);
            break;
        case WM_COMMAND:
             if(LOWORD(lParam)==(unsigned short)button1)
             {
                hDc=GetDC(MainhWnd);
                for(int r=200;r>=0;r--)
                MidpointCircle(hDc,260,260,r,RGB(255,0,0xff-r));
                MidpointCircle(hDc,260,260,250,RGB(255,0,250));
                ReleaseDC(MainhWnd,hDc);
             }
             if(LOWORD(lParam)==(unsigned short)button2)
             {
                hDc=GetDC(MainhWnd);
                for(int r=200;r>=0;r--)
                BresenhamCircle(hDc,260,260,r,RGB(255,0xff-r,0));
                BresenhamCircle(hDc,260,260,250,RGB(255,250,0));
                ReleaseDC(MainhWnd,hDc);
             }
             if(LOWORD(lParam)==(unsigned short)button4)
             {
                hDc=GetDC(MainhWnd);
                for(int i=0;i<700;i++)
                    MidLine(hDc,300,200,i,50,RGB(255,0,i*255/700));
                for(int i=0;i<700;i++)
                    MidLine(hDc,300,200,i,350,RGB(i*255/700,255,0));
                for(int i=50;i<350;i++)
                    MidLine(hDc,300,200,0,i,RGB(0,255,i*255/350));
                for(int i=50;i<350;i++)
                    MidLine(hDc,300,200,700,i,RGB(0,i*255/350,255));
                ReleaseDC(MainhWnd,hDc);
             }
             if(LOWORD(lParam)==(unsigned short)button3)
             {
                InvalidateRect(hWnd,NULL,true);
             }
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hWnd,Message,wParam,lParam);
}
//------------------------------------------------

int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
    LOGFONT LogFont;
    MSG msg;
    HDC hDc;
    hFont=GetStockObject(DEFAULT_GUI_FONT);
    if(!hPrevInstance)
         if(!InitApplication(hInstance)) return (FALSE);
    if(!InitInstance(hInstance,nCmdShow)) return(FALSE);

    while(GetMessage(&msg,NULL,NULL,NULL))
    {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
    }
    DeleteObject(hFont);
    return(msg.wParam);
}
//------------------------------------------------

BOOL InitApplication(HANDLE hInstance)
{
    WNDCLASS wc;
    wc.style=NULL;
    wc.lpfnWndProc=MainWnProc;
    wc.cbClsExtra=0;
    wc.cbWndExtra=0;
    wc.hInstance=hInstance;
    wc.hIcon=LoadIcon(hInstance,"MAINICON");
    wc.hCursor=LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground=GetSysColorBrush(COLOR_WINDOW);
    wc.lpszMenuName="";  //Name of menu resource in .RC file
    wc.lpszClassName=classname; //Name used in call to CreateWindow.
    return RegisterClass(&wc);
}
//------------------------------------------------

BOOL InitInstance(HANDLE hInstance,int nCmdShow)
{
    HWND hWnd;
    hInst=hInstance;
    hWnd=CreateWindowEx(WS_EX_APPWINDOW,classname,titletext,WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    MainhWnd=hWnd;
    if(!hWnd)return(FALSE);
    ShowWindow(hWnd,nCmdShow);
    UpdateWindow(hWnd);
    return(TRUE);

}
//------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -