Nebula
Loading...
Searching...
No Matches
vec2.h
Go to the documentation of this file.
1#pragma once
2#ifndef MATH_FLOAT2_H
3#define MATH_FLOAT2_H
4//------------------------------------------------------------------------------
14#include "core/types.h"
15#include "math/scalar.h"
16
17//------------------------------------------------------------------------------
18namespace Math
19{
20struct vec2
21{
23 vec2() = default;
25 vec2(scalar x);
29 vec2(const vec2& rhs) = default;
31 vec2 operator-() const;
33 void operator+=(const vec2& rhs);
35 void operator-=(const vec2& rhs);
37 void operator*=(scalar s);
39 vec2 operator+(const vec2& rhs) const;
41 vec2 operator-(const vec2& rhs) const;
43 vec2 operator*(scalar s) const;
45 bool operator==(const vec2& rhs) const;
47 bool operator!=(const vec2& rhs) const;
49 void set(scalar x, scalar y);
50
52 void load(const scalar* ptr);
54 void store(scalar* ptr) const;
55
57 scalar length() const;
59 scalar lengthsq() const;
61 vec2 abs() const;
63 bool any() const;
65 bool all() const;
67 static vec2 multiply(const vec2& v0, const vec2& v1);
69 static vec2 divide(const vec2& v0, const vec2& v1);
71 static vec2 maximize(const vec2& v0, const vec2& v1);
73 static vec2 minimize(const vec2& v0, const vec2& v1);
75 static vec2 normalize(const vec2& v);
77 static vec2 lt(const vec2& v0, const vec2& v1);
79 static vec2 le(const vec2& v0, const vec2& v1);
81 static vec2 gt(const vec2& v0, const vec2& v1);
83 static vec2 ge(const vec2& v0, const vec2& v1);
84
86 template<typename T> T as() const;
87
90};
91
92//------------------------------------------------------------------------------
95inline
97 x(x), y(x)
98{
99 // empty
100}
101
102//------------------------------------------------------------------------------
105inline
107 x(x), y(y)
108{
109 // empty
110}
111
112//------------------------------------------------------------------------------
115inline bool
116vec2::operator==(const vec2& rhs) const
117{
118 return (this->x == rhs.x) && (this->y == rhs.y);
119}
120
121//------------------------------------------------------------------------------
124inline bool
125vec2::operator!=(const vec2& rhs) const
126{
127 return (this->x != rhs.x) || (this->y != rhs.y);
128}
129
130//------------------------------------------------------------------------------
133inline vec2
135{
136 return vec2(-this->x, -this->y);
137}
138
139//------------------------------------------------------------------------------
142inline vec2
144{
145 return vec2(this->x * t, this->y * t);
146}
147
148//------------------------------------------------------------------------------
151inline void
153{
154 this->x += rhs.x;
155 this->y += rhs.y;
156}
157
158//------------------------------------------------------------------------------
161inline void
163{
164 this->x -= rhs.x;
165 this->y -= rhs.y;
166}
167
168//------------------------------------------------------------------------------
171inline void
173{
174 this->x *= s;
175 this->y *= s;
176}
177
178//------------------------------------------------------------------------------
181inline vec2
182vec2::operator+(const vec2& rhs) const
183{
184 return vec2(this->x + rhs.x, this->y + rhs.y);
185}
186
187//------------------------------------------------------------------------------
190inline vec2
191vec2::operator-(const vec2& rhs) const
192{
193 return vec2(this->x - rhs.x, this->y - rhs.y);
194}
195
196//------------------------------------------------------------------------------
199inline void
201{
202 this->x = x;
203 this->y = y;
204}
205
206//------------------------------------------------------------------------------
209inline
210void vec2::load(const scalar* ptr)
211{
212 this->x = ptr[0];
213 this->y = ptr[1];
214}
215
216//------------------------------------------------------------------------------
219inline
220void vec2::store(scalar* ptr) const
221{
222 ptr[0] = this->x;
223 ptr[1] = this->y;
224}
225
226//------------------------------------------------------------------------------
229inline scalar
231{
232 return Math::sqrt(this->x * this->x + this->y * this->y);
233}
234
235//------------------------------------------------------------------------------
238inline scalar
240{
241 return this->x * this->x + this->y * this->y;
242}
243
244//------------------------------------------------------------------------------
247inline vec2
249{
250 return vec2(Math::abs(this->x), Math::abs(this->y));
251}
252
253//------------------------------------------------------------------------------
256inline bool
258{
259 return (this->x != 0.0f) || (this->y != 0.0f);
260}
261
262//------------------------------------------------------------------------------
265inline bool
267{
268 return (this->x != 0.0f) && (this->y != 0.0f);
269}
270
271//------------------------------------------------------------------------------
274inline vec2
275vec2::lt(const vec2& v0, const vec2& v1)
276{
277 vec2 res;
278 res.x = (v0.x < v1.x) ? 1.0f : 0.0f;
279 res.y = (v0.y < v1.y) ? 1.0f : 0.0f;
280 return res;
281}
282
283//------------------------------------------------------------------------------
286inline vec2
287vec2::le(const vec2& v0, const vec2& v1)
288{
289 vec2 res;
290 res.x = (v0.x <= v1.x) ? 1.0f : 0.0f;
291 res.y = (v0.y <= v1.y) ? 1.0f : 0.0f;
292 return res;
293}
294
295//------------------------------------------------------------------------------
298inline vec2
299vec2::gt(const vec2& v0, const vec2& v1)
300{
301 vec2 res;
302 res.x = (v0.x > v1.x) ? 1.0f : 0.0f;
303 res.y = (v0.y > v1.y) ? 1.0f : 0.0f;
304 return res;
305}
306
307//------------------------------------------------------------------------------
310inline vec2
311vec2::ge(const vec2& v0, const vec2& v1)
312{
313 vec2 res;
314 res.x = (v0.x >= v1.x) ? 1.0f : 0.0f;
315 res.y = (v0.y >= v1.y) ? 1.0f : 0.0f;
316 return res;
317}
318
319//------------------------------------------------------------------------------
322inline vec2
324{
325 scalar l = v.length();
326 if (l > 0.0f)
327 {
328 return vec2(v.x / l, v.y / l);
329 }
330 else
331 {
332 return vec2(1.0f, 0.0f);
333 }
334}
335
336
337//------------------------------------------------------------------------------
340inline vec2
341vec2::multiply( const vec2& v0, const vec2& v1 )
342{
343 return vec2(v0.x * v1.x, v0.y * v1.y);
344}
345
346//------------------------------------------------------------------------------
349inline vec2
350vec2::divide(const vec2& v0, const vec2& v1)
351{
352 return vec2(v0.x / v1.x, v0.y / v1.y);
353}
354
355//------------------------------------------------------------------------------
358inline vec2
359vec2::maximize(const vec2& v0, const vec2& v1)
360{
361 return vec2(Math::max(v0.x, v1.x), Math::max(v0.y, v1.y));
362}
363
364//------------------------------------------------------------------------------
367inline vec2
368vec2::minimize(const vec2& v0, const vec2& v1)
369{
370 return vec2(Math::min(v0.x, v1.x), Math::min(v0.y, v1.y));
371}
372
373//------------------------------------------------------------------------------
376__forceinline vec2
377lerp(const vec2& v0, const vec2& v1, scalar s)
378{
379 return v0 + ((v1 - v0) * s);
380}
381
382
383} // namespace Math
384//------------------------------------------------------------------------------
385#endif
Different curves.
Definition angularpfeedbackloop.h:17
__forceinline scalar sqrt(scalar x)
Definition scalar.h:236
__forceinline float lerp(float x, float y, float l)
Linearly interpolate between 2 values: ret = x + l * (y - x)
Definition scalar.h:606
__forceinline TYPE min(TYPE a, TYPE b)
Definition scalar.h:399
float scalar
Definition scalar.h:45
__forceinline TYPE max(TYPE a, TYPE b)
Definition scalar.h:368
__forceinline scalar abs(scalar a)
Definition scalar.h:441
Nebula's scalar datatype.
A 2-component float vector class.
Definition vec2.h:21
bool operator==(const vec2 &rhs) const
equality operator
Definition vec2.h:116
void set(scalar x, scalar y)
set content
Definition vec2.h:200
bool any() const
return true if any components are non-zero
Definition vec2.h:257
static vec2 ge(const vec2 &v0, const vec2 &v1)
set greater-or-equal components to non-zero
Definition vec2.h:311
void store(scalar *ptr) const
write content memory
Definition vec2.h:220
static vec2 lt(const vec2 &v0, const vec2 &v1)
set less-then components to non-zero
Definition vec2.h:275
static vec2 minimize(const vec2 &v0, const vec2 &v1)
return vector made up of smallest components of 2 vectors
Definition vec2.h:368
bool operator!=(const vec2 &rhs) const
inequality operator
Definition vec2.h:125
scalar lengthsq() const
return squared length of vector
Definition vec2.h:239
void operator*=(scalar s)
inplace scalar multiply
Definition vec2.h:172
void load(const scalar *ptr)
load content from memory
Definition vec2.h:210
void operator+=(const vec2 &rhs)
inplace add
Definition vec2.h:152
static vec2 multiply(const vec2 &v0, const vec2 &v1)
component wise multiplication
Definition vec2.h:341
vec2 operator+(const vec2 &rhs) const
add 2 vectors
Definition vec2.h:182
vec2 operator-() const
flip sign
Definition vec2.h:134
vec2 operator*(scalar s) const
multiply with scalar
Definition vec2.h:143
scalar x
Definition vec2.h:88
static vec2 maximize(const vec2 &v0, const vec2 &v1)
return vector made up of largest components of 2 vectors
Definition vec2.h:359
vec2()=default
default constructor, NOTE: does NOT setup components!
bool all() const
return true if all components are non-zero
Definition vec2.h:266
static vec2 normalize(const vec2 &v)
return normalized version of vector
Definition vec2.h:323
vec2(const vec2 &rhs)=default
copy constructor
static vec2 divide(const vec2 &v0, const vec2 &v1)
component wise division
Definition vec2.h:350
vec2 abs() const
return component-wise absolute
Definition vec2.h:248
T as() const
convert to anything
static vec2 gt(const vec2 &v0, const vec2 &v1)
set greater-then components to non-zero
Definition vec2.h:299
static vec2 le(const vec2 &v0, const vec2 &v1)
set less-or-equal components to non-zero
Definition vec2.h:287
void operator-=(const vec2 &rhs)
inplace sub
Definition vec2.h:162
scalar y
Definition vec2.h:89
scalar length() const
return length of vector
Definition vec2.h:230