您的位置:首页 > 其它

二、Math3D向量的计算

2016-12-07 22:37 211 查看

向量的运算

零向量

负向量

向量大小、长度、模

标量与向量的乘法

标准化向量

向量的加法和减法

距离公式

向量点乘

向量投影

向量叉乘

C++语言

1. 零向量

[0,0,0]

2. 负向量

一个向量的负向量长度与这个向量的长度是相等的,负向量是这个向量的反向量


v + -v = -v + v = 0



3. 向量大小、长度、模

二维向量





多维向量



例如:



代码实现

#ifndef _VECTOR3_H_INCLUDED_
#define _VECTOR3_H_INCLUDED_
#include <math.h>

class Vector3 {
//标准三维
public:
float x;
float y;
float z;

Vector3() {}
Vector3(const Vector3 &a) :x(a.x), y(a.y), z(a.z) {}
Vector3(float nX, float nY, float nZ) :x(nX), y(nY), z(nZ) {}

void zero() {
x = y = z = 0.0f;
}
Vector3 operator -() const { return Vector3(-x, -y, -z); }
};

inline float vectorMag(const Vector3 &a) {
return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}

#endif


#include <iostream>
#include "Vector3.h"
using namespace std;
void print_v(Vector3 v)
{
cout << "[" << v.x << "," << v.y << "," << v.z << "]" << endl;
}

int main()
{
cout << "HELLO Vector" << endl;

Vector3 v1(100,200,300);
print_v(v1);

Vector3 v2(v1);
print_v(v2);

Vector3 v3(5, -4, 7);
float vR = vectorMag(v3);
cout << vR << endl;

system("pause");
return 0;
}


下一章继续向量的运算。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  math 3d