-
Notifications
You must be signed in to change notification settings - Fork 7
/
vec.h
51 lines (46 loc) · 1.21 KB
/
vec.h
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
#ifndef _VEC_H_
#define _VEC_H_
#include <cmath>
namespace edubpt {
struct Vec {
double x, y, z;
Vec(const double ax = 0, const double ay = 0, const double az = 0) : x(ax), y(ay), z(az) {}
inline Vec operator+(const Vec &b) const {
return Vec(x + b.x, y + b.y, z + b.z);
}
inline Vec operator-(const Vec &b) const {
return Vec(x - b.x, y - b.y, z - b.z);
}
inline Vec operator*(const double b) const {
return Vec(x * b, y * b, z * b);
}
inline Vec operator/(const double b) const {
return Vec(x / b, y / b, z / b);
}
inline const double length_squared() const {
return x*x + y*y + z*z;
}
inline const double length() const {
return sqrt(length_squared());
}
};
inline Vec operator*(double f, const Vec &v) {
return v * f;
}
inline Vec normalize(const Vec &v) {
return v * (1.0 / v.length());
}
inline const Vec multiply(const Vec &v1, const Vec &v2) {
return Vec(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
}
inline const double dot(const Vec &v1, const Vec &v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
inline const Vec cross(const Vec &v1, const Vec &v2) {
return Vec(
(v1.y * v2.z) - (v1.z * v2.y),
(v1.z * v2.x) - (v1.x * v2.z),
(v1.x * v2.y) - (v1.y * v2.x));
}
};
#endif