38 template<
typename T>
struct Vec2;
40 template<
typename T>
struct Vec3;
42 template<
typename T>
struct Vec4;
63 template<
typename T>
struct Vec2
67 #if (MTOOLS_TGX_EXTENSIONS)
68 #include <mtools/extensions/tgx/tgx_ext_Vec2.inl>
85 constexpr Vec2(T X, T Y) :
x(X),
y(Y) {}
121 return ((
x == V.
x) && (
y == V.
y));
130 return(!(
operator==(V)));
139 return ((
x < V.
x) || ((
x == V.
x) && (
y < V.
y)));
148 return ((
x <= V.
x) || ((
x == V.
x) && (
y <= V.
y)));
157 return(!(
operator<=(V)));
166 return(!(
operator<(V)));
277 template<typename Tfloat = typename DefaultFPType<T>::fptype >
inline Tfloat
norm()
const
292 template<typename Tfloat = typename DefaultFPType<T>::fptype >
inline Tfloat
norm_fast()
const
306 template<typename Tfloat = typename DefaultFPType<T>::fptype >
inline Tfloat
invnorm()
const
321 template<typename Tfloat = typename DefaultFPType<T>::fptype >
inline Tfloat
invnorm_fast()
const
335 template<typename Tfloat = typename DefaultFPType<T>::fptype >
inline void normalize()
337 Tfloat a = invnorm<Tfloat>();
352 template<typename Tfloat = typename DefaultFPType<T>::fptype >
inline void normalize_fast()
354 Tfloat a = invnorm_fast<Tfloat>();
442 inline void print(Stream & outputStream = Serial)
const
444 outputStream.printf(
"[%.6f \t %.6f]\n",
x,
y);
461 V.template normalize<Tfloat>();
475 V.template normalize_fast<Tfloat>();
485 const T xx = V1.
x - V2.
y;
486 const T yy = V1.
y - V2.
y;
487 return xx * xx + yy * yy;
497 template<typename T, typename Tfloat = typename DefaultFPType<T>::fptype > Tfloat
dist(
Vec2<T> V1,
const Vec2<T> V2)
499 const T xx = V1.
x - V2.
y;
500 const T yy = V1.
y - V2.
y;
514 const T xx = V1.
x - V2.
y;
515 const T yy = V1.
y - V2.
y;
562 return U.
x * V.
x + U.
y * V.
y;
571 return (U.
x * V.
y - U.
y * V.
x);
580 return Vec2<T>{ (T)(V1.
x + alpha * (V2.
x - V1.
x)),
581 (T)(V1.
y + alpha * (V2.
y - V1.
y)) };
589 return ((x < 0) ? -1 : ((x > 0) ? 1 : 0));
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)));
604 y = ((a1 == 0) ? LA1.
y : ((a2 == 0) ? LB1.
y : (T)((a1 * c2 - a2 * c1) / delta)));
Mat4< T > operator*(const Mat4< T > &A, const Mat4< T > &B)
Matrix-matrix multiplication.
Definition: Mat4.h:633
Utility/miscellaneous functions used throughout the library.
TGX_INLINE float fast_invsqrt(float x)
Compute a fast approximation of the inverse square root of a float.
Definition: Misc.h:326
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition: Misc.h:254
TGX_INLINE float precise_invsqrt(float x)
Compute the inverse square root of a float (exact computation).
Definition: Misc.h:306
TGX_INLINE float fast_sqrt(float x)
Compute a fast approximation of the square root of a float.
Definition: Misc.h:272
T crossProduct(const Vec2< T > &U, const Vec2< T > &V)
Return the cross product UxV (i.e.
Definition: Vec2.h:569
T dotProduct(const Vec2< T > U, const Vec2< T > V)
Return the dot product U.V between two vectors.
Definition: Vec2.h:560
Tfloat dist_fast(Vec2< T > V1, const Vec2< T > V2)
Compute the euclidian distance between two vectors.
Definition: Vec2.h:512
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
Vec2< T > lerp(Tfloat alpha, Vec2< T > V1, Vec2< T > V2)
Return the linear interpolation: V1 + alpha(V2 - V1).
Definition: Vec2.h:578
Vec2< T > operator+(Vec2< T > V1, const Vec2< T > V2)
Addition operator.
Definition: Vec2.h:521
Vec2< T > operator/(Vec2< T > V1, const Vec2< T > V2)
Division operator.
Definition: Vec2.h:530
Tfloat dist(Vec2< T > V1, const Vec2< T > V2)
Compute the euclidian distance between two vectors.
Definition: Vec2.h:497
T dist2(const Vec2< T > V1, const Vec2< T > V2)
Compute the squared euclidian distance between two vectors.
Definition: Vec2.h:483
Vec2< T > operator-(Vec2< T > V1, const Vec2< T > V2)
Substraction operator.
Definition: Vec2.h:524
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
Generic 2D vector [specializations iVec2, fVec2, dVec2].
Definition: Vec2.h:64
void operator+=(const Vec2 V)
Add another vector to this one.
Definition: Vec2.h:173
Tfloat invnorm_fast() const
Compute the inverse of the euclidian norm of the vector.
Definition: Vec2.h:321
Vec2 operator-() const
unary negation operator
Definition: Vec2.h:251
T x
'x' coordinate (first dimension)
Definition: Vec2.h:72
Vec2< T > getRotate90() const
Return the vector obtained after rotation by +90 degree (anti-clockise).
Definition: Vec2.h:406
void operator/=(const Vec2 V)
Divide this vector by another one (coordinate by coordinate division).
Definition: Vec2.h:203
void operator+=(const T &v)
scalar addition.
Definition: Vec2.h:213
Tfloat norm() const
Compute the euclidian norm of the vector.
Definition: Vec2.h:277
void operator*=(const T &v)
scalar multiplication.
Definition: Vec2.h:232
void print(Stream &outputStream=Serial) const
Print a representation of the vector using a given stream object.
Definition: Vec2.h:442
void normalize_fast()
Normalise the vector so that its norm is 1 (do nothing if the vector is 0).
Definition: Vec2.h:352
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
bool operator>=(const Vec2 V) const
Greater-than-or-equal comparison operator.
Definition: Vec2.h:164
T y
'y' coordinate (second dimension)
Definition: Vec2.h:73
Tfloat invnorm() const
Compute the inverse of the euclidian norm of the vector.
Definition: Vec2.h:306
void rotate90()
Rotate this vector by +90 degree (anti-clockise).
Definition: Vec2.h:397
Vec2(const Vec2 &v)=default
Default copy constructor.
bool operator<=(const Vec2 V) const
Less-than-or-equal comparison operator.
Definition: Vec2.h:146
void operator/=(const T &v)
scalar division.
Definition: Vec2.h:241
Vec2< T > getNormalize() const
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec2.h:368
T norm2() const
Compute the squared euclidian norm of the vector.
Definition: Vec2.h:263
Tfloat norm_fast() const
Compute the euclidian norm of the vector.
Definition: Vec2.h:292
Vec2< T > getNormalize_fast() const
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec2.h:385
Vec2()
default constructor: the vector content is undefined.
Definition: Vec2.h:79
bool operator==(const Vec2 V) const
Equality comparator.
Definition: Vec2.h:119
void operator-=(const T &v)
scalar substraction.
Definition: Vec2.h:223
bool operator!=(const Vec2 V) const
Inequality operator.
Definition: Vec2.h:128
void operator-=(const Vec2 V)
Substract another vector from this one.
Definition: Vec2.h:183
Vec2 & operator=(const Vec2 &V)=default
Default assignment operator.
void normalize()
Normalise the vector so that its norm is 1 (do nothing if the vector is 0).
Definition: Vec2.h:335
bool operator<(const Vec2 V) const
Less-than comparison operator.
Definition: Vec2.h:137
constexpr Vec2(T X, T Y)
Constructor with explicit initialization.
Definition: Vec2.h:85
void operator*=(const Vec2 V)
Multiply this vector by another one (coordinate by coordinate multiplication).
Definition: Vec2.h:193
bool operator>(const Vec2 V) const
Greater-than comparison operator.
Definition: Vec2.h:155
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