SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLVec2< T > Class Template Reference

2D vector template class for standard 2D vector algebra. More...

#include <SLVec2.h>

Public Member Functions

 SLVec2 ()
 
 SLVec2 (const T X, const T Y)
 
 SLVec2 (const T v[2])
 
 SLVec2 (const SLVec2 &v)
 
void set (const T X, const T Y)
 
void set (const T v[2])
 
void set (const SLVec2 &v)
 
SLbool operator== (const SLVec2 &v) const
 
SLbool operator!= (const SLVec2 &v) const
 
SLbool operator<= (const SLVec2 &v) const
 
SLbool operator>= (const SLVec2 &v) const
 
SLbool operator< (const SLVec2 &v) const
 
SLbool operator> (const SLVec2 &v) const
 
SLbool operator<= (const T v) const
 
SLbool operator>= (const T v) const
 
SLbool operator< (const T v) const
 
SLbool operator> (const T v) const
 
SLVec2 operator+ (const SLVec2 &v) const
 
SLVec2 operator- (const SLVec2 &v) const
 
SLVec2 operator- () const
 
operator* (const SLVec2 &v) const
 
SLVec2 operator* (const T s) const
 
SLVec2 operator/ (const T s) const
 
SLVec2 operator& (const SLVec2 &v) const
 
SLVec2operator= (const SLVec2 &v)
 
SLVec2operator+= (const SLVec2 &v)
 
SLVec2operator-= (const SLVec2 &v)
 
SLVec2operator*= (const T s)
 
SLVec2operator/= (const T s)
 
SLVec2operator&= (const SLVec2 &v)
 
void add (const SLVec2 &a, const SLVec2 &b)
 
void sub (const SLVec2 &a, const SLVec2 &b)
 
void scale (const T s)
 
dot (const SLVec2 &v)
 
length () const
 
lengthSqr () const
 
SLVec2normalize ()
 
void clampMinMax (const T min, const T max)
 
void toPolar (T &r, T &phiRAD)
 Calculates polar coords with radius & angle phi in radians (-pi < phi < pi) More...
 
void fromPolar (T r, T phiRAD)
 Calculates the vector from polar coords r & phi in radians (-pi < phi < pi) More...
 
SLVec2 barycentricCoords (SLVec2 A, SLVec2 B, SLVec2 C)
 Calculate the barycentric coordinate uv of the point within a triangle ABC. More...
 
diff (const SLVec2 &v)
 Calculate the absolute to the vector v. More...
 
void setMin (const SLVec2 &v)
 
void setMax (const SLVec2 &v)
 
SLbool isZero ()
 
void print (const char *str=nullptr)
 
SLstring toString (SLstring delimiter=", ", int decimals=2)
 Conversion to string. More...
 

Public Attributes

union {
   struct {
      T   x
 
      T   y
 
   } 
 
   struct {
      T   comp [2]
 
   } 
 
}; 
 

Static Public Attributes

static SLVec2 ZERO = SLVec2<T>(0,0)
 

Friends

SLVec2 operator* (T s, const SLVec2 &v)
 
std::ostream & operator<< (std::ostream &output, const SLVec2 &v)
 

Detailed Description

template<class T>
class SLVec2< T >

2D vector template class for standard 2D vector algebra.

2D vector template class and type definitions for 2D vectors;
Use SLVec2f for a specific float type vector
Use SLVec2d for a specific double type vector
Use SLVec2r for a precision independent real type.

Definition at line 26 of file SLVec2.h.

Constructor & Destructor Documentation

◆ SLVec2() [1/4]

template<class T >
SLVec2< T >::SLVec2 ( )
inline

Definition at line 34 of file SLVec2.h.

34 {x=0;y=0;}
T y
Definition: SLVec2.h:30
T x
Definition: SLVec2.h:30

◆ SLVec2() [2/4]

template<class T >
SLVec2< T >::SLVec2 ( const T  X,
const T  Y 
)
inline

Definition at line 35 of file SLVec2.h.

36  {x=X;y=Y;}

◆ SLVec2() [3/4]

template<class T >
SLVec2< T >::SLVec2 ( const T  v[2])
inline

Definition at line 37 of file SLVec2.h.

37 {x=v[0]; y=v[1];}

◆ SLVec2() [4/4]

template<class T >
SLVec2< T >::SLVec2 ( const SLVec2< T > &  v)
inline

Definition at line 38 of file SLVec2.h.

38 {x=v.x; y=v.y;}

Member Function Documentation

◆ add()

template<class T >
void SLVec2< T >::add ( const SLVec2< T > &  a,
const SLVec2< T > &  b 
)
inline

Definition at line 81 of file SLVec2.h.

82  {x=a.x+b.x; y=a.y+b.y;}

◆ barycentricCoords()

template<class T >
SLVec2 SLVec2< T >::barycentricCoords ( SLVec2< T >  A,
SLVec2< T >  B,
SLVec2< T >  C 
)
inline

Calculate the barycentric coordinate uv of the point within a triangle ABC.

Definition at line 105 of file SLVec2.h.

106  {
107  // Formula from: https://en.wikipedia.org/wiki/Barycentric_coordinate_system
108  T xAC = A.x-C.x, xC = x-C.x, xCB = C.x-B.x;
109  T yAC = A.y-C.y, yBC = B.y-C.y, yC = y-C.y, yCA = C.y-A.y;
110 
111  T u = (yBC*xC + xCB*yC) / (yBC*xAC + xCB*yAC);
112  T v = (yCA*xC + xAC*yC) / (yBC*xAC + xCB*yAC);
113 
114  return SLVec2(u,v);
115  }
SLVec2()
Definition: SLVec2.h:34

◆ clampMinMax()

template<class T >
void SLVec2< T >::clampMinMax ( const T  min,
const T  max 
)
inline

Definition at line 92 of file SLVec2.h.

93  {x = (x>max)?max : (x<min)?min : x;
94  y = (y>max)?max : (y<min)?min : y;}

◆ diff()

template<class T >
T SLVec2< T >::diff ( const SLVec2< T > &  v)
inline

Calculate the absolute to the vector v.

Definition at line 118 of file SLVec2.h.

118  {return Utils::abs(x-v.x) +
119  Utils::abs(y-v.y);}
T abs(T a)
Definition: Utils.h:249

◆ dot()

template<class T >
T SLVec2< T >::dot ( const SLVec2< T > &  v)
inline

Definition at line 86 of file SLVec2.h.

86 {return x*v.x+y*v.y;}

◆ fromPolar()

template<class T >
void SLVec2< T >::fromPolar ( r,
phiRAD 
)
inline

Calculates the vector from polar coords r & phi in radians (-pi < phi < pi)

Definition at line 101 of file SLVec2.h.

101  {x = r * cos(phiRAD);
102  y = r * sin(phiRAD);}

◆ isZero()

template<class T >
SLbool SLVec2< T >::isZero ( )
inline

Definition at line 124 of file SLVec2.h.

124 {return (x==0 && y==0);}

◆ length()

template<class T >
T SLVec2< T >::length ( ) const
inline

Definition at line 87 of file SLVec2.h.

87 {return ((T)sqrt(x*x+y*y));}

◆ lengthSqr()

template<class T >
T SLVec2< T >::lengthSqr ( ) const
inline

Definition at line 88 of file SLVec2.h.

88 {return (x*x+y*y); }

◆ normalize()

template<class T >
SLVec2& SLVec2< T >::normalize ( )
inline

Definition at line 89 of file SLVec2.h.

89  {T L=length();
90  if (L>0) {x/=L; y/=L;}
91  return *this;}
T length() const
Definition: SLVec2.h:87

◆ operator!=()

template<class T >
SLbool SLVec2< T >::operator!= ( const SLVec2< T > &  v) const
inline

Definition at line 47 of file SLVec2.h.

47 {return (x!=v.x || y!=v.y);}

◆ operator&()

template<class T >
SLVec2 SLVec2< T >::operator& ( const SLVec2< T > &  v) const
inline

Definition at line 64 of file SLVec2.h.

64 {return SLVec2(x*v.x, y*v.y);}

◆ operator&=()

template<class T >
SLVec2& SLVec2< T >::operator&= ( const SLVec2< T > &  v)
inline

Definition at line 74 of file SLVec2.h.

74 {x*=v.x; y*=v.y; return *this;}

◆ operator*() [1/2]

template<class T >
T SLVec2< T >::operator* ( const SLVec2< T > &  v) const
inline

Definition at line 61 of file SLVec2.h.

61 {return x*v.x+y*v.y;} //dot

◆ operator*() [2/2]

template<class T >
SLVec2 SLVec2< T >::operator* ( const T  s) const
inline

Definition at line 62 of file SLVec2.h.

62 {return SLVec2(x*s, y*s);}
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47

◆ operator*=()

template<class T >
SLVec2& SLVec2< T >::operator*= ( const T  s)
inline

Definition at line 72 of file SLVec2.h.

72 {x*=s; y*=s; return *this;}
SLScene * s
Definition: SLScene.h:31

◆ operator+()

template<class T >
SLVec2 SLVec2< T >::operator+ ( const SLVec2< T > &  v) const
inline

Definition at line 58 of file SLVec2.h.

58 {return SLVec2(x+v.x, y+v.y);}

◆ operator+=()

template<class T >
SLVec2& SLVec2< T >::operator+= ( const SLVec2< T > &  v)
inline

Definition at line 70 of file SLVec2.h.

70 {x+=v.x; y+=v.y; return *this;}

◆ operator-() [1/2]

template<class T >
SLVec2 SLVec2< T >::operator- ( ) const
inline

Definition at line 60 of file SLVec2.h.

60 {return SLVec2(-x, -y);}

◆ operator-() [2/2]

template<class T >
SLVec2 SLVec2< T >::operator- ( const SLVec2< T > &  v) const
inline

Definition at line 59 of file SLVec2.h.

59 {return SLVec2(x-v.x, y-v.y);}

◆ operator-=()

template<class T >
SLVec2& SLVec2< T >::operator-= ( const SLVec2< T > &  v)
inline

Definition at line 71 of file SLVec2.h.

71 {x-=v.x; y-=v.y; return *this;}

◆ operator/()

template<class T >
SLVec2 SLVec2< T >::operator/ ( const T  s) const
inline

Definition at line 63 of file SLVec2.h.

63 {return SLVec2(x/s, y/s);}

◆ operator/=()

template<class T >
SLVec2& SLVec2< T >::operator/= ( const T  s)
inline

Definition at line 73 of file SLVec2.h.

73 {x/=s; y/=s; return *this;}

◆ operator<() [1/2]

template<class T >
SLbool SLVec2< T >::operator< ( const SLVec2< T > &  v) const
inline

Definition at line 50 of file SLVec2.h.

50 {return (x<v.x && y<v.y);}

◆ operator<() [2/2]

template<class T >
SLbool SLVec2< T >::operator< ( const T  v) const
inline

Definition at line 54 of file SLVec2.h.

54 {return (x< v && y< v);}

◆ operator<=() [1/2]

template<class T >
SLbool SLVec2< T >::operator<= ( const SLVec2< T > &  v) const
inline

Definition at line 48 of file SLVec2.h.

48 {return (x<=v.x && y<=v.y);}

◆ operator<=() [2/2]

template<class T >
SLbool SLVec2< T >::operator<= ( const T  v) const
inline

Definition at line 52 of file SLVec2.h.

52 {return (x<=v && y<=v);}

◆ operator=()

template<class T >
SLVec2& SLVec2< T >::operator= ( const SLVec2< T > &  v)
inline

Definition at line 69 of file SLVec2.h.

69 {x=v.x; y=v.y; return *this;}

◆ operator==()

template<class T >
SLbool SLVec2< T >::operator== ( const SLVec2< T > &  v) const
inline

Definition at line 46 of file SLVec2.h.

46 {return (x==v.x && y==v.y);}

◆ operator>() [1/2]

template<class T >
SLbool SLVec2< T >::operator> ( const SLVec2< T > &  v) const
inline

Definition at line 51 of file SLVec2.h.

51 {return (x>v.x && y>v.y);}

◆ operator>() [2/2]

template<class T >
SLbool SLVec2< T >::operator> ( const T  v) const
inline

Definition at line 55 of file SLVec2.h.

55 {return (x> v && y> v);}

◆ operator>=() [1/2]

template<class T >
SLbool SLVec2< T >::operator>= ( const SLVec2< T > &  v) const
inline

Definition at line 49 of file SLVec2.h.

49 {return (x>=v.x && y>=v.y);}

◆ operator>=() [2/2]

template<class T >
SLbool SLVec2< T >::operator>= ( const T  v) const
inline

Definition at line 53 of file SLVec2.h.

53 {return (x>=v && y>=v);}

◆ print()

template<class T >
void SLVec2< T >::print ( const char *  str = nullptr)
inline

Definition at line 125 of file SLVec2.h.

125  {if (str) SLMATH_LOG("%s",str);
126  SLMATH_LOG("% 3.3f, % 3.3f",x, y);}
#define SLMATH_LOG(...)
Definition: SLMath.h:70

◆ scale()

template<class T >
void SLVec2< T >::scale ( const T  s)
inline

Definition at line 85 of file SLVec2.h.

85 {x*=s; y*=s;}

◆ set() [1/3]

template<class T >
void SLVec2< T >::set ( const SLVec2< T > &  v)
inline

Definition at line 43 of file SLVec2.h.

43 {x=v.x; y=v.y;}

◆ set() [2/3]

template<class T >
void SLVec2< T >::set ( const T  v[2])
inline

Definition at line 42 of file SLVec2.h.

42 {x=v[0]; y=v[1];}

◆ set() [3/3]

template<class T >
void SLVec2< T >::set ( const T  X,
const T  Y 
)
inline

Definition at line 40 of file SLVec2.h.

41  {x=X; y=Y;}

◆ setMax()

template<class T >
void SLVec2< T >::setMax ( const SLVec2< T > &  v)
inline

Definition at line 122 of file SLVec2.h.

122  {if (v.x > x) x=v.x;
123  if (v.y > y) y=v.y;}

◆ setMin()

template<class T >
void SLVec2< T >::setMin ( const SLVec2< T > &  v)
inline

Definition at line 120 of file SLVec2.h.

120  {if (v.x < x) x=v.x;
121  if (v.y < y) y=v.y;}

◆ sub()

template<class T >
void SLVec2< T >::sub ( const SLVec2< T > &  a,
const SLVec2< T > &  b 
)
inline

Definition at line 83 of file SLVec2.h.

84  {x=a.x-b.x; y=a.y-b.y;}

◆ toPolar()

template<class T >
void SLVec2< T >::toPolar ( T &  r,
T &  phiRAD 
)
inline

Calculates polar coords with radius & angle phi in radians (-pi < phi < pi)

Definition at line 97 of file SLVec2.h.

97  {r = length();
98  phiRAD = atan2(y,x);}

◆ toString()

template<class T >
SLstring SLVec2< T >::toString ( SLstring  delimiter = ", ",
int  decimals = 2 
)
inline

Conversion to string.

Definition at line 129 of file SLVec2.h.

130  { return Utils::toString(x,decimals) + delimiter +
131  Utils::toString(y,decimals);
132  }
string toString(float f, int roundedDecimals)
Returns a string from a float with max. one trailing zero.
Definition: Utils.cpp:92

Friends And Related Function Documentation

◆ operator*

template<class T >
SLVec2 operator* ( s,
const SLVec2< T > &  v 
)
friend

Definition at line 66 of file SLVec2.h.

66 {return SLVec2(v.x*s, v.y*s);}

◆ operator<<

template<class T >
std::ostream& operator<< ( std::ostream &  output,
const SLVec2< T > &  v 
)
friend

Definition at line 77 of file SLVec2.h.

78  {output<<"["<<v.x<<","<<v.y<<"]"; return output;}

Member Data Documentation

◆ 

union { ... }

◆ comp

template<class T >
T SLVec2< T >::comp[2]

Definition at line 31 of file SLVec2.h.

◆ x

template<class T >
T SLVec2< T >::x

Definition at line 30 of file SLVec2.h.

◆ y

template<class T >
T SLVec2< T >::y

Definition at line 30 of file SLVec2.h.

◆ ZERO

template<class T >
SLVec2< T > SLVec2< T >::ZERO = SLVec2<T>(0,0)
static

Definition at line 135 of file SLVec2.h.


The documentation for this class was generated from the following file: