Vector2,3,4类在DirectX中都有现成的可以调用,不过要实现其中的功能其实也不难,也都是一些简单的数学知识罢了。
本文用C++实现一个简单的Vector3类的功能,暂时有的功能是:
1 + - * /算术运算
2 向量的数量积,又叫:点乘
3 向量的向量积,又叫:叉乘
4 向量单位化(normalization)
//Vecotr3.h
#pragma once
extern const double uZero;
class Vector3
{
float x, y, z;
public:
Vector3():x(0), y(0), z(0){}
Vector3(float x1, float y1, float z1):x(x1), y(y1), z(z1){}
Vector3(const Vector3 &v);
~Vector3();
void operator=(const Vector3 &v);
Vector3 operator+(const Vector3 &v);
Vector3 operator-(const Vector3 &v);
Vector3 operator/(const Vector3 &v);
Vector3 operator*(const Vector3 &v);
Vector3 operator+(float f);
Vector3 operator-(float f);
Vector3 operator/(float f);
Vector3 operator*(float f);
float dot(const Vector3 &v);
float length();
void normalize();
Vector3 crossProduct(const Vector3 &v);
void printVec3();
};
//Vector3.c