-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.hpp
72 lines (57 loc) · 1.93 KB
/
Vector.hpp
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
#ifndef VECTOR
#define VECTOR
#include <cmath>
struct Vector3 {
double x, y, z;
Vector3() : x(0.0), y(0.0), z(0.0) {}
Vector3(double x_, double y_, double z_) : x(x_), y(y_), z(z_) {}
// Vector negation
Vector3 operator-() const {
return Vector3(-x, -y, -z);
}
// Vector addition
Vector3 operator+(const Vector3& other) const {
return Vector3(x + other.x, y + other.y, z + other.z);
}
// Vector subtraction
Vector3 operator-(const Vector3& other) const {
return Vector3(x - other.x, y - other.y, z - other.z);
}
// Vector scalar multiplication (from left)
Vector3 operator*(double scalar) const {
return Vector3(x * scalar, y * scalar, z * scalar);
}
// Vector scalar multiplication (from right)
friend Vector3 operator*(double scalar, const Vector3& vector) {
return Vector3(vector.x * scalar, vector.y * scalar, vector.z * scalar);
}
// Vector scalar division (from left)
Vector3 operator/(double scalar) const {
return Vector3(x / scalar, y / scalar, z / scalar);
}
// Vector scalar division (from right)
friend Vector3 operator/(double scalar, const Vector3& vector) {
return Vector3(vector.x / scalar, vector.y / scalar, vector.z / scalar);
}
// Vector dot product
float dot(const Vector3& other) const {
return x * other.x + y * other.y + z * other.z;
}
// Vector cross product
Vector3 cross(const Vector3& other) const {
return Vector3(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x);
}
// Vector magnitude
float magnitude() const {
return std::sqrt(x * x + y * y + z * z);
}
// Normalize the vector
Vector3 normalize() const {
float mag = magnitude();
if (mag != 0.0f)
return Vector3(x / mag, y / mag, z / mag);
else
return Vector3();
}
};
#endif