-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3D.cpp
89 lines (74 loc) · 1.58 KB
/
Vector3D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include "Vector3D.h"
Vector3D::Vector3D()
: x(0), y(0), z(0)
{
}
Vector3D::Vector3D(const Vector3D& vec)
: x(vec.x), y(vec.y), z(vec.z)
{
}
Vector3D::Vector3D(float x, float y, float z)
: x(x), y(y), z(z)
{
}
void Vector3D::normalize()
{
float invLength = 1.0f / sqrt(x * x + y * y + z * z);
x *= invLength;
y *= invLength;
z *= invLength;
}
Vector3D& Vector3D::operator=(const Vector3D& vec)
{
x = vec.x;
y = vec.y;
z = vec.z;
return *this;
}
Vector3D Vector3D::operator+(const Vector3D& vec) const
{
return Vector3D(x + vec.x, y + vec.y, z + vec.z);
}
Vector3D Vector3D::operator-(const Vector3D& vec) const
{
return Vector3D(x - vec.x, y - vec.y, z - vec.z);
}
Vector3D Vector3D::operator*(float m) const
{
return Vector3D(x * m, y * m, z * m);
}
std::ostream& operator<<(std::ostream& os, const Vector3D& vec)
{
os << "Vector3D[" << vec.x << ";" << vec.y << ";" << vec.z << "]";
return os;
}
float Vector3D::getX() const
{
return x;
}
float Vector3D::getY() const
{
return y;
}
float Vector3D::getZ() const
{
return z;
}
Vector3D CrossProduct(const Vector3D& left, const Vector3D& right)
{
float rx = left.y * right.z - left.z * right.y;
float ry = left.z * right.x - left.x * right.z;
float rz = left.x * right.y - left.y * right.x;
return Vector3D(rx, ry, rz);
}
float DotProduct(const Vector3D& left, const Vector3D& right)
{
return (left.x * right.x + left.y * right.y + left.z * right.z);
}
float CosTheta(const Vector3D& left, const Vector3D& right)
{
float cosTheta = DotProduct(left, right);
if (cosTheta < 0) cosTheta = 0;
return cosTheta;
}