参考《计算几何算法与实现》–孔令德
节点矢量与控制点分别设置如下,进行绘制:
/*----------------------------------------------------
input :
function: 初始化节点与控制点
-----------------------------------------------------*/
void Repeat_Knot_BSpline::InitPoint()
{
n=6, k=3;
//节点初始化
knot[0]=-3/4.0, knot[1]=-2/4.0, knot[2]=-1/4.0;
knot[3]=0.0, knot[4]=1/4.0, knot[5]=2/4.0;
knot[6]=3/4.0, knot[7]=1.0, knot[8]= 5/4.0;
knot[9]=6/4.0, knot[10]=7/4.0;
//控制点初始化
P[0].x=-250, P[0].y=-100;
P[1].x=-350, P[1].y=0;
P[2].x=-330, P[2].y=75;
P[3].x=-250, P[3].y=45;
P[4].x=-170, P[4].y=75;
P[5].x=-150, P[5].y=0;
P[6].x=-250, P[6].y=-100;
}
void Repeat_Knot_BSpline::InitPoint2(void)
{
n=6, k=3;
//节点初始化
knot[0]=0.0, knot[1]=0.0, knot[2]=0.0;
knot[3]=0.0, knot[4]=1/4.0, knot[5]=2/4.0;
knot[6]=3/4.0, knot[7]=1.0, knot[8]=1.0;
knot[9]=1.0, knot[10]=1.0;
//控制点初始化
P[0].x=0, P[0].y=-100;
P[1].x=-100, P[1].y=0;
P[2].x= -80, P[2].y=75;
P[3].x= 0, P[3].y=45;
P[4].x= 80, P[4].y=75;
P[5].x= 100, P[5].y=0;
P[6].x= 0, P[6].y=-100;
}
void Repeat_Knot_BSpline::InitPoint3(void)
{
n=6, k=3;
//节点初始化
knot[0]=0.0, knot[1]=0.0, knot[2]=0.0;
knot[3]=0.0, knot[4]=1/2.0, knot[5]=1/2.0;
knot[6]=1/2.0, knot[7]=1.0, knot[8]=1.0;
knot[9]=1.0, knot[10]=1.0;
//控制点初始化
P[0].x=250, P[0].y=-100;
P[1].x=150, P[1].y=0;
P[2].x=170, P[2].y=75;
P[3].x=250, P[3].y=45;
P[4].x=330, P[4].y=75;
P[5].x=350, P[5].y=0;
P[6].x=250, P[6].y=-100;
}
完整的类为:
#pragma once
#include"P2.h"
#define ROUND(d) int(d+0.5)//四舍五入宏定义
class Repeat_Knot_BSpline
{
public:
Repeat_Knot_BSpline(void);
Repeat_Knot_BSpline(int theChoice);
~Repeat_Knot_BSpline(void);
public:
//计算基函数
double BasicFunctionValue(double t, int i, int k);
//初始化控制点,节点矢量
void InitPoint();
//绘制样条线
void DrawCurve(CDC*pDC);
//绘制控制点
void DrawControlPoint(CDC*pDC);
private:
double knot[11]; //定义节点矢量数组
int n; //控制点个数减1
int k; //次数
CP2 P[7]; //控制点坐标
public:
void InitPoint2(void);
void InitPoint3(void);
};
#include "StdAfx.h"
#include "Repeat_Knot_BSpline.h"
Repeat_Knot_BSpline::Repeat_Knot_BSpline(void)
{
InitPoint();
}
Repeat_Knot_BSpline::Repeat_Knot_BSpline(int theChoice)
{
switch(theChoice)
{
case 0: InitPoint(); break;
case 1: InitPoint2(); break;
case 2: InitPoint3(); break;
default:InitPoint();
}
}
Repeat_Knot_BSpline::~Repeat_Knot_BSpline(void)
{
}
/*----------------------------------------------------
input : t, i, k分别为参数值,支撑区间左端节点的下标,曲线次数
function: 计算第i个k次B样条基函数值
-----------------------------------------------------*/
double Repeat_Knot_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 Repeat_Knot_BSpline::InitPoint()
{
n=6, k=3;
//节点初始化
knot[0]=-3/4.0, knot[1]=-2/4.0, knot[2]=-1/4.0;
knot[3]=0.0, knot[4]=1/4.0, knot[5]=2/4.0;
knot[6]=3/4.0, knot[7]=1.0, knot[8]= 5/4.0;
knot[9]=6/4.0, knot[10]=7/4.0;
//控制点初始化
P[0].x=-250, P[0].y=-100;
P[1].x=-350, P[1].y=0;
P[2].x=-330, P[2].y=75;
P[3].x=-250, P[3].y=45;
P[4].x=-170, P[4].y=75;
P[5].x=-150, P[5].y=0;
P[6].x=-250, P[6].y=-100;
}
void Repeat_Knot_BSpline::InitPoint2(void)
{
n=6, k=3;
//节点初始化
knot[0]=0.0, knot[1]=0.0, knot[2]=0.0;
knot[3]=0.0, knot[4]=1/4.0, knot[5]=2/4.0;
knot[6]=3/4.0, knot[7]=1.0, knot[8]=1.0;
knot[9]=1.0, knot[10]=1.0;
//控制点初始化
P[0].x=0, P[0].y=-100;
P[1].x=-100, P[1].y=0;
P[2].x= -80, P[2].y=75;
P[3].x= 0, P[3].y=45;
P[4].x= 80, P[4].y=75;
P[5].x= 100, P[5].y=0;
P[6].x= 0, P[6].y=-100;
}
void Repeat_Knot_BSpline::InitPoint3(void)
{
n=6, k=3;
//节点初始化
knot[0]=0.0, knot[1]=0.0, knot[2]=0.0;
knot[3]=0.0, knot[4]=1/2.0, knot[5]=1/2.0;
knot[6]=1/2.0, knot[7]=1.0, knot[8]=1.0;
knot[9]=1.0, knot[10]=1.0;
//控制点初始化
P[0].x=250, P[0].y=-100;
P[1].x=150, P[1].y=0;
P[2].x=170, P[2].y=75;
P[3].x=250, P[3].y=45;
P[4].x=330, P[4].y=75;
P[5].x=350, P[5].y=0;
P[6].x=250, P[6].y=-100;
}
/*----------------------------------------------------
input :
function: 绘制曲线
-----------------------------------------------------*/
void Repeat_Knot_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 Repeat_Knot_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();
}