参考《计算几何算法与实现》–孔令德
节点矢量与控制点设置为:
//节点初始化
knot[0]=-3/3.0, knot[1]=-2/3.0, knot[2]=-1/3.0;
knot[3]=0.0, knot[4]=1/3.0, knot[5]=2/3.0;
knot[6]=1.0, knot[7]= 4/3.0, knot[8]= 5/3.0;
knot[9]=6/3.0;
//控制点初始化
P[0].x=-460, P[0].y=-49;
P[1].x=-355, P[1].y=204;
P[2].x= -63, P[2].y=241;
P[3].x= 66, P[3].y=-117;
P[4].x= 264, P[4].y=-101;
P[5].x= 400, P[5].y=208;
在MFC单文档类中添加类Cubic_BSpline,用来绘制该曲线,实现如下:
#pragma once
#include"P2.h"
class Cubic_BSpline
{
public:
Cubic_BSpline(void);
~Cubic_BSpline(void);
public:
//计算基函数
double BasicFunctionValue(double t, int i, int k);
//初始化控制点,节点矢量
void InitPoint();
//绘制样条线
void DrawCurve(CDC*pDC);
//绘制控制点
void DrawControlPoint(CDC*pDC);
private:
double knot[10]; //定义节点矢量数组
int n; //控制点个数减1
int k; //次数
CP2 P[6]; //控制点坐标
};
#include "StdAfx.h"
#include "Cubic_BSpline.h"
#define ROUND(d) int(d+0.5)//四舍五入宏定义
Cubic_BSpline::Cubic_BSpline(void)
{
InitPoint();
}
Cubic_BSpline::~Cubic_BSpline(void)
{
}
/*----------------------------------------------------
input : t, i, k分别为参数值,支撑区间左端节点的下标,曲线次数
function: 计算第i个k次B样条基函数值
-----------------------------------------------------*/
double Cubic_BSpline::BasicFunctionValue(double t, int i, int k)
{
//次数为0时的基函数值
if(k==0)
{
if(t>=knot[i]&&t<knot[i+1])
return 1.0;
else
return 0.0;
}
//次数不为0时的基函数计算
double value1, value2, value;
if(k>0)
{
//节点矢量位于定义域之外
if(t<knot[i]||t>knot[i+k+1])
return 0.0;
//节点矢量位于定义域之外
else
{
double coff1, coff2; //系数
double denominator=0.0; //分母
//递推公式第一项
denominator=knot[i+k]-knot[i];
if(denominator==0.0)
coff1=0.0; //重节点分母为0
else
coff1=(t-knot[i])/denominator;
//递推公式第二项
denominator=knot[i+k+1]-knot[i+1];
if(denominator==0.0)
coff2=0.0; //重节点分母为0
else
coff2=(knot[i+k+1]-t)/denominator;
//递推公式第一、二项的值
value1=coff1*BasicFunctionValue(t,i,k-1);
value2=coff2*BasicFunctionValue(t,i+1,k-1);
value=value1+value2;
}
}
return value;
}
/*----------------------------------------------------
input :
function: 初始化节点与控制点
-----------------------------------------------------*/
void Cubic_BSpline::InitPoint()
{
n=5, k=3;
//节点初始化
knot[0]=-3/3.0, knot[1]=-2/3.0, knot[2]=-1/3.0;
knot[3]=0.0, knot[4]=1/3.0, knot[5]=2/3.0;
knot[6]=1.0, knot[7]= 4/3.0, knot[8]= 5/3.0;
knot[9]=6/3.0;
//控制点初始化
P[0].x=-460, P[0].y=-49;
P[1].x=-355, P[1].y=204;
P[2].x= -63, P[2].y=241;
P[3].x= 66, P[3].y=-117;
P[4].x= 264, P[4].y=-101;
P[5].x= 400, P[5].y=208;
}
/*----------------------------------------------------
input :
function: 绘制曲线
-----------------------------------------------------*/
void Cubic_BSpline::DrawCurve(CDC*pDC)
{
//绘制控制点
DrawControlPoint(pDC);
//创建并选取画笔
CPen NewPen, *pOldPen;
NewPen.CreatePen(PS_SOLID,2,RGB(0,0,255));
pOldPen=pDC->SelectObject(&NewPen);
//等距取参数点计算值
double tStep=0.01;
for(double t=0.0; t<=1.0; t+=tStep)
{
CP2 p(0,0);//离散点
for(int i=0; i<=n; i++)
{
double BValue=BasicFunctionValue(t, i, k);
p+=P[i]*BValue;
}
if(t==0)
pDC->MoveTo(ROUND(p.x), ROUND(p.y));
else
pDC->LineTo(ROUND(p.x), ROUND(p.y));
}
//还原画笔,删除创建的画笔
pDC->SelectObject(pOldPen);
NewPen.DeleteObject();
}
/*----------------------------------------------------
input :
function: 绘制控制点
-----------------------------------------------------*/
void Cubic_BSpline::DrawControlPoint(CDC*pDC)
{
//初始化画笔
CPen NewPen,*pOldPen;
NewPen.CreatePen(PS_SOLID,3,RGB(0,0,0));
pOldPen=pDC->SelectObject(&NewPen);
//初始化画刷
CBrush NewBrush,*pOldBrush;
NewBrush.CreateSolidBrush(RGB(0,0,0));
pOldBrush=pDC->SelectObject(&NewBrush);
//加粗控制点
for(int i=0;i<=n;i++)
{
if(0==i)
{
pDC->MoveTo(ROUND(P[i].x),ROUND(P[i].y));
pDC->Ellipse(ROUND(P[i].x)-5,ROUND(P[i].y)-5,ROUND(P[i].x)+5,ROUND(P[i].y)+5);
}
else
{
pDC->LineTo(ROUND(P[i].x),ROUND(P[i].y));
pDC->Ellipse(ROUND(P[i].x)-5,ROUND(P[i].y)-5,ROUND(P[i].x)+5,ROUND(P[i].y)+5);
}
}
//还原画笔画刷
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
NewPen.DeleteObject();
NewBrush.DeleteObject();
}