| TGX 1.0.4 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>() { return Vec2<U>((U)x, (U)y); }
108
109
113 operator Vec2<typename DefaultFPType<T>::fptype>() { 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 inline void operator+=(const Vec2 V)
174 {
175 x += V.x;
176 y += V.y;
177 }
178
179
183 inline void operator-=(const Vec2 V)
184 {
185 x -= V.x;
186 y -= V.y;
187 }
188
189
193 inline void operator*=(const Vec2 V)
194 {
195 x *= V.x;
196 y *= V.y;
197 }
198
199
203 inline void operator/=(const Vec2 V)
204 {
205 x /= V.x;
206 y /= V.y;
207 }
208
209
213 inline void operator+=(const T & v)
214 {
215 x += v;
216 y += v;
217 }
218
219
223 inline void operator-=(const T & v)
224 {
225 x -= v;
226 y -= v;
227 }
228
232 inline void operator*=(const T & v)
233 {
234 x *= v;
235 y *= v;
236 }
237
241 inline void operator/=(const T & v)
242 {
243 x /= v;
244 y /= v;
245 }
246
247
251 inline Vec2 operator-() const
252 {
253 return Vec2{ -x, -y };
254 }
255
256
263 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 > 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 > 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 > 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 > inline Vec2<T> getNormalize_fast() const
386 {
387 Vec2<T> V(*this);
388 V.normalize_fast();
389 return V;
390 }
391
392
393
397 inline void rotate90()
398 {
399 *this = getRotate90();
400 }
401
402
406 inline Vec2<T> getRotate90() const
407 {
408 return Vec2<T>(-y, x);
409 }
410
411
420 inline int leftOf(Vec2<T> LA, Vec2<T> LB) const;
421
422
431 inline bool setAsIntersection(Vec2<T> LA1, Vec2<T> LA2, Vec2<T> LB1, 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 > 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 > 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> 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 > Tfloat dist(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 > Tfloat dist_fast(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> inline Vec2<T> operator+(Vec2<T> V1, const Vec2<T> V2) { V1 += V2; return V1; }
522
524 template<typename T> inline Vec2<T> operator-(Vec2<T> V1, const Vec2<T> V2) { V1 -= V2; return V1; }
525
527 template<typename T> inline Vec2<T> operator*(Vec2<T> V1, const Vec2<T> V2) { V1 *= V2; return V1; }
528
530 template<typename T> inline Vec2<T> operator/(Vec2<T> V1, const Vec2<T> V2) { V1 /= V2; return V1; }
531
533 template<typename T> inline Vec2<T> operator+(const T a, Vec2<T> V) { V += a; return V; }
534
536 template<typename T> inline Vec2<T> operator+(Vec2<T> V, const T a) { V += a; return V; }
537
539 template<typename T> inline Vec2<T> operator-(const T a, Vec2<T> V) { V -= a; return V; }
540
542 template<typename T> inline Vec2<T> operator-(Vec2<T> V, const T a) { V -= a; return V; }
543
545 template<typename T> inline Vec2<T> operator*(const T a, Vec2<T> V) { V *= a; return V; }
546
548 template<typename T> inline Vec2<T> operator*(Vec2<T> V, const T a) { V *= a; return V; }
549
551 template<typename T> inline Vec2<T> operator/(const T a, Vec2<T> V) { V /= a; return V; }
552
554 template<typename T> inline Vec2<T> operator/(Vec2<T> V, const T a) { V /= a; return V; }
555
556
560 template<typename T> inline T dotProduct(const Vec2<T> U, const Vec2<T> V)
561 {
562 return U.x * V.x + U.y * V.y;
563 }
564
565
569 template<typename T> inline T crossProduct(const Vec2<T>& U, const Vec2<T>& V)
570 {
571 return (U.x * V.y - U.y * V.x);
572 }
573
574
578 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > inline Vec2<T> lerp(Tfloat alpha, Vec2<T> V1, Vec2<T> V2)
579 {
580 return Vec2<T>{ (T)(V1.x + alpha * (V2.x - V1.x)),
581 (T)(V1.y + alpha * (V2.y - V1.y)) };
582 }
583
584
585
586 template<typename T> inline int Vec2<T>::leftOf(Vec2<T> LA, Vec2<T> LB) const
587 {
588 T x = crossProduct(LB - LA, (*this) - LB);
589 return ((x < 0) ? -1 : ((x > 0) ? 1 : 0));
590 }
591
592
593 template<typename T> inline bool Vec2<T>::setAsIntersection(Vec2<T> LA1, Vec2<T> LA2, Vec2<T> LB1, Vec2<T> LB2)
594 {
595 const T a1 = LA2.y - LA1.y;
596 const T b1 = LA1.x - LA2.x;
597 const T a2 = LB2.y - LB1.y;
598 const T b2 = LB1.x - LB2.x;
599 const T delta = a1 * b2 - a2 * b1;
600 if (delta == 0) { return false; }
601 const T c1 = LA1.x * a1 + LA1.y * b1;
602 const T c2 = LB1.x * a2 + LB1.y * b2;
603 x = ((b1 == 0) ? LA1.x : ((b2 == 0) ? LB1.x : (T)((b2 * c1 - b1 * c2) / delta))); // complicated but insures perfect clipping
604 y = ((a1 == 0) ? LA1.y : ((a2 == 0) ? LB1.y : (T)((a1 * c2 - a2 * c1) / delta))); // for horizontal and vertical lines
605 return true;
606 }
607
608
609
610
611
612}
613
614#endif
615
616#endif
617
619
tgx::operator*
Mat4< T > operator*(const Mat4< T > &A, const Mat4< T > &B)
Matrix-matrix multiplication.
Definition Mat4.h:633
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:368
tgx::precise_sqrt
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition Misc.h:270
tgx::precise_invsqrt
TGX_INLINE float precise_invsqrt(float x)
Compute the inverse square root of a float (exact computation).
Definition Misc.h:322
tgx::fast_sqrt
TGX_INLINE float fast_sqrt(float x)
Compute a fast approximation of the square root of a float.
Definition Misc.h:288
tgx::crossProduct
T crossProduct(const Vec2< T > &U, const Vec2< T > &V)
Return the cross product UxV (i.e.
Definition Vec2.h:569
tgx::fVec2
Vec2< float > fVec2
Floating point valued 2D vector with single (float) precision.
Definition Vec2.h:49
tgx::iVec2
Vec2< int > iVec2
Integer-valued 2D vector.
Definition Vec2.h:47
tgx::dotProduct
T dotProduct(const Vec2< T > U, const Vec2< T > V)
Return the dot product U.V between two vectors.
Definition Vec2.h:560
tgx::dist_fast
Tfloat dist_fast(Vec2< T > V1, const Vec2< T > V2)
Compute the euclidian distance between two vectors.
Definition Vec2.h:512
tgx::normalize
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::lerp
Vec2< T > lerp(Tfloat alpha, Vec2< T > V1, Vec2< T > V2)
Return the linear interpolation: V1 + alpha(V2 - V1).
Definition Vec2.h:578
tgx::operator+
Vec2< T > operator+(Vec2< T > V1, const Vec2< T > V2)
Addition operator.
Definition Vec2.h:521
tgx::operator/
Vec2< T > operator/(Vec2< T > V1, const Vec2< T > V2)
Division operator.
Definition Vec2.h:530
tgx::dist
Tfloat dist(Vec2< T > V1, const Vec2< T > V2)
Compute the euclidian distance between two vectors.
Definition Vec2.h:497
tgx::dist2
T dist2(const Vec2< T > V1, const Vec2< T > V2)
Compute the squared euclidian distance between two vectors.
Definition Vec2.h:483
tgx::operator-
Vec2< T > operator-(Vec2< T > V1, const Vec2< T > V2)
Substraction operator.
Definition Vec2.h:524
tgx::dVec2
Vec2< double > dVec2
Floating point valued 2D vector with double precision.
Definition Vec2.h:51
tgx::normalize_fast
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::Vec2
Generic 2D vector [specializations iVec2, fVec2, dVec2].
Definition Vec2.h:64
tgx::Vec2::operator+=
void operator+=(const Vec2 V)
Add another vector to this one.
Definition Vec2.h:173
tgx::Vec2::invnorm_fast
Tfloat invnorm_fast() const
Compute the inverse of the euclidian norm of the vector.
Definition Vec2.h:321
tgx::Vec2::operator-
Vec2 operator-() const
unary negation operator
Definition Vec2.h:251
tgx::Vec2< int >::x
int x
Definition Vec2.h:72
tgx::Vec2::getRotate90
Vec2< T > getRotate90() const
Return the vector obtained after rotation by +90 degree (anti-clockise).
Definition Vec2.h:406
tgx::Vec2::operator/=
void operator/=(const Vec2 V)
Divide this vector by another one (coordinate by coordinate division).
Definition Vec2.h:203
tgx::Vec2::operator+=
void operator+=(const 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::operator*=
void operator*=(const T &v)
scalar multiplication.
Definition Vec2.h:232
tgx::Vec2::print
void print(Stream &outputStream=Serial) const
Print a representation of the vector using a given stream object.
Definition Vec2.h:442
tgx::Vec2::normalize_fast
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::leftOf
int leftOf(Vec2< T > LA, Vec2< T > LB) const
Determine which half-space delimited by the line (LA,LB) the point represented by this vector belongs...
Definition Vec2.h:586
tgx::Vec2::operator>=
bool operator>=(const Vec2 V) const
Greater-than-or-equal comparison operator.
Definition Vec2.h:164
tgx::Vec2< int >::y
int y
Definition Vec2.h:73
tgx::Vec2::invnorm
Tfloat invnorm() const
Compute the inverse of the euclidian norm of the vector.
Definition Vec2.h:306
tgx::Vec2::rotate90
void rotate90()
Rotate this vector by +90 degree (anti-clockise).
Definition Vec2.h:397
tgx::Vec2::Vec2
Vec2(const Vec2 &v)=default
Default copy constructor.
tgx::Vec2::operator<=
bool operator<=(const Vec2 V) const
Less-than-or-equal comparison operator.
Definition Vec2.h:146
tgx::Vec2::operator/=
void operator/=(const T &v)
scalar division.
Definition Vec2.h:241
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::norm2
T norm2() const
Compute the squared euclidian norm of the vector.
Definition Vec2.h:263
tgx::Vec2::norm_fast
Tfloat norm_fast() const
Compute the euclidian norm of the vector.
Definition Vec2.h:292
tgx::Vec2::getNormalize_fast
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::Vec2
Vec2()
default constructor: the vector content is undefined.
Definition Vec2.h:79
tgx::Vec2::operator==
bool operator==(const Vec2 V) const
Equality comparator.
Definition Vec2.h:119
tgx::Vec2::operator-=
void operator-=(const T &v)
scalar substraction.
Definition Vec2.h:223
tgx::Vec2::operator!=
bool operator!=(const Vec2 V) const
Inequality operator.
Definition Vec2.h:128
tgx::Vec2::operator-=
void operator-=(const Vec2 V)
Substract another vector from this one.
Definition Vec2.h:183
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::operator<
bool operator<(const Vec2 V) const
Less-than comparison operator.
Definition Vec2.h:137
tgx::Vec2::Vec2
constexpr Vec2(T X, T Y)
Constructor with explicit initialization.
Definition Vec2.h:85
tgx::Vec2::operator*=
void operator*=(const Vec2 V)
Multiply this vector by another one (coordinate by coordinate multiplication).
Definition Vec2.h:193
tgx::Vec2::operator>
bool operator>(const Vec2 V) const
Greater-than comparison operator.
Definition Vec2.h:155
tgx::Vec2::setAsIntersection
bool setAsIntersection(Vec2< T > LA1, Vec2< T > LA2, Vec2< T > LB1, Vec2< T > LB2)
Set this vector as the intersection of the two lines (LA1,LA2) and (LB1,LB2).
Definition Vec2.h:593
tgx::Vec3
Generic 3D vector [specializations iVec3, fVec3, dVec3].
Definition Vec3.h:70
tgx::Vec4
Geenric 4D vector [specializations iVec4, fVec4, dVec4].
Definition Vec4.h:71