64,682
社区成员
发帖
与我相关
我的任务
分享
//! 2d vector template class with lots of operators and methods.
/** As of Irrlicht 1.6, this class supercedes position2d, which should
be considered deprecated. */
template <class T>
class vector2d
{
public:
//! Default constructor (null vector)
vector2d() : X(0), Y(0) {}
//! Constructor with two different values
vector2d(T nx, T ny) : X(nx), Y(ny) {}
//! Constructor with the same value for both members
explicit vector2d(T n) : X(n), Y(n) {}
//! Copy constructor
vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {}
//! 3d vector template class with lots of operators and methods.
/** The vector3d class is used in Irrlicht for three main purposes:
1) As a direction vector (most of the methods assume this).
2) As a position in 3d space (which is synonymous with a direction vector from the origin to this position).
3) To hold three Euler rotations, where X is pitch, Y is yaw and Z is roll.
*/
template <class T>
class vector3d
{
public:
//! Default constructor (null vector).
vector3d() : X(0), Y(0), Z(0) {}
//! Constructor with three different values
vector3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}
//! Constructor with the same value for all elements
explicit vector3d(T n) : X(n), Y(n), Z(n) {}
//! Copy constructor
vector3d(const vector3d<T>& other) : X(other.X), Y(other.Y), Z(other.Z) {}
//举个例子来说明我想表达的内容:
/*
Y
^
| *(end Point)
| /
| /
| /
y +------*(start Point)
| |
+------+---------------> X
x
*/
#include <vector> //std::vector
//平面坐标点
class Point{
public:
int x()const;
int y()const;
};
//(数学)向量,最佳的备选单词应该是vector,
//为了和STL的vector区分开来,命名为MathVector
class MathVector{
public:
Point start()const;
Point end()const;
//方向:(start->end)与+x形成的夹角,范围[0,360]
double direcion()const;
//大小:start到end的距离
double size()const;
};
//和数学向量类MathVector以示区别命名为STLVector
typedef std::vector STLVector;
//至此,自我感觉良好,是一个不错的设计。
int main()
{
STLVector<MathVector> vec; //用于收纳同一平面内的多个向量(MathVector)
const int N = 2;
MathVector v[N];
int vi = 0;
v[vi++] = MathVector(Point(2,1),Point(3,3.8));
v[vi++] = MathVector(Point(2,1),Point(3,3.9));
for(int i=0; i<N; i++)
vec.push_back(v[i]);
//写代码时,因为是自己设计的类,所以总能理解这两个类的区别,
//小心一点,应该不会弄错。
//如果是另一个程序员看见这个main()函数:
//都是vector,一个叫STLVector,一个叫MathVector,
//两个名字看似很像,就像它们都继承至Vector类一样,
//事实上却不是的,很容易误导读者!
//数学向量不应该取名为vector或XXXVector一类的名字,
//那取什么名字看上去比较优雅呢?
return 0;
}