Vec2.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef Vec2_H
  2. #define Vec2_H
  3. #include "FrameworkMath.h"
  4. namespace Framework
  5. {
  6. template< typename T >
  7. class Vec2
  8. {
  9. public:
  10. T x;
  11. T y;
  12. // Konstruktor
  13. inline Vec2()
  14. {
  15. }
  16. inline Vec2( T x, T y )
  17. : x( x ),
  18. y( y )
  19. {
  20. }
  21. inline Vec2( const Vec2 &vect )
  22. : x( (T)vect.x ),
  23. y( (T)vect.y )
  24. {
  25. }
  26. // nicht constant
  27. inline Vec2 &normalize()
  28. {
  29. const T länge = län();
  30. x /= länge;
  31. y /= länge;
  32. return *this;
  33. }
  34. inline Vec2 &CCW90()
  35. {
  36. T temp = y;
  37. y = -x;
  38. x = temp;
  39. return *this;
  40. }
  41. inline Vec2 &CW90()
  42. {
  43. T temp = y;
  44. y = x;
  45. x = -temp;
  46. return *this;
  47. }
  48. inline Vec2 &Swap( Vec2 &vect )
  49. {
  50. const Vec2 tmp = vect;
  51. vect = *this;
  52. *this = tmp;
  53. return *this;
  54. }
  55. inline Vec2 operator=( const Vec2 &r )
  56. {
  57. x = r.x;
  58. y = r.y;
  59. return *this;
  60. }
  61. inline Vec2 operator+=( const Vec2 &r )
  62. {
  63. x += r.x;
  64. y += r.y;
  65. return *this;
  66. }
  67. inline Vec2 operator-=( const Vec2 &r )
  68. {
  69. x -= r.x;
  70. y -= r.y;
  71. return *this;
  72. }
  73. inline Vec2 operator*=( const T &r )
  74. {
  75. x *= r;
  76. y *= r;
  77. return *this;
  78. }
  79. inline Vec2 operator/=( const T &r )
  80. {
  81. x /= r;
  82. y /= r;
  83. return *this;
  84. }
  85. // constant
  86. inline Vec2 operator-( ) const
  87. {
  88. return Vec2< T >( -x, -y );
  89. }
  90. template< typename T2 >
  91. inline operator Vec2< T2 >() const
  92. {
  93. return Vec2< T2 >( (T2)x, (T2)y );
  94. }
  95. inline T länSq() const
  96. {
  97. return *this * *this;
  98. }
  99. inline T län() const
  100. {
  101. return sqrt( länSq() );
  102. }
  103. inline T operator*( const Vec2 &r ) const
  104. {
  105. return x * r.x + y * r.y;
  106. }
  107. inline Vec2 operator+( const Vec2 &r ) const
  108. {
  109. return Vec2( *this ) += r;
  110. }
  111. inline Vec2 operator-( const Vec2 &r ) const
  112. {
  113. return Vec2( *this ) -= r;
  114. }
  115. inline Vec2 operator*( const T &r ) const
  116. {
  117. return Vec2( *this ) *= r;
  118. }
  119. inline Vec2 operator/( const T &r ) const
  120. {
  121. return Vec2( *this ) /= r;
  122. }
  123. inline bool istInRegion( const Vec2 &p1, const Vec2 &p2 ) const
  124. {
  125. const T medianX = (T)( ( p1.x + p2.x ) / 2.0 );
  126. const T medianY = (T)( ( p1.y + p2.y ) / 2.0 );
  127. return abs< T >( medianX - x ) <= abs< T >( medianX - p1.x ) &&
  128. abs< T >( medianY - y ) <= abs< T >( medianY - p1.y );
  129. }
  130. inline bool operator==( const Vec2 &r ) const
  131. {
  132. return x == r.x && y == r.y;
  133. }
  134. inline bool operator!=( const Vec2 &r ) const
  135. {
  136. return !( *this == r );
  137. }
  138. inline Vec2 mittelpunktMit( const Vec2 &p2 ) const
  139. {
  140. return Vec2( (T)( ( x + p2.x ) / 2.0 ), (T)( ( y + p2.y ) / 2.0 ) );
  141. }
  142. inline Vec2 rotation( const float angle ) const
  143. {
  144. Vec2 result;
  145. float cosine = cosf( angle );
  146. float sine = sinf( angle );
  147. result.x = (T)( x * cosine - y * sine );
  148. result.y = (T)( x * sine + y * cosine );
  149. return result;
  150. }
  151. };
  152. }
  153. #endif