新建网页 1
提供以下基本操作:
1. 存取向量的各分量(x, y , z)
2. 向量间的赋值操作
3. 比较两向量是否相同
4. 将向量置为零向量
5. 向量求负
6. 求向量的模
7. 向量与标量的乘除法
8. 向量标准化
9. 向量加减法
10. 计算两点(点用向量表示)间距离
11. 向量点乘
12. 向量叉乘
该向量的操作运算对3D点同样适合。
#include <math.h>
class cVector3
{
public:
float x, y, z;
public:
cVector3() { }
cVector3(const cVector3& v)
{
x = v.x; y = v.y; z = v.z;
}
cVector3(float vx, float vy, float vz)
{
x = vx; y = vy; z = vz;
}
cVector3& operator=(const cVector3& v)
{
x = v.x; y = v.y; z = v.z;
return *this;
}
bool operator==(const cVector3& v)
{
return (x == v.x && y == v.y && z == v.z);
}
bool operator!=(const cVector3& v)
{
return (x != v.x || y != v.y || z != v.z);
}
void zero()
{
// set the vector to zero
x = y = z = 0.0f;
}
cVector3 operator-()
{
// unary minus returns the negative of the vector
return cVector3(-x, -y, -z);
}
cVector3 operator+(const cVector3& v)
{
return cVector3(x + v.x, y + v.y, z + v.z);
}
cVector3 operator-(const cVector3& v)
{
return cVector3(x - v.x, y - v.y, z - v.z);
}
cVector3 operator*(float scale)
{
// multiplication and division by scalar
return cVector3(x * scale, y * scale, z * scale);
}
cVector3 operator/(float scale)
{
float temp = 1.0f / scale; // NOTE: no check for divide by zero here
return cVector3(x * temp, y * temp, z * temp);
}
cVector3& operator+=(const cVector3& v)
{
x += v.x; y += v.y; z += v.z;
return *this;
}
cVector3& operator-=(const cVector3& v)
{
x -= v.x; y -= v.y; z -= v.z;
return *this;
}
cVector3& operator*=(float scale)
{
x *= scale; y *= scale; z *= scale;
return *this;
}
cVector3& operator/=(float scale)
{
float temp = 1.0f / scale;
x *= temp; y *= temp; z *= temp;
return *this;
}
void normalize()
{
// normalize the vector
float mag = x * x + y * y + z * z;
if(mag > 0.0f) // check for divide-by-zero
{
float one_over_mag = 1.0f / sqrt(mag);
x *= one_over_mag;
y *= one_over_mag;
z *= one_over_mag;
}
}
float operator*(const cVector3& v)
{
// vector dot product, we overload the standard multiplication symbol to do this.
return (x * v.x + y * v.y + z * v.z);
}
};
/***************************************************************************************************/
inline float vector_mag(const cVector3& v)
{
// compute the magnitude of a vector
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
inline cVector3 cross_product(const cVector3& a, const cVector3& b)
{
// compute the cross product of two vectors
return cVector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
inline cVector3 operator*(float k, const cVector3& v)
{
// scalar on the left multiplication, for symmetry.
return cVector3(k * v.x, k * v.y, k * v.z);
}
inline float distance(const cVector3& a, const cVector3& b)
{
// compute the distance between two points
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.z - b.z;
return sqrt(dx * dx + dy * dy + dz * dz);
}
inline float distance_squared(const cVector3& a, const cVector3& b)
{
// compute the distance between two points
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.z - b.z;
return (dx * dx + dy * dy + dz * dz);
}
extern const cVector3 g_zero_vector;