-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vec3.cpp
119 lines (98 loc) · 1.72 KB
/
Vec3.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
116
117
118
119
#include "Vec3.h"
#include <math.h>
Vec3 &Vec3::operator+=(const Vec3 &rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
Vec3 Vec3::operator+(const Vec3 &rhs) const
{
Vec3 temp = *this;
temp += rhs;
return temp;
}
Vec3 &Vec3::operator-=(const Vec3 &rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
Vec3 Vec3::operator-(const Vec3 &rhs) const
{
Vec3 temp = *this;
temp -= rhs;
return temp;
}
Vec3 &Vec3::operator*=(const float &rhs)
{
x *= rhs;
y *= rhs;
z *= rhs;
return *this;
}
Vec3 Vec3::operator*(const float &rhs) const
{
Vec3 temp = *this;
temp *= rhs;
return temp;
}
Vec3 &Vec3::operator/=(const float &rhs)
{
if (rhs != 0)
{
x /= rhs;
y /= rhs;
z /= rhs;
}
return *this;
}
Vec3 Vec3::operator/(const float &rhs) const
{
Vec3 temp = *this;
temp /= rhs;
return temp;
}
// normal()
// params:
// return: vector with direction of this, of unit length
// notes:
Vec3 Vec3::normal() const
{
if (!(x == 0 && y == 0 && z == 0))
{
float length = sqrt(x * x + y * y + z * z);
Vec3 temp = *this;
temp /= length;
return temp;
} else return *this;
}
// length()
// params:
// return: float length of vector
// notes:
float Vec3::length() const
{
return sqrt(x * x + y * y + z * z);
}
// dot()
// params: vec3s to dot product
// return: float dot product of two vec3s
// notes:
float dot(const Vec3 &lhs, const Vec3 &rhs)
{
return rhs.x * lhs.x + rhs.y * lhs.y + rhs.z * lhs.z;
}
// cross()
// params: vec3s to cross product
// return: vec3 cross product of two vec3s
// notes:
Vec3 cross(const Vec3 &lhs, const Vec3 &rhs)
{
float x = (lhs.y * rhs.z - lhs.z * rhs.y);
float y = (lhs.z * rhs.x - lhs.x * rhs.z);
float z = (lhs.x * rhs.y - lhs.y * rhs.x);
return Vec3(x, y, z);
}