-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVec3.hpp
More file actions
79 lines (62 loc) · 1.77 KB
/
Vec3.hpp
File metadata and controls
79 lines (62 loc) · 1.77 KB
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
/**
* @file Vec3.hpp
* The vector class
* @author Navi Ning <xning5@illinois.edu>
*/
#pragma once
#include <cmath>
/**
* Class Vec3
*/
class Vec3 {
public:
Vec3(double _x = 0, double _y = 0, double _z = 0) : x(_x), y(_y), z(_z) {}
Vec3 operator+(const Vec3 &other) const {
return Vec3(x + other.x, y + other.y, z + other.z);
}
Vec3 operator-(const Vec3 &other) const {
return Vec3(x - other.x, y - other.y, z - other.z);
}
Vec3 operator*(const Vec3 &other) const {
return Vec3(x * other.x, y * other.y, z * other.z);
}
Vec3 operator*(double d) const { return Vec3(d * x, d * y, d * z); }
Vec3 operator/(double d) const { return Vec3(x / d, y / d, z / d); }
Vec3 operator+(double d) const { return Vec3(x + d, y + d, z + d); }
Vec3 operator-(double d) const { return Vec3(x - d, y - d, z - d); }
void operator+=(const Vec3 &other) {
x += other.x;
y += other.y;
z += other.z;
}
/**
* Dot product of two vectors
* @param v other vector
* @return the dot product
*/
double dot(const Vec3 &v) const { return x * v.x + y * v.y + z * v.z; }
/**
* Length of the vector
* @return length of the vector
*/
double length() const { return sqrt(x * x + y * y + z * z); }
/**
* Normalize the vector
* @return the normalized vector
*/
Vec3 normalize() const {
double l = length();
return Vec3(x / l, y / l, z / l);
}
/**
* Cross product of two vectors
* @param v other vector
* @return the cross product
*/
Vec3 cross(const Vec3 &v) {
return Vec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
double x, y, z; // Coordinates
};
inline Vec3 operator*(double d, const Vec3 &v) { return v * d; }
inline Vec3 operator-(const Vec3 &v) { return Vec3(-v.x, -v.y, -v.z); }