| TGX 1.1.3 A tiny 2D/3D graphics library optimized for 32 bits microcontrollers.

Loading...
Searching...
No Matches
Vec2.h
Go to the documentation of this file.
1
5//
6// Copyright 2020 Arvind Singh
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public
10// License as published by the Free Software Foundation; either
11//version 2.1 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
16// Lesser General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public
19// License along with this library; If not, see <http://www.gnu.org/licenses/>.
20
21
22#ifndef _TGX_VEC2_H_
23#define _TGX_VEC2_H_
24
25// only C++, no plain C
26#ifdef __cplusplus
27
28
29#include <stdint.h>
30#include "Misc.h"
31
32 namespace tgx
33{
34
35
36 // forward declarations
37
38 template<typename T> struct Vec2;
39
40 template<typename T> struct Vec3;
41
42 template<typename T> struct Vec4;
43
44
45 // Specializations
46
47 typedef Vec2<int> iVec2;
48
49 typedef Vec2<float> fVec2;
50
51 typedef Vec2<double> dVec2;
52
53
63 template<typename T> struct Vec2
64 {
65
66 // mtools extension (if available).
67 #if (MTOOLS_TGX_EXTENSIONS)
68 #include <mtools/extensions/tgx/tgx_ext_Vec2.inl>
69 #endif
70
71 // coordinates
72 T x;
73 T y;
74
75
79 Vec2() {}
80
81
85 constexpr Vec2(T X, T Y) : x(X), y(Y) {}
86
87
91 Vec2(const Vec2 & v) = default;
92
93
97 Vec2 & operator=(const Vec2 & V) = default;
98
99
106 template<typename U>
107 explicit operator Vec2<U>() const { return Vec2<U>((U)x, (U)y); }
108
109
113 operator Vec2<typename DefaultFPType<T>::fptype>() const { return Vec2<typename DefaultFPType<T>::fptype>((typename DefaultFPType<T>::fptype)x, (typename DefaultFPType<T>::fptype)y); }
114
115
119 inline bool operator==(const Vec2 & V) const
120 {
121 return ((x == V.x) && (y == V.y));
122 }
123
124
128 inline bool operator!=(const Vec2 & V) const
129 {
130 return(!(operator==(V)));
131 }
132
133
137 inline bool operator<(const Vec2 & V) const
138 {
139 return ((x < V.x) || ((x == V.x) && (y < V.y)));
140 }
141
142
146 inline bool operator<=(const Vec2 & V) const
147 {
148 return ((x < V.x) || ((x == V.x) && (y <= V.y)));
149 }
150
151
155 inline bool operator>(const Vec2 & V) const
156 {
157 return(!(operator<=(V)));
158 }
159
160
164 inline bool operator>=(const Vec2 & V) const
165 {
166 return(!(operator<(V)));
167 }
168
169
173 TGX_INLINE inline void operator+=(const Vec2 & V)
174 {
175 x += V.x;
176 y += V.y;
177 }
178
179
183 TGX_INLINE inline void operator-=(const Vec2 & V)
184 {
185 x -= V.x;
186 y -= V.y;
187 }
188
189
193 TGX_INLINE inline void operator*=(const Vec2 & V)
194 {
195 x *= V.x;
196 y *= V.y;
197 }
198
199
203 TGX_INLINE inline void operator/=(const Vec2 & V)
204 {
205 x /= V.x;
206 y /= V.y;
207 }
208
209
213 TGX_INLINE inline void operator+=(T v)
214 {
215 x += v;
216 y += v;
217 }
218
219
223 TGX_INLINE inline void operator-=(T v)
224 {
225 x -= v;
226 y -= v;
227 }
228
232 TGX_INLINE inline void operator*=(T v)
233 {
234 x *= v;
235 y *= v;
236 }
237
241 TGX_INLINE inline void operator/=(T v)
242 {
243 x /= v;
244 y /= v;
245 }
246
247
251 TGX_INLINE inline Vec2 operator-() const
252 {
253 return Vec2{ -x, -y };
254 }
255
256
263 TGX_INLINE inline T norm2() const
264 {
265 return x*x + y*y;
266 }
267
268
277 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Tfloat norm() const
278 {
279 return (Tfloat)tgx::precise_sqrt((Tfloat)(x*x + y*y));
280 }
281
282
292 template<typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Tfloat norm_fast() const
293 {
294 return (Tfloat)tgx::fast_sqrt((Tfloat)(x*x + y*y));
295 }
296
297
306 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Tfloat invnorm() const
307 {
308 return (Tfloat)tgx::precise_invsqrt((Tfloat)(x*x + y*y));
309 }
310
311
321 template<typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Tfloat invnorm_fast() const
322 {
323 return (Tfloat)tgx::fast_invsqrt((Tfloat)(x*x + y*y));
324 }
325
326
335 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline void normalize()
336 {
337 Tfloat a = invnorm<Tfloat>();
338 x = (T)(x * a);
339 y = (T)(y * a);
340 }
341
342
352 template<typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline void normalize_fast()
353 {
354 Tfloat a = invnorm_fast<Tfloat>();
355 x = (T)(x * a);
356 y = (T)(y * a);
357 }
358
359
368 template<typename Tfloat = typename DefaultFPType<T>::fptype > inline Vec2<T> getNormalize() const
369 {
370 Vec2<T> V(*this);
371 V.normalize();
372 return V;
373 }
374
375
385 template<typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Vec2<T> getNormalize_fast() const
386 {
387 Vec2<T> V(*this);
388 V.normalize_fast();
389 return V;
390 }
391
392
393
397 TGX_INLINE inline void rotate90()
398 {
399 *this = getRotate90();
400 }
401
402
406 TGX_INLINE inline Vec2<T> getRotate90() const
407 {
408 return Vec2<T>(-y, x);
409 }
410
411
420 inline int leftOf(const Vec2<T> & LA, const Vec2<T> & LB) const;
421
422
431 inline bool setAsIntersection(const Vec2<T> & LA1, const Vec2<T> & LA2, const Vec2<T> & LB1, const Vec2<T> & LB2);
432
433
434
435#ifdef TGX_ON_ARDUINO
436
442 inline void print(Stream & outputStream = Serial) const
443 {
444 outputStream.printf("[%.6f \t %.6f]\n", x, y);
445 }
446
447#endif
448
449 };
450
451
452
459 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Vec2<T> normalize(Vec2<T> V)
460 {
461 V.template normalize<Tfloat>();
462 return V;
463 }
464
465
473 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Vec2<T> normalize_fast(Vec2<T> V)
474 {
475 V.template normalize_fast<Tfloat>();
476 return V;
477 }
478
479
483 template<typename T> TGX_INLINE inline T dist2(const Vec2<T> & V1, const Vec2<T> & V2)
484 {
485 const T xx = V1.x - V2.x;
486 const T yy = V1.y - V2.y;
487 return xx * xx + yy * yy;
488 }
489
490
497 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Tfloat dist(const Vec2<T> & V1, const Vec2<T> & V2)
498 {
499 const T xx = V1.x - V2.x;
500 const T yy = V1.y - V2.y;
501 return (Tfloat)tgx::precise_sqrt((Tfloat)(xx * xx + yy * yy));
502 }
503
504
512 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Tfloat dist_fast(const Vec2<T> & V1, const Vec2<T> & V2)
513 {
514 const T xx = V1.x - V2.x;
515 const T yy = V1.y - V2.y;
516 return (Tfloat)tgx::fast_sqrt((Tfloat)(xx * xx + yy * yy));
517 }
518
519
521 template<typename T> TGX_INLINE inline Vec2<T> operator+(Vec2<T> V1, const Vec2<T> & V2) { V1 += V2; return V1; }
522
524 template<typename T> TGX_INLINE inline Vec2<T> operator-(Vec2<T> V1, const Vec2<T> & V2) { V1 -= V2; return V1; }
525
527 template<typename T> TGX_INLINE inline Vec2<T> operator*(Vec2<T> V1, const Vec2<T> & V2) { V1 *= V2; return V1; }
528
530 template<typename T> TGX_INLINE inline Vec2<T> operator/(Vec2<T> V1, const Vec2<T> & V2) { V1 /= V2; return V1; }
531
533 template<typename T> TGX_INLINE inline Vec2<T> operator+(const T a, Vec2<T> V) { V += a; return V; }
534
536 template<typename T> TGX_INLINE inline Vec2<T> operator+(Vec2<T> V, const T a) { V += a; return V; }
537
539 template<typename T> TGX_INLINE inline Vec2<T> operator-(Vec2<T> V, const T a) { V -= a; return V; }
540
542 template<typename T> TGX_INLINE inline Vec2<T> operator*(const T a, Vec2<T> V) { V *= a; return V; }
543
545 template<typename T> TGX_INLINE inline Vec2<T> operator*(Vec2<T> V, const T a) { V *= a; return V; }
546
548 template<typename T> TGX_INLINE inline Vec2<T> operator/(Vec2<T> V, const T a) { V /= a; return V; }
549
550
554 template<typename T> TGX_INLINE inline T dotProduct(const Vec2<T> & U, const Vec2<T> & V)
555 {
556 return U.x * V.x + U.y * V.y;
557 }
558
559#if TGX_USE_FMA_MATH
564 TGX_INLINE inline float dotProduct(const Vec2<float>& U, const Vec2<float>& V)
565 {
566 return fmaf(U.x, V.x, U.y * V.y);
567 }
568#endif
569
570
574 template<typename T> TGX_INLINE inline T crossProduct(const Vec2<T>& U, const Vec2<T>& V)
575 {
576 return (U.x * V.y - U.y * V.x);
577 }
578
579
583 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > TGX_INLINE inline Vec2<T> lerp(Tfloat alpha, const Vec2<T> & V1, const Vec2<T> & V2)
584 {
585 return Vec2<T>{ (T)(V1.x + alpha * (V2.x - V1.x)),
586 (T)(V1.y + alpha * (V2.y - V1.y)) };
587 }
588
589
590
591 template<typename T> TGX_INLINE inline int Vec2<T>::leftOf(const Vec2<T> & LA, const Vec2<T> & LB) const
592 {
593 T x = crossProduct(LB - LA, (*this) - LB);
594 return ((x < 0) ? -1 : ((x > 0) ? 1 : 0));
595 }
596
597
598 template<typename T> inline bool Vec2<T>::setAsIntersection(const Vec2<T> & LA1, const Vec2<T> & LA2, const Vec2<T> & LB1, const Vec2<T> & LB2)
599 {
600 const T a1 = LA2.y - LA1.y;
601 const T b1 = LA1.x - LA2.x;
602 const T a2 = LB2.y - LB1.y;
603 const T b2 = LB1.x - LB2.x;
604 const T delta = a1 * b2 - a2 * b1;
605 if (delta == 0) { return false; }
606 const T c1 = LA1.x * a1 + LA1.y * b1;
607 const T c2 = LB1.x * a2 + LB1.y * b2;
608 x = ((b1 == 0) ? LA1.x : ((b2 == 0) ? LB1.x : (T)((b2 * c1 - b1 * c2) / delta))); // complicated but insures perfect clipping
609 y = ((a1 == 0) ? LA1.y : ((a2 == 0) ? LB1.y : (T)((a1 * c2 - a2 * c1) / delta))); // for horizontal and vertical lines
610 return true;
611 }
612
613
614
615
616
617}
618
619#endif
620
621#endif
622
tgx::operator*
Mat4< T > operator*(const Mat4< T > &A, const Mat4< T > &B)
Matrix-matrix multiplication.
Definition: Mat4.h:689
Misc.h
Utility/miscellaneous functions used throughout the library.
tgx::fast_invsqrt
TGX_INLINE float fast_invsqrt(float x)
Compute a fast approximation of the inverse square root of a float.
Definition: Misc.h:390
tgx::precise_sqrt
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition: Misc.h:291
tgx::precise_invsqrt
TGX_INLINE float precise_invsqrt(float x)
Compute the inverse square root of a float (exact computation).
Definition: Misc.h:343
tgx::fast_sqrt
TGX_INLINE float fast_sqrt(float x)
Compute a fast approximation of the square root of a float.
Definition: Misc.h:311
tgx::dist
TGX_INLINE Tfloat dist(const Vec2< T > &V1, const Vec2< T > &V2)
Compute the euclidian distance between two vectors.
Definition: Vec2.h:497
tgx::operator+
TGX_INLINE Vec2< T > operator+(Vec2< T > V1, const Vec2< T > &V2)
Addition operator.
Definition: Vec2.h:521
tgx::dist2
TGX_INLINE T dist2(const Vec2< T > &V1, const Vec2< T > &V2)
Compute the squared euclidian distance between two vectors.
Definition: Vec2.h:483
tgx::normalize
TGX_INLINE Vec2< T > normalize(Vec2< T > V)
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec2.h:459
tgx::operator-
TGX_INLINE Vec2< T > operator-(Vec2< T > V1, const Vec2< T > &V2)
Substraction operator.
Definition: Vec2.h:524
tgx::dotProduct
TGX_INLINE T dotProduct(const Vec2< T > &U, const Vec2< T > &V)
Return the dot product U.V between two vectors.
Definition: Vec2.h:554
tgx::normalize_fast
TGX_INLINE Vec2< T > normalize_fast(Vec2< T > V)
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec2.h:473
tgx::operator/
TGX_INLINE Vec2< T > operator/(Vec2< T > V1, const Vec2< T > &V2)
Division operator.
Definition: Vec2.h:530
tgx::dist_fast
TGX_INLINE Tfloat dist_fast(const Vec2< T > &V1, const Vec2< T > &V2)
Compute the euclidian distance between two vectors.
Definition: Vec2.h:512
tgx::lerp
TGX_INLINE Vec2< T > lerp(Tfloat alpha, const Vec2< T > &V1, const Vec2< T > &V2)
Return the linear interpolation: V1 + alpha(V2 - V1).
Definition: Vec2.h:583
tgx::crossProduct
TGX_INLINE T crossProduct(const Vec2< T > &U, const Vec2< T > &V)
Return the cross product UxV (i.e.
Definition: Vec2.h:574
tgx::Vec2
Generic 2D vector [specializations iVec2, fVec2, dVec2].
Definition: Vec2.h:64
tgx::Vec2::operator*=
TGX_INLINE void operator*=(T v)
scalar multiplication.
Definition: Vec2.h:232
tgx::Vec2::x
T x
'x' coordinate (first dimension)
Definition: Vec2.h:72
tgx::Vec2::operator+=
TGX_INLINE void operator+=(const Vec2 &V)
Add another vector to this one.
Definition: Vec2.h:173
tgx::Vec2::operator+=
TGX_INLINE void operator+=(T v)
scalar addition.
Definition: Vec2.h:213
tgx::Vec2::norm
Tfloat norm() const
Compute the euclidian norm of the vector.
Definition: Vec2.h:277
tgx::Vec2::setAsIntersection
bool setAsIntersection(const Vec2< T > &LA1, const Vec2< T > &LA2, const Vec2< T > &LB1, const Vec2< T > &LB2)
Set this vector as the intersection of the two lines (LA1,LA2) and (LB1,LB2).
Definition: Vec2.h:598
tgx::Vec2::y
T y
'y' coordinate (second dimension)
Definition: Vec2.h:73
tgx::Vec2::getRotate90
TGX_INLINE Vec2< T > getRotate90() const
Return the vector obtained after rotation by +90 degree (anti-clockise).
Definition: Vec2.h:406
tgx::Vec2::invnorm
Tfloat invnorm() const
Compute the inverse of the euclidian norm of the vector.
Definition: Vec2.h:306
tgx::Vec2::Vec2
Vec2(const Vec2 &v)=default
Default copy constructor.
tgx::Vec2::operator-
TGX_INLINE Vec2 operator-() const
unary negation operator
Definition: Vec2.h:251
tgx::Vec2::getNormalize
Vec2< T > getNormalize() const
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec2.h:368
tgx::Vec2::operator/=
TGX_INLINE void operator/=(T v)
scalar division.
Definition: Vec2.h:241
tgx::Vec2::Vec2
Vec2()
default constructor: the vector content is undefined.
Definition: Vec2.h:79
tgx::Vec2::operator>=
bool operator>=(const Vec2 &V) const
Greater-than-or-equal comparison operator.
Definition: Vec2.h:164
tgx::Vec2::norm_fast
TGX_INLINE Tfloat norm_fast() const
Compute the euclidian norm of the vector.
Definition: Vec2.h:292
tgx::Vec2::operator==
bool operator==(const Vec2 &V) const
Equality comparator.
Definition: Vec2.h:119
tgx::Vec2::operator*=
TGX_INLINE void operator*=(const Vec2 &V)
Multiply this vector by another one (coordinate by coordinate multiplication).
Definition: Vec2.h:193
tgx::Vec2::norm2
TGX_INLINE T norm2() const
Compute the squared euclidian norm of the vector.
Definition: Vec2.h:263
tgx::Vec2::rotate90
TGX_INLINE void rotate90()
Rotate this vector by +90 degree (anti-clockise).
Definition: Vec2.h:397
tgx::Vec2::operator>
bool operator>(const Vec2 &V) const
Greater-than comparison operator.
Definition: Vec2.h:155
tgx::Vec2::operator<=
bool operator<=(const Vec2 &V) const
Less-than-or-equal comparison operator.
Definition: Vec2.h:146
tgx::Vec2::normalize_fast
TGX_INLINE void normalize_fast()
Normalise the vector so that its norm is 1 (do nothing if the vector is 0).
Definition: Vec2.h:352
tgx::Vec2::operator-=
TGX_INLINE void operator-=(const Vec2 &V)
Substract another vector from this one.
Definition: Vec2.h:183
tgx::Vec2::operator!=
bool operator!=(const Vec2 &V) const
Inequality operator.
Definition: Vec2.h:128
tgx::Vec2::operator<
bool operator<(const Vec2 &V) const
Less-than comparison operator.
Definition: Vec2.h:137
tgx::Vec2::operator=
Vec2 & operator=(const Vec2 &V)=default
Default assignment operator.
tgx::Vec2::normalize
void normalize()
Normalise the vector so that its norm is 1 (do nothing if the vector is 0).
Definition: Vec2.h:335
tgx::Vec2::Vec2
constexpr Vec2(T X, T Y)
Constructor with explicit initialization.
Definition: Vec2.h:85
tgx::Vec2::getNormalize_fast
TGX_INLINE Vec2< T > getNormalize_fast() const
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec2.h:385
tgx::Vec2::invnorm_fast
TGX_INLINE Tfloat invnorm_fast() const
Compute the inverse of the euclidian norm of the vector.
Definition: Vec2.h:321
tgx::Vec2::leftOf
int leftOf(const Vec2< T > &LA, const Vec2< T > &LB) const
Determine which half-space delimited by the line (LA,LB) the point represented by this vector belongs...
Definition: Vec2.h:591
tgx::Vec2::operator/=
TGX_INLINE void operator/=(const Vec2 &V)
Divide this vector by another one (coordinate by coordinate division).
Definition: Vec2.h:203
tgx::Vec2::operator-=
TGX_INLINE void operator-=(T v)
scalar substraction.
Definition: Vec2.h:223