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

本文介绍了如何使用C++实现一个基本的Vector3类,包括加减乘除运算、点乘、叉乘以及向量单位化的功能。通过提供的测试主程序验证了这些操作的正确性。
最低0.47元/天 解锁文章
2120

被折叠的 条评论
为什么被折叠?



