fork download
  1. template <class T> struct RVector2 {
  2. public:
  3. typedef T TElement;
  4.  
  5. template <int x> struct ExpandingSwizzler2 {
  6. struct {
  7. T v[2];
  8. };
  9.  
  10. ExpandingSwizzler2<x>& operator=(const T& right) {
  11. v[0] = right;
  12. v[1] = right;
  13. return *this;
  14. }
  15.  
  16. operator RVector2<T> () const {
  17. RVector2<T> r = { v[x], v[x] };
  18. return r;
  19. }
  20. };
  21.  
  22. template <int x, int y> struct Swizzler2 {
  23. struct {
  24. T v[2];
  25. };
  26.  
  27. Swizzler2<x, y>& operator= (const RVector2<T>& right) {
  28. T ex = right.x;
  29. T ey = right.y;
  30. v[x] = ex;
  31. v[y] = ey;
  32. return *this;
  33. }
  34.  
  35. operator RVector2<T> () const {
  36. RVector2<T> r = { v[x], v[y] };
  37. return r;
  38. }
  39. };
  40.  
  41. union {
  42.  
  43. struct {
  44. T x, y;
  45. };
  46.  
  47. struct {
  48. T u, v;
  49. };
  50.  
  51. T cell[2];
  52.  
  53. ExpandingSwizzler2<0> xx;
  54. ExpandingSwizzler2<1> yy;
  55. Swizzler2<0, 1> xy;
  56. Swizzler2<1, 0> yx;
  57.  
  58. ExpandingSwizzler2<0> uu;
  59. ExpandingSwizzler2<1> vv;
  60. Swizzler2<0, 1> uv;
  61. Swizzler2<1, 0> vu;
  62. };
  63.  
  64. T DistanceToSquared (const RVector2<T>& to) const {
  65. T tx = to.x - x;
  66. T ty = to.y - y;
  67. return tx * tx + ty * ty;
  68. }
  69.  
  70. RVector2<T> Direction (const RVector2<T>& to) const {
  71. RVector2<T> direction = to - *this;
  72. direction.Normalize();
  73. return direction;
  74. }
  75.  
  76. T Dot (const RVector2<T>& right) const {
  77. return x * right.x + y * right.y;
  78. }
  79.  
  80. T Cross (const RVector2<T>& right) const {
  81. return x * right.y - right.x * y;
  82. }
  83.  
  84. int Quadrant () const {
  85. int quad = 0;
  86. quad |= ((x >= 0));
  87. quad |= ((y >= 0) << 1);
  88. return quad;
  89. }
  90.  
  91. int Quadrant (const RVector2<T>& origin) const {
  92. int quad = 0;
  93. quad |= ((x >= origin.x));
  94. quad |= ((y >= origin.y) << 1);
  95. return quad;
  96. }
  97.  
  98. bool QuadrantChange ( const RVector2<T>& after, const RVector2<T>& origin ) const {
  99. RVector2<T> bOrigin = *this - origin;
  100. RVector2<T> aOrigin = after - origin;
  101.  
  102. return ( !( (aOrigin.x > 0) == (bOrigin.x > 0) ) ||
  103. !( (aOrigin.y > 0) == (bOrigin.y > 0) ) )
  104. // Quadrant has changed
  105. ;
  106. }
  107.  
  108. bool QuadrantChange ( const RVector2<T>& after ) const {
  109. return ( !( (after.x > 0) == (x > 0) ) ||
  110. !( (after.y > 0) == (y > 0) ) )
  111. // Quadrant has changed
  112. ;
  113. }
  114.  
  115. bool IsUnit () const {
  116. return ( x * x + y * y ) == 1;
  117. }
  118.  
  119. RVector2<T>& operator= (const RVector2<T>& right) {
  120. x = right.x;
  121. y = right.y;
  122. return *this;
  123. }
  124.  
  125. RVector2<T>& operator-= (const RVector2<T>& right) {
  126. x -= right.x;
  127. y -= right.y;
  128. return *this;
  129. }
  130.  
  131. RVector2<T>& operator+= (const RVector2<T>& right) {
  132. x += right.x;
  133. y += right.y;
  134. return *this;
  135. }
  136.  
  137. RVector2<T>& operator*= (const RVector2<T>& right) {
  138. x *= right.x;
  139. y *= right.y;
  140. return *this;
  141. }
  142.  
  143. RVector2<T>& operator/= (const RVector2<T>& right) {
  144. x /= right.x;
  145. y /= right.y;
  146. return *this;
  147. }
  148.  
  149. RVector2<T>& operator*= (const T& right) {
  150. x *= right;
  151. y *= right;
  152. return *this;
  153. }
  154.  
  155. RVector2<T>& operator/= (const T& right) {
  156. T invright = 1 / right;
  157. x *= invright;
  158. y *= invright;
  159. return *this;
  160. }
  161.  
  162. RVector2<T>& operator++ () {
  163. ++x;
  164. ++y;
  165. return *this;
  166. }
  167.  
  168. RVector2<T>& operator-- () {
  169. --x;
  170. --y;
  171. return *this;
  172. }
  173.  
  174. RVector2<T> operator++ (int) {
  175. RVector2<T> r = {x++, y++};
  176. return r;
  177. }
  178.  
  179. RVector2<T> operator-- (int) {
  180. RVector2<T> r = {x--, y--};
  181. return r;
  182. }
  183.  
  184. RVector2<T> operator- (const RVector2<T>& right) const {
  185. RVector2<T> r = {x - right.x, y - right.y};
  186. return r;
  187. }
  188.  
  189. RVector2<T> operator+ (const RVector2<T>& right) const {
  190. RVector2<T> r = {x + right.x, y + right.y};
  191. return r;
  192. }
  193.  
  194. RVector2<T> operator* (const RVector2<T>& right) const {
  195. RVector2<T> r = {x * right.x, y * right.y};
  196. return r;
  197. }
  198.  
  199. RVector2<T> operator/ (const RVector2<T>& right) const {
  200. RVector2<T> r = {x / right.x, y / right.y};
  201. return r;
  202. }
  203.  
  204. RVector2<T> operator* (const T& right) const {
  205. RVector2<T> r = {x * right, y * right};
  206. return r;
  207. }
  208.  
  209. RVector2<T> operator/ (const T& right) const {
  210. T invright = 1 / right;
  211. RVector2<T> r = {x * invright, y * invright};
  212. return r;
  213. }
  214.  
  215. RVector2<T> operator+ () const {
  216. RVector2<T> r = {+x, +y};
  217. return r;
  218. }
  219.  
  220. RVector2<T> operator- () const {
  221. RVector2<T> r = {-x, -y};
  222. return r;
  223. }
  224.  
  225. bool operator== (const RVector2<T>& right) const {
  226. return right.x == x && right.y == y;
  227. }
  228.  
  229. bool operator!= (const RVector2<T>& right) const {
  230. return right.x != x || right.y != y;
  231. }
  232.  
  233. operator T* () {
  234. return (T*)cell;
  235. }
  236.  
  237. operator T* () const {
  238. return (T*)cell;
  239. }
  240.  
  241. T& operator[] (int index) {
  242. return cell[index];
  243. }
  244.  
  245. T& operator[] (int index) const {
  246. return *(T*)( cell + index );
  247. }
  248. };
  249.  
  250. int main (int argc, int argv) {
  251. RVector2<float> arf = { 1.0f, 5.0f };
  252. arf *= 2;
  253. /// Muahahaha, aaahahahahaaaaaaaaaaa!
  254. return 0;
  255. }
Success #stdin #stdout 0s 2880KB
stdin
Standard input is empty
stdout
Standard output is empty