absence_thelab
view src/common/n3dmath.inl @ 0:1cffe3409164
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 23 Oct 2014 01:46:07 +0300 |
parents | |
children |
line source
1 float Vector3::DotProduct(const Vector3 &vec) const {
2 return x * vec.x + y * vec.y + z * vec.z;
3 }
5 float DotProduct(const Vector3 &vec1, const Vector3 &vec2) {
6 return vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
7 }
9 Vector3 Vector3::CrossProduct(const Vector3 &vec) const {
10 return Vector3(y * vec.z - z * vec.y, z * vec.x - x * vec.z, x * vec.y - y * vec.x);
11 }
13 Vector3 CrossProduct(const Vector3 &vec1, const Vector3 &vec2) {
14 return Vector3(vec1.y * vec2.z - vec1.z * vec2.y, vec1.z * vec2.x - vec1.x * vec2.z, vec1.x * vec2.y - vec1.y * vec2.x);
15 }
17 Vector3 Vector3::operator +(const Vector3 &vec) const {
18 return Vector3(x + vec.x, y + vec.y, z + vec.z);
19 }
21 Vector3 Vector3::operator -(const Vector3 &vec) const {
22 return Vector3(x - vec.x, y - vec.y, z - vec.z);
23 }
25 Vector3 Vector3::operator *(float scalar) const {
26 return Vector3(x * scalar, y * scalar, z * scalar);
27 }
29 Vector3 Vector3::operator /(float scalar) const {
30 return Vector3(x / scalar, y / scalar, z / scalar);
31 }
33 void Vector3::operator +=(const Vector3 &vec) {
34 x += vec.x;
35 y += vec.y;
36 z += vec.z;
37 }
39 void Vector3::operator -=(const Vector3 &vec) {
40 x -= vec.x;
41 y -= vec.y;
42 z -= vec.z;
43 }
45 void Vector3::operator *=(float scalar) {
46 x *= scalar;
47 y *= scalar;
48 z *= scalar;
49 }
51 void Vector3::operator /=(float scalar) {
52 x /= scalar;
53 y /= scalar;
54 z /= scalar;
55 }
57 Vector3 Vector3::operator -() const {
58 return Vector3(-x, -y, -z);
59 }
61 bool Vector3::operator >(const Vector3 &vec) const {
62 return LengthSq() > vec.LengthSq();
63 }
65 bool Vector3::operator <(const Vector3 &vec) const {
66 return LengthSq() < vec.LengthSq();
67 }
69 bool Vector3::operator >(float len) const {
70 return LengthSq() > len;
71 }
73 bool Vector3::operator <(float len) const {
74 return LengthSq() < len;
75 }
77 bool Vector3::operator ==(const Vector3 &vec) const {
78 return ((*this - vec).Length() < XSmallNumber);
79 }
81 bool Vector3::operator ==(float len) const {
82 return ((this->Length() - len) < XSmallNumber);
83 }
85 Vector3::operator Vector2() const {
86 return Vector2(x, y);
87 }
89 Vector3::operator Vector4() const {
90 return Vector4(x, y, z, 1.0f);
91 }
94 float Vector3::Length() const {
95 return (float)sqrt(x*x + y*y + z*z);
96 }
98 float Vector3::LengthSq() const {
99 return x*x + y*y + z*z;
100 }
102 void Vector3::Normalize() {
103 float len = (float)sqrt(x*x + y*y + z*z);
104 x /= len;
105 y /= len;
106 z /= len;
107 }
109 Vector3 Vector3::Normalized() const {
110 float len = (float)sqrt(x*x + y*y + z*z);
111 return Vector3(x / len, y / len, z / len);
112 }
114 Vector3 Vector3::Reflection(const Vector3 &normal) const {
115 return normal * this->DotProduct(normal) * 2.0f - *this;
116 }