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 maximize(const vec2& v0, const vec2& v1);
71 static vec2 minimize(const vec2& v0, const vec2& v1);
73 static vec2 normalize(const vec2& v);
75 static vec2 lt(const vec2& v0, const vec2& v1);
77 static vec2 le(const vec2& v0, const vec2& v1);
79 static vec2 gt(const vec2& v0, const vec2& v1);
81 static vec2 ge(const vec2& v0, const vec2& v1);
82
84 template<typename T> T as() const;
85
88};
89
90//------------------------------------------------------------------------------
93inline
95 x(x), y(x)
96{
97 // empty
98}
99
100//------------------------------------------------------------------------------
103inline
105 x(x), y(y)
106{
107 // empty
108}
109
110//------------------------------------------------------------------------------
113inline bool
114vec2::operator==(const vec2& rhs) const
115{
116 return (this->x == rhs.x) && (this->y == rhs.y);
117}
118
119//------------------------------------------------------------------------------
122inline bool
123vec2::operator!=(const vec2& rhs) const
124{
125 return (this->x != rhs.x) || (this->y != rhs.y);
126}
127
128//------------------------------------------------------------------------------
131inline vec2
133{
134 return vec2(-this->x, -this->y);
135}
136
137//------------------------------------------------------------------------------
140inline vec2
142{
143 return vec2(this->x * t, this->y * t);
144}
145
146//------------------------------------------------------------------------------
149inline void
151{
152 this->x += rhs.x;
153 this->y += rhs.y;
154}
155
156//------------------------------------------------------------------------------
159inline void
161{
162 this->x -= rhs.x;
163 this->y -= rhs.y;
164}
165
166//------------------------------------------------------------------------------
169inline void
171{
172 this->x *= s;
173 this->y *= s;
174}
175
176//------------------------------------------------------------------------------
179inline vec2
180vec2::operator+(const vec2& rhs) const
181{
182 return vec2(this->x + rhs.x, this->y + rhs.y);
183}
184
185//------------------------------------------------------------------------------
188inline vec2
189vec2::operator-(const vec2& rhs) const
190{
191 return vec2(this->x - rhs.x, this->y - rhs.y);
192}
193
194//------------------------------------------------------------------------------
197inline void
199{
200 this->x = x;
201 this->y = y;
202}
203
204//------------------------------------------------------------------------------
207inline
208void vec2::load(const scalar* ptr)
209{
210 this->x = ptr[0];
211 this->y = ptr[1];
212}
213
214//------------------------------------------------------------------------------
217inline
218void vec2::store(scalar* ptr) const
219{
220 ptr[0] = this->x;
221 ptr[1] = this->y;
222}
223
224//------------------------------------------------------------------------------
227inline scalar
229{
230 return Math::sqrt(this->x * this->x + this->y * this->y);
231}
232
233//------------------------------------------------------------------------------
236inline scalar
238{
239 return this->x * this->x + this->y * this->y;
240}
241
242//------------------------------------------------------------------------------
245inline vec2
247{
248 return vec2(Math::abs(this->x), Math::abs(this->y));
249}
250
251//------------------------------------------------------------------------------
254inline bool
256{
257 return (this->x != 0.0f) || (this->y != 0.0f);
258}
259
260//------------------------------------------------------------------------------
263inline bool
265{
266 return (this->x != 0.0f) && (this->y != 0.0f);
267}
268
269//------------------------------------------------------------------------------
272inline vec2
273vec2::lt(const vec2& v0, const vec2& v1)
274{
275 vec2 res;
276 res.x = (v0.x < v1.x) ? 1.0f : 0.0f;
277 res.y = (v0.y < v1.y) ? 1.0f : 0.0f;
278 return res;
279}
280
281//------------------------------------------------------------------------------
284inline vec2
285vec2::le(const vec2& v0, const vec2& v1)
286{
287 vec2 res;
288 res.x = (v0.x <= v1.x) ? 1.0f : 0.0f;
289 res.y = (v0.y <= v1.y) ? 1.0f : 0.0f;
290 return res;
291}
292
293//------------------------------------------------------------------------------
296inline vec2
297vec2::gt(const vec2& v0, const vec2& v1)
298{
299 vec2 res;
300 res.x = (v0.x > v1.x) ? 1.0f : 0.0f;
301 res.y = (v0.y > v1.y) ? 1.0f : 0.0f;
302 return res;
303}
304
305//------------------------------------------------------------------------------
308inline vec2
309vec2::ge(const vec2& v0, const vec2& v1)
310{
311 vec2 res;
312 res.x = (v0.x >= v1.x) ? 1.0f : 0.0f;
313 res.y = (v0.y >= v1.y) ? 1.0f : 0.0f;
314 return res;
315}
316
317//------------------------------------------------------------------------------
320inline vec2
322{
323 scalar l = v.length();
324 if (l > 0.0f)
325 {
326 return vec2(v.x / l, v.y / l);
327 }
328 else
329 {
330 return vec2(1.0f, 0.0f);
331 }
332}
333
334
335//------------------------------------------------------------------------------
338inline vec2
339vec2::multiply( const vec2& v0, const vec2& v1 )
340{
341 return vec2(v0.x * v1.x, v0.y * v1.y);
342}
343
344//------------------------------------------------------------------------------
347inline vec2
348vec2::maximize(const vec2& v0, const vec2& v1)
349{
350 return vec2(Math::max(v0.x, v1.x), Math::max(v0.y, v1.y));
351}
352
353//------------------------------------------------------------------------------
356inline vec2
357vec2::minimize(const vec2& v0, const vec2& v1)
358{
359 return vec2(Math::min(v0.x, v1.x), Math::min(v0.y, v1.y));
360}
361
362//------------------------------------------------------------------------------
365__forceinline vec2
366lerp(const vec2& v0, const vec2& v1, scalar s)
367{
368 return v0 + ((v1 - v0) * s);
369}
370
371
372} // namespace Math
373//------------------------------------------------------------------------------
374#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:597
__forceinline TYPE min(TYPE a, TYPE b)
Definition scalar.h:390
float scalar
Definition scalar.h:45
__forceinline TYPE max(TYPE a, TYPE b)
Definition scalar.h:359
__forceinline scalar abs(scalar a)
Definition scalar.h:432
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:114
void set(scalar x, scalar y)
set content
Definition vec2.h:198
bool any() const
return true if any components are non-zero
Definition vec2.h:255
static vec2 ge(const vec2 &v0, const vec2 &v1)
set greater-or-equal components to non-zero
Definition vec2.h:309
void store(scalar *ptr) const
write content memory
Definition vec2.h:218
static vec2 lt(const vec2 &v0, const vec2 &v1)
set less-then components to non-zero
Definition vec2.h:273
static vec2 minimize(const vec2 &v0, const vec2 &v1)
return vector made up of smallest components of 2 vectors
Definition vec2.h:357
bool operator!=(const vec2 &rhs) const
inequality operator
Definition vec2.h:123
scalar lengthsq() const
return squared length of vector
Definition vec2.h:237
void operator*=(scalar s)
inplace scalar multiply
Definition vec2.h:170
void load(const scalar *ptr)
load content from memory
Definition vec2.h:208
void operator+=(const vec2 &rhs)
inplace add
Definition vec2.h:150
static vec2 multiply(const vec2 &v0, const vec2 &v1)
component wise multiplication
Definition vec2.h:339
vec2 operator+(const vec2 &rhs) const
add 2 vectors
Definition vec2.h:180
vec2 operator-() const
flip sign
Definition vec2.h:132
vec2 operator*(scalar s) const
multiply with scalar
Definition vec2.h:141
scalar x
Definition vec2.h:86
static vec2 maximize(const vec2 &v0, const vec2 &v1)
return vector made up of largest components of 2 vectors
Definition vec2.h:348
vec2()=default
default constructor, NOTE: does NOT setup components!
bool all() const
return true if all components are non-zero
Definition vec2.h:264
static vec2 normalize(const vec2 &v)
return normalized version of vector
Definition vec2.h:321
vec2(const vec2 &rhs)=default
copy constructor
vec2 abs() const
return component-wise absolute
Definition vec2.h:246
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:297
static vec2 le(const vec2 &v0, const vec2 &v1)
set less-or-equal components to non-zero
Definition vec2.h:285
void operator-=(const vec2 &rhs)
inplace sub
Definition vec2.h:160
scalar y
Definition vec2.h:87
scalar length() const
return length of vector
Definition vec2.h:228