-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector3.cpp
116 lines (94 loc) · 2.44 KB
/
vector3.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//vector3.cpp
//
//You do not need to modify this file
//C.P
////////////////////////////////////////////////////////////
#include "vector3.h"
#include <cmath>
static const Vector3 ZERO = Vector3(0.0, 0.0, 0.0);
Vector3::Vector3()
: x(0.0), y(0.0), z(0.0)
{
}
Vector3::Vector3(double x, double y, double z)
: x(x), y(y), z(z)
{}
Vector3::Vector3(const Vector3 & v)
: x(v.x), y(v.y), z(v.z)
{}
Vector3 Vector3::operator+(const Vector3 & rhs) const
{
return Vector3(x + rhs.x, y + rhs.y, z + rhs.z);
}
Vector3 Vector3::operator-(const Vector3 & rhs) const
{
return Vector3(x - rhs.x, y - rhs.y, z - rhs.z);
}
Vector3 Vector3::operator*(double rhs) const
{
return Vector3(x * rhs, y * rhs, z * rhs);
}
Vector3 Vector3::operator/(double rhs) const
{
return Vector3(x / rhs, y / rhs, z / rhs);
}
Vector3 Vector3::operator+=(const Vector3 & rhs)
{
x += rhs.x; y += rhs.y; z += rhs.z;
return *this;
}
Vector3 Vector3::operator-=(const Vector3 & rhs)
{
x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this;
}
Vector3 Vector3::operator*=(double rhs)
{
x *= rhs; y *= rhs; z *= rhs; return *this;
}
Vector3 Vector3::operator/=(double rhs)
{
x /= rhs; y /= rhs; z /= rhs; return *this;
}
double Vector3::magnitude() const
{
return std::sqrt(x * x + y * y + z * z);
}
void Vector3::normalize()
{
*this /= magnitude();
}
Vector3 Vector3::normalized() const
{
return *this / magnitude();
}
double Vector3::dot(const Vector3 & rhs) const
{
return x * rhs.x + y * rhs.y + z * rhs.z;
}
Vector3 Vector3::cross(const Vector3 & rhs) const
{
return Vector3(y * rhs.z - z * rhs.y,
z * rhs.x - x * rhs.z,
x * rhs.y - y * rhs.x);
}
Vector3 Vector3::rotate(const Vector3 & axis, double angle)
{
double cosTheta = std::cos(angle);
double sinTheta = std::sin(angle);
double aXX = axis.x * axis.x;
double aXY = axis.x * axis.y;
double aXZ = axis.x * axis.z;
double aYY = axis.y * axis.y;
double aYZ = axis.y * axis.z;
double aZZ = axis.z * axis.z;
double nx = x * (cosTheta + aXX * (1 - cosTheta)) +
y * (aXY * (1 - cosTheta) - axis.z * sinTheta) +
z * (aXZ * (1 - cosTheta) + axis.y * sinTheta);
double ny = x * (aXY * (1 - cosTheta) + axis.z * sinTheta) +
y * (cosTheta + aYY * (1 - cosTheta)) +
z * (aYZ * (1 - cosTheta) - axis.x * sinTheta);
double nz = x * (aXZ * (1 - cosTheta) - axis.y * sinTheta) +
y * (aYZ * (1 - cosTheta) + axis.x * sinTheta) +
z * (cosTheta + aZZ * (1 - cosTheta));
return Vector3(nx, ny, nz);
}