资源说明:C+ + Bezier 曲线类的构造X
中图法分类号: TP302. 4
Bezier 曲线作为一种特殊的参数多项式曲线, 一经问世, 就曾受到CAGD 学术界的广泛重
视. 尽管如今在CAD 领域有许多种不同的自由型曲线和曲面的构造方法, 但使用Bezier 曲线
仍不失为一种重要的备选方案. 例如国内外多种矢量字库的构建, 仍然广泛使用Bezier 曲线技
术.
Bezier 曲线的实现方法传统上主要求助于de Casteljau 算法. 但随着计算机硬件技术的不
断进步, 计算机的处理速度越来越快, 算法的高效尽管仍很重要, 但代码的易于维护性和可重
用性即显得日见重要. 本文利用C+ + 面向对象的特性, 将Bezier 曲线的定义和生成建立在矩
阵运算类的基础上, 从而使描述和生成Bezier 曲线的代码变得简单明了, 且有着很好的可扩展
性.
1 Bezier 曲线的矩阵表示
记B n= [ Bn0
( t ) , Bnn
( t ) ] , 其中B n
i ( t ) = Ci
nt i ( 1- t ) n- i , t I [ 0, 1] .
Tn = [ 1 t , t
n
] , Vn=
b0
b
1
s
bn
1 , Mn =
m00 m01 , m0n
m10 m11 , m1n
, , , ,
mn 0 mn1 , mnn
其中mij = ( - 1) ij
* Ci
n
* Cj
i , 且当j > i 时, mij = 0, 则以b0, b 1, ,, bn 为控制顶点的Bezier 曲线
可以表为P( t ) = Tn
* Mn
* Vn .
2 几个通用模板的定义
为了能够方便地使用计算机来处理上述简便的Bezier 矩阵表达式, 从而大大简化计算机
图形软件的开发, 显然我们首先必须能够方便地使用计算机来处理矩阵和向量等对象, 为此目
的我们利用C+ + 中的最新特征引入了如下向量模板和矩阵模板的概念.
第21 卷 第2 期 江西师范大学学报( 自然科学版) Vol. 21 No. 2
1997 年5 月 JOURNAL OF JIANGXI NORMAL UNIVERSITY May 1997
X 收稿日期: 1997- 01- 20
2. 1 向量模板的定义
template 3class T4
class VectorTemplate {
private:
int numElement s;
T * element s;
protected:
public:
VectorTemplate( void) ;
VectorTemlate( int) ;
VectorTemplate( VectorTemplate3T4 &vSrc;) ;
-VectorTemplate( void) ;
T & operator[ ] ( unsigned int index ) { return elements [ index] ; } ;
int size( void) { return numElement s; } ;
void resize ( int sizeIn) ;
VectorTemplate3T4& operator= ( VectorTemplate3T4 & ) ;
VectorTemplate3T4& operator= ( T) ;
float leng th( void) ;
VectorTemplate3T4 &normalize;( void) ;
friend T operator * ( VectorTemplate3T4 & v1, VectorT emplate3T4 & v2) ;
friend VectorTemplate 3T 4 operator + ( VectorTemplate3T 4 & v1, VectorTemplate3T 4
&v 2) ;
friend VectorTemplate3T4 operator-( VectorT emplate3T4 & v1, VectorTemplate3T4 & v2) ;
friend VectorTemplate3T4 operator * ( VectorTemplate3T4 & v1, T scalar) ;
friend VectorTemplate3T4 operator * ( T scalar, VectorTemplate3T4 & v1) ;
VectorTemplate3T4 & operator+ = ( vectorTemplate3T4 & v) ;
VectorTemplate3T4 & operator * = ( T scalar) ;
VectorTemplate3T4 & operator- = ( VectorTemplate3T4 & v) ;
VectorTemplae3T4 cross( VectorT emplate3T4 & v) ;
}
在向量模板的定义中, 通过运算符的重载, 使我们能像对待普通的数学运算一样来引述两
个向量或向量与标量之间的四则运算. 例如: 对于两个向量V1、V2 的内积, 只须简单地表示为
V1* V2, 而对于一个标量s 与向量V1 的乘积亦只须记着s* V1, 而丝毫不会引起混淆.
2. 2 矩阵模板的定义
template 3class T4
class Mat rixTemplate {
private:
int numRow s;
int numColumns;
1 28 江西师范大学学报( 自然科学版) 1997 年
VectorTemplate3T4 * element s;
Void create( int , int ) ;
protected:
public:
Mat rixTemplate( void) {
numRow s= 0;
numColumns= 0;
elements= NULL;
} ;
Mat rixTemplate( int size) { create( size, size) ; } ;
Mat rixTemplate( int, int ) ;
Mat rixTemplate( Mat rixTemplate3T4 & mSrc) ;
-Mat rixT emplate( void) ;
VectorTemplate3T4& operator[ ] ( int row ) { return elements[ row ] ; } ;
int nrows( void) { return numRows; } ;
int ncols( void) { return numColumns; } ;
void resize( int new-ncols, int new-nrows) ;
friend Mat rixT emplate3T4 operator * ( Mat rixTemplate3T4 & , Mat rixTemplate3T4 & ) ;
friend VectorTemplate3T4 operator * ( MatrixTemplate3T4 & , VectorTemplate3T4 & ) ;
Mat rixTemplate3T4& operator * = ( Mat rixTemplate3T4 & m)
{ * this= m * ( * this) ; return * this; } ;
Mat rixTemplate3T4& operator= ( MatrixT emplate3T4 & ) ;
float determinant ( void) ;
Mat rixTemplate3T4& t ranslate( VectorTemplate3T4 & ) ;
Mat rixTemplate3T4& scale( VectorTemplate3T4 & ) ;
Mat rixTemplate3T4& t ranslate( float , float ) ;
Mat rixTemplate3T4& scale( f loat, f loat) ;
Mat rixTemplate3T4& rotate( f loat ) ;
Mat rixTemplate3T4& t ranslate( float , float , f loat) ;
Mat rixTemplate3T4& scale( f loat, f loat, float) ;
Mat rixTemplate3T4& rotate( f loat , f loat, f loat) ;
Mat rixTemplate3T4& setIdent ity( void) ;
Mat rixTemplate3T4& ident ity( void) { return set Identity( ) ; } ; } ;
typedef Mat rixTemplate3f loat4 Matrix;
在矩阵模板中, 我们不但定义了各种常用的运算, 而且还封装了平移、缩放、旋转等成员函
数, 这一切都是为了使图象的生成和处理变得更简便些. 但仅有这两个类当然还不够. 我们的
目的是要生成Bezier 曲线, 然而我们知道, 在计算机表示中曲线是由折线来逼近的, 而折线又
是由线段组成的. 此外, 两点决定一条线段. 因此, 为了一般的表示一条Bezier 曲线, 我们需要
先对点、线段和折线有一个统一的描述, 同时, 这也是为了符合GKS-3D 图形标准.
第2 期 陈国华 C+ + Bezier 曲线类的构造 12 9
点的功能无非是用来确定空间的一个坐标, 所以我们不难从向量类中来派生一个点类,
如:
( 1) 点类
Class SplinePoint: public Vector{
public
,,
void setCoord( Vector & v) ;
void getCoord( Vector & v) ;
,,
}
有了点类之定义, 我们可以把折线看成是由一系列顶点控制确定的, 但为了使这一系列顶
点的排列有一定的顺序, 并且便于索引访问, 我们又不得不增加一个简单的线性表类来对之加
以管理.
( 2) 简单表类
template 3class T4
class SimpleTable{
private:
int curSize;
T * * items;
,,
public:
,,
T & operator[ ] ( int ) ;
,,
}
( 3) 折线类
class SplinePolyline{
protected:
SimpleT able3SplinePoint4 point s;
public:
SplinePolyline( void) ;
SplinePolyline( int np) ;
SplinePoint & operator[ ] ( unsigned int index) ;
void draw ( ) ;
}
3 Bezier 曲线类的构造
如上所述, 计算机所表示的曲线实际上只是一条折线, Bezier 曲线也不例外, 只不过根据
用户的精度要求不同增加插值点的个数有所不同而已. 据此, 我们不难从折线类来派生我们所
1 30 江西师范大学学报( 自然科学版) 1997 年
要求的Bezier 曲线类.
class BezCurve: public SplinePolyline
{
private:
int smooth;
public:
BezCurve( void) { } ;
BezCurve( int numPoint In) : SplinePolyline( numPoint In+ 1)
{ smooth= 5; }
void set smooth( int s In) { smooth= s In; }
int getsmooth( void) { return smooth; }
void draw( void) ;
void draw( class SplinePerspective& ) ;
}
在上述构造中, 我们缺省地取光滑度smooth= 5, 即在每两个顶点之间加5 个插值点, 其
中令人感兴趣的当然是draw ( ) 例程, 缺省情况下按2 维的要求来绘制Bezier 曲线, 如果给出
适当的透视参数( 由class Spline Perspective 定义) , 则可按透视要求来绘制Bezier 曲线.
4 应用
在以往的基于C 或Pascal 等语言的Bezier 曲线的生成过程中, 往往是一面定义一条Bez-i
er 曲线, 然后调用一外部过程来绘制它, 因此涉及复杂的参数传递等问题, 而在此处, 每一条
Bezier 曲线都被定义为知道如何绘制自己了. 因此, 要定义一条Bezier 曲线并实际地画出它,
只须简单地遵循以下几步即可.
第一步: 定义一条Bezier 曲线
BezCurve bezcurve( n) ;
如果欲使该曲线更光滑, 则可进一步设置其光滑度
bezcurve. set smooth( sIn) ;
第二步: 设定n 个顶点的坐标
bezcurve. setCoord( x , y )
或bezcurve. setCoord( x , y , z ) ;
或bezcurve. setCoord( vec) ;
第三步: 绘制曲线
bezcurve. draw( ) ;
或对空间曲线bezcurve. draw ( proj) ;
即可完成整条曲线的定义和绘制过程.
第2 期 陈国华 C+ + Bezier 曲线类的构造 13 1
参 考 文 献
1 Marc Berger. Computer g raphics wit h Pascal. T he Benjamin/ Cummings Pub Inc, 1986. 1~ 340
2 易忠亮. C+ + 矩阵运算类与Windows 应用软件. 北京: 清华大学出版社, 1995. 1~ 380
3 罗振东, 廖光裕. 计算机图示学原理和方法. 上海: 复旦大学出版社, 1993. 120~ 140
4 蔡士杰等. 三维图形系统PHIGS 的原理与技术. 南京: 南京大学出版社, 1993. 62~ 89
5 施法中. 计算机辅助几何设计与非均匀有理B 样条. 北京: 北京航空航天大学出版社, 1994. 121~ 173
The Construction of C+ + Bezier Curve Class
Chen Guohua
( Department of Comput er Science, Guangdong Inst itut e for Nat ionalities, Guangzhou 510633)
Abstract: In this paper, we have used the C+ + OOP feature to encapsulate the procedure of
both defining and draw ing of a Bezier curve, so greatly simplifed the programming of draw ing a
Bezier curve.
Key words: OOP, class, template, Bezier curve
( 上接第120 页)
参 考 文 献
1 刘余善. 实用管理系统工程. 杭州: 浙江人民出版社, 1983. 266~ 268
2 魏国华, 傅家良, 周仲良. 实用运筹学. 上海: 复旦大学出版社, 1987. 363~ 365
3 王俊峰. 确定有批量价格折扣问题的经济订购批量的最大利润法. 系统工程理论与实践, 1996, ( 5) : 60
~ 63
EOQModels Concerning the Questions of Permissible Shortage
and Quantity Discount
Wan Baozhen
( Commercial College of Jiangxi Normal University, Nanchang 330027)
Abstract: With a study of the improvement on the broadening of sphere of applicat ion and the
re-establishment of cost project, the paper presents a new SDEOQ model that is more suitable to
the pract ices in the area of product ion and circulat ion under the condit ions of socialist market economy
and a discussion about the opt imality of SDEOQ model w ith both it s part ial and whole opt
imal solut ions being achieved.
Key words: economic order quant ity( EOQ) , permissble shortage, quant ity discount , opt imal-i
ty
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。