-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3d.h
111 lines (85 loc) · 1.9 KB
/
vec3d.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
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
// vec3d.h : 3d vector mathematics
//
#pragma once
#include <cmath>
template<typename real> struct Vec3D
{
real x, y, z;
Vec3D()
{
}
Vec3D(const Vec3D &v) : x(v.x), y(v.y), z(v.z)
{
}
Vec3D(real x_, real y_, real z_) : x(x_), y(y_), z(z_)
{
}
Vec3D &operator = (const Vec3D &v)
{
x = v.x; y = v.y; z = v.z; return *this;
}
Vec3D operator + (const Vec3D &v) const
{
return Vec3D(x + v.x, y + v.y, z + v.z);
}
Vec3D &operator += (const Vec3D &v)
{
x += v.x; y += v.y; z += v.z; return *this;
}
Vec3D operator - (const Vec3D &v) const
{
return Vec3D(x - v.x, y - v.y, z - v.z);
}
Vec3D &operator -= (const Vec3D &v)
{
x -= v.x; y -= v.y; z -= v.z; return *this;
}
Vec3D operator - () const
{
return Vec3D(-x, -y, -z);
}
Vec3D operator * (real a) const
{
return Vec3D(a * x, a * y, a * z);
}
friend inline Vec3D<real> operator * (real a, const Vec3D<real> &v)
{
return v * a;
}
Vec3D &operator *= (real a)
{
x *= a; y *= a; z *= a; return *this;
}
real operator * (const Vec3D &v) const
{
return x * v.x + y * v.y + z * v.z;
}
Vec3D operator / (real a) const
{
return (*this) * (1 / a);
}
Vec3D operator /= (real a)
{
return (*this) *= (1 / a);
}
Vec3D operator % (const Vec3D &v) const
{
return Vec3D(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
Vec3D &operator %= (const Vec3D &v)
{
*this = *this % v; return *this;
}
real sqr() const
{
return x * x + y * y + z * z;
}
real len() const
{
return std::sqrt(x * x + y * y + z * z);
}
friend inline Vec3D<real> normalize(const Vec3D<real> &v)
{
return v / v.len();
}
};