Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
vector_4.h
1#ifndef __VEC4_H__
2#define __VEC4_H__ 1
3
4#include "math/matrix_3.h"
5#include "math/vector_3.h"
6namespace coma {
8class Vec4 {
9 public:
10 Vec4();
11 Vec4(float x, float y, float z, float w);
12 Vec4(Vec3 a, float w);
13 Vec4(float a);
14 Vec4(float* values_array);
15 Vec4(const Vec4& other);
16 ~Vec4();
17
18 Vec4 operator+(const Vec4& other) const;
19 Vec4 operator+(float value) const;
20 void operator+=(const Vec4& other);
21 void operator+=(float value);
22 Vec4 operator-(const Vec4& other) const;
23 Vec4 operator-(float value) const;
24 void operator-=(const Vec4& other);
25 void operator-=(float value);
26
27 Vec4 operator*(float value) const;
28 void operator*=(float value);
29 Vec4 operator/(float value) const;
30 void operator/=(float value);
31 bool operator==(const Vec4& other);
32 bool operator!=(const Vec4& other);
33 void operator=(const Vec4& other);
34
37 float Magnitude() const;
38
40 void Normalize();
41
44 Vec4 Normalized() const;
45
48 void Scale(Vec4 scale);
49
52 float SqrMagnitude() const;
53
57
62 static float Distance(const Vec4& a, const Vec4& b);
63
68 static float DotProduct(Vec4 a, Vec4 b);
69
75 static Vec4 Lerp(const Vec4& a, const Vec4& b, float index);
76
77 static const Vec4 one;
78 static const Vec4 zero;
79
80 float x;
81 float y;
82 float z;
83 float w;
84};
85
86// Author: Alan Gutierrez Ramirez
88 Vec4 ans;
89 if (this->w != 0) {
90 float reciproca = 1.0f / this->w;
91 ans *= reciproca;
92 }
93 return ans;
94}
95
96inline float Vec4::Magnitude() const {
97 return sqrtf(x * x + y * y + z * z + w * w);
98}
99
100inline void Vec4::Normalize() { *this /= Magnitude(); }
101
102inline Vec4 Vec4::Normalized() const { return Vec4(*this / Magnitude()); }
103
104inline void Vec4::Scale(Vec4 other) {
105 x *= other.x;
106 y *= other.y;
107 z *= other.z;
108 w *= other.w;
109}
110
111inline float Vec4::SqrMagnitude() const {
112 return x * x + y * y + z * z + w * w;
113}
114
115inline float Vec4::Distance(const Vec4& a, const Vec4& b) {
116 return (b - a).Magnitude();
117}
118
119inline float Vec4::DotProduct(Vec4 a, Vec4 b) {
120 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
121}
122
123inline Vec4 Vec4::Lerp(const Vec4& a, const Vec4& b, float t) {
124 t = t > 1 ? 1 : t;
125 t = t < 0 ? 0 : t;
126 return (b - a) * t + a;
127}
128
129inline Vec4 Vec4::operator+(const Vec4& other) const {
130 return Vec4(x + other.x, y + other.y, z + other.z, w + other.w);
131}
132
133inline Vec4 Vec4::operator+(float value) const {
134 return Vec4(x + value, y + value, z + value, w + value);
135}
136
137inline void Vec4::operator+=(const Vec4& other) {
138 x += other.x;
139 y += other.y;
140 z += other.z;
141 w += other.w;
142}
143
144inline void Vec4::operator+=(float value) {
145 x += value;
146 y += value;
147 z += value;
148 w += value;
149}
150
151inline Vec4 Vec4::operator-(const Vec4& other) const {
152 return Vec4(x - other.x, y - other.y, z - other.z, w - other.w);
153}
154
155inline Vec4 Vec4::operator-(float value) const {
156 return Vec4(x - value, y - value, z - value, w - value);
157}
158
159inline void Vec4::operator-=(const Vec4& other) {
160 x -= other.x;
161 y -= other.y;
162 z -= other.z;
163 w -= other.w;
164}
165
166inline void Vec4::operator-=(float value) {
167 x -= value;
168 y -= value;
169 z -= value;
170 w -= value;
171}
172
173inline Vec4 Vec4::operator*(float value) const {
174 return Vec4(x * value, y * value, z * value, w * value);
175}
176
177inline void Vec4::operator*=(float value) {
178 x *= value;
179 y *= value;
180 z *= value;
181 w *= value;
182}
183
184inline Vec4 Vec4::operator/(float value) const {
185 return Vec4(x / value, y / value, z / value, w / value);
186}
187
188inline void Vec4::operator/=(float value) {
189 x /= value;
190 y /= value;
191 z /= value;
192 w /= value;
193}
194
195inline bool Vec4::operator==(const Vec4& other) {
196 if (x == other.x && y == other.y && z == other.z && w == other.w) return true;
197 return false;
198}
199inline bool Vec4::operator!=(const Vec4& other) {
200 if (x == other.x && y == other.y && z == other.z && w == other.w)
201 return false;
202 return true;
203}
204inline void Vec4::operator=(const Vec4& other) {
205 x = other.x;
206 y = other.y;
207 z = other.z;
208 w = other.w;
209}
210} // namespace coma
211#endif
represents mathematical vector with 3 components
Definition: vector_3.h:12
represents mathematical vector with 4 components
Definition: vector_4.h:8
static float DotProduct(Vec4 a, Vec4 b)
Definition: vector_4.h:119
void Scale(Vec4 scale)
Definition: vector_4.h:104
float Magnitude() const
Definition: vector_4.h:96
static float Distance(const Vec4 &a, const Vec4 &b)
Definition: vector_4.h:115
float SqrMagnitude() const
Definition: vector_4.h:111
Vec4 Homogenize()
Definition: vector_4.h:87
static Vec4 Lerp(const Vec4 &a, const Vec4 &b, float index)
Definition: vector_4.h:123
Vec4 Normalized() const
Definition: vector_4.h:102
copperdielf Math Library
Definition: buffer.hpp:5