-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.cpp
99 lines (79 loc) · 2.11 KB
/
vector.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
#include "vector3.h"
const Vector3f Vector3f::UnitX(1.0, 0.0, 0.0);
const Vector3f Vector3f::UnitY(0.0, 1.0, 0.0);
const Vector3f Vector3f::UnitZ(0.0, 0.0, 1.0);
Vector3f operator +(const Vector3f &v, float s)
{
return (Vector3f(v.x + s, v.y + s, v.z + s));
}
Vector3f operator +(float s, const Vector3f &v)
{
return (Vector3f(s + v.x, s + v.y, s + v.z));
}
Vector3f operator +(const Vector3f &u, const Vector3f &v)
{
return (Vector3f(u.x + v.x, u.y + v.y, u.z + v.z));
}
Vector3f operator -(const Vector3f &v, float s)
{
return (Vector3f(v.x - s, v.y - s, v.z - s));
}
Vector3f operator -(float s, const Vector3f &v)
{
return (Vector3f(s - v.x, s - v.y, s - v.z));
}
Vector3f operator -(const Vector3f &u, const Vector3f &v)
{
return (Vector3f(u.x - v.x, u.y - v.y, u.z - v.z));
}
Vector3f operator *(const Vector3f &v, float s)
{
return (Vector3f(v.x * s, v.y * s, v.z * s));
}
Vector3f operator *(float s, const Vector3f &v)
{
return (Vector3f(s * v.x, s * v.y, s * v.z));
}
Vector3f operator *(const Vector3f &u, const Vector3f &v)
{
return (Vector3f(u.x * v.x, u.y * v.y, u.z * v.z));
}
Vector3f operator /(const Vector3f &v, float s)
{
float inv = 1.0f / s;
return (Vector3f(v.x * inv, v.y * inv, v.z * inv));
}
Vector3f operator /(float s, const Vector3f &v)
{
return (Vector3f(s / v.x, s / v.y, s / v.z));
}
Vector3f operator /(const Vector3f &u, const Vector3f &v)
{
return (Vector3f(u.x / v.x, u.y / v.y, u.z / v.z));
}
Vector3f operator -(const Vector3f &v)
{
return (Vector3f(-v.x, -v.y, -v.z));
}
float dot(const Vector3f &u, const Vector3f &v)
{
return (u.x * v.x + u.y * v.y + u.z * v.z);
}
Vector3f cross(const Vector3f &u, const Vector3f &v)
{
return (Vector3f(u.y * v.z - v.y * u.z,
u.z * v.x - u.x * v.z,
u.x * v.y - u.y * v.x));
}
float length(const Vector3f &v)
{
return ((float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z));
}
bool isZeroLength(const Vector3f &v) {
float sqlen = v.x*v.x + v.y*v.y + v.z*v.z;
return sqlen < (1e-06 * 1e-06);
}
Vector3f normalize(const Vector3f &v)
{
return (v / length(v));
}