De Casteljau算法-贝塞尔

/*

 de Casteljau算法的具体描述:

 Input: array P[0:n]of n+1 control points and a real number u in [0,1]

 Output: point oncurve, C(u)

 Working: pointarray Q[0:n]

 for i=0 to n do

 Q[i] = P[i]

 for k=1 to ndo          // the times of recursion isn!

 for i=0 to n-k do

 Q[i] = (1-u)Q[i] +uQ[i+1]

 return Q[0];

 */

// DrawBezierLine.cpp : 定义应用程序的入口点。



#include <assert.h>

#define MAX_LOADSTRING 100

 

int ctpt_max=100;

int indexct=0;  //指向待写入的控制点


POINT * ctptArray=NULL;  //控制多边形数组

int ndegree=4; //次数

 void addCtPoint(POINTpt)

 {

     if (ctptArray==NULL)

     {

         ctptArray=(POINT *)malloc(ctpt_max*sizeof(POINT));

     }

     else

     {

         if (indexct>=ctpt_max)

         {

             ctpt_max=2*ctpt_max;

             realloc(ctptArray,ctpt_max);

         }

     }

     ctptArray[indexct++]=pt;

 }



 /**

 *@funcname:de_Casteljau_bezier

 *@Access: public 

 *@brief:

 *@param:POINT * ctArray :控制曲线数组起点指针

 *@param:int n:贝塞尔次数

 *@param:double u:细分的u值

 *@param:POINT * pt:输出的线上点

 *@return :void

 */

 void de_Casteljau_bezier(POINT*ctArray, intn,doubleu,POINT*pt)

 {

     int i=0;

     int k=0;

     POINT* pV=(POINT*)malloc(n*sizeof(POINT));

     if (pV==NULL)

     {

         assert("申请内存出错"&&0);

     }

     for (i=0;i<=n;i++)

     {

         pV[i]=ctArray[i];

     }

 

     for (k=1;k<=n;k++)

     {

         for ( i = 0; i <=n-k; i++)

         {

             pV[i].x=(long)((1-u)*pV[i].x+u*pV[i+1].x);

             pV[i].y=(long)((1-u)*pV[i].y+u*pV[i+1].y);

         }

     }

 

     *pt=pV[0];

 }

// 全局变量:

HINSTANCE hInst;                                //当前实例

TCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本

TCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

 

// 此代码模块中包含的函数的前向声明:

ATOM                MyRegisterClass(HINSTANCE hInstance);

BOOL                InitInstance(HINSTANCE, int);

LRESULT CALLBACK    WndProc(HWND,UINT, WPARAM,LPARAM);

INT_PTR CALLBACK    About(HWND,UINT, WPARAM,LPARAM);

 

int APIENTRY _tWinMain(_In_ HINSTANCEhInstance,

                     _In_opt_HINSTANCE hPrevInstance,

                     _In_LPTSTR    lpCmdLine,

                     _In_int       nCmdShow)

{

    UNREFERENCED_PARAMETER(hPrevInstance);

    UNREFERENCED_PARAMETER(lpCmdLine);

 

     //TODO: 在此放置代码。

    MSG msg;

    HACCEL hAccelTable;

 

    //初始化全局字符串

    LoadString(hInstance,IDS_APP_TITLE, szTitle,MAX_LOADSTRING);

    LoadString(hInstance,IDC_DRAWBEZIERLINE, szWindowClass, MAX_LOADSTRING);

    MyRegisterClass(hInstance);

 

    //执行应用程序初始化:

    if(!InitInstance (hInstance,nCmdShow))

    {

        returnFALSE;

    }

 

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DRAWBEZIERLINE));

 

    //主消息循环:

    while(GetMessage(&msg,NULL, 0,0))

    {

        if(!TranslateAccelerator(msg.hwnd,hAccelTable, &msg))

        {

            TranslateMessage(&msg);

            DispatchMessage(&msg);

        }

    }

 

    return(int) msg.wParam;

}

 

 

 

//

// 函数:MyRegisterClass()

//

// 目的:注册窗口类。

//

ATOM MyRegisterClass(HINSTANCEhInstance)

{

    WNDCLASSEX wcex;

 

    wcex.cbSize= sizeof(WNDCLASSEX);

 

    wcex.style          = CS_HREDRAW| CS_VREDRAW;

    wcex.lpfnWndProc    = WndProc;

    wcex.cbClsExtra     = 0;

    wcex.cbWndExtra     = 0;

    wcex.hInstance      = hInstance;

    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DRAWBEZIERLINE));

    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);

    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);

    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_DRAWBEZIERLINE);

    wcex.lpszClassName  = szWindowClass;

    wcex.hIconSm        = LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_SMALL));

 

    returnRegisterClassEx(&wcex);

}

 

//

//  函数:InitInstance(HINSTANCE, int)

//

//  目的:保存实例句柄并创建主窗口

//

//  注释:

//

//        在此函数中,我们在全局变量中保存实例句柄并

//        创建和显示主程序窗口。

//

BOOL InitInstance(HINSTANCEhInstance, intnCmdShow)

{

  HWND hWnd;

 

  hInst = hInstance;// 将实例句柄存储在全局变量中

 

  hWnd = CreateWindow(szWindowClass, szTitle,WS_OVERLAPPEDWINDOW,

     CW_USEDEFAULT, 0,CW_USEDEFAULT, 0,NULL, NULL,hInstance, NULL);

 

  if (!hWnd)

  {

     return FALSE;

  }

 

  ShowWindow(hWnd,nCmdShow);

  UpdateWindow(hWnd);

 

  return TRUE;

}

void  OnLButtonDown(UINT nFlags,POINT point)

{

    addCtPoint(point);

}

//

// 函数:WndProc(HWND, UINT, WPARAM, LPARAM)

//

// 目的:处理主窗口的消息。

//

// WM_COMMAND   - 处理应用程序菜单

// WM_PAINT - 绘制主窗口

// WM_DESTROY   - 发送退出消息并返回

//

//

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)

{

    intwmId, wmEvent;

    PAINTSTRUCT ps;

    HDC hdc;

    POINT pt;

    inti=0;

    HPEN hpen;

    POINT ptOnline;

 

    switch(message)

    {

    caseWM_COMMAND:

        wmId    = LOWORD(wParam);

        wmEvent = HIWORD(wParam);

        //分析菜单选择:

        switch(wmId)

        {

        caseIDM_ABOUT:

            DialogBox(hInst,MAKEINTRESOURCE(IDD_ABOUTBOX),hWnd, About);

            break;

        caseIDM_EXIT:

            DestroyWindow(hWnd);

            break;

        default:

            returnDefWindowProc(hWnd,message, wParam,lParam);

        }

        break;

    caseWM_PAINT:

        if(indexct<2)

        {

            break;

        }

        hdc = BeginPaint(hWnd, &ps);

        //TODO: 在此添加任意绘图代码...

        hpen=CreatePen(PS_SOLID,0.5,RGB(255,0,0));

        SelectObject(hdc,hpen);

        for(i=0;i<indexct-1;i++)

        {

            ::MoveToEx(hdc,ctptArray[i].x,ctptArray[i].y,NULL);

            ::LineTo(hdc,ctptArray[i+1].x,ctptArray[i+1].y);

        }

        if(indexct>ndegree)

        {

            for(i=0;i<indexct-ndegree;i+=ndegree)

            {

                for(double t=0.0;t<=1.0;)

                {

 

                    de_Casteljau_bezier(ctptArray+i,ndegree,t,&ptOnline);

                    SetPixel(hdc,ptOnline.x,ptOnline.y,RGB(0,255,0));

                    t+=0.001;

                }

 

            }

        }

 

 

 

        EndPaint(hWnd,&ps);

       

        break;

    caseWM_LBUTTONDOWN:

        pt.x= GET_X_LPARAM( lParam);

        pt.y= GET_Y_LPARAM( lParam);

        OnLButtonDown( wParam,pt );

        ::InvalidateRect(hWnd,NULL,TRUE);

        break;

    caseWM_RBUTTONDOWN:

 

        indexct=0;

        ::InvalidateRect(hWnd,NULL,TRUE);

        break;

    caseWM_DESTROY:

        PostQuitMessage(0);

        break;

    default:

        returnDefWindowProc(hWnd,message, wParam,lParam);

    }

    return0;

}

 

// “关于”框的消息处理程序。

INT_PTR CALLBACK About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)

{

    UNREFERENCED_PARAMETER(lParam);

    switch(message)

    {

    caseWM_INITDIALOG:

        return(INT_PTR)TRUE;

 

    caseWM_COMMAND:

        if(LOWORD(wParam)== IDOK || LOWORD(wParam) == IDCANCEL)

        {

            EndDialog(hDlg,LOWORD(wParam));

            return(INT_PTR)TRUE;

        }

        break;

    }

    return(INT_PTR)FALSE;

}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值