Advertisement
Guest User

Untitled

a guest
Aug 13th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. struct vec2{
  2. float x, y;
  3.  
  4. vec2(float x, float y):
  5. x(x),
  6. y(y){}
  7.  
  8. vec2():
  9. x(0.0f),
  10. y(0.0f){}
  11.  
  12. vec2(float f):
  13. x(f),
  14. y(f){}
  15.  
  16. vec2(const vec2 &v):
  17. x(v.x),
  18. y(v.y){}
  19.  
  20. float operator[](const int v){
  21. if(v == 0){
  22. return x;
  23. }
  24. else{
  25. return y;
  26. }
  27. }
  28.  
  29. vec2 operator=(const vec2 v){
  30. if (this == &v) {
  31. return *this;
  32. }
  33.  
  34. x = v.x;
  35. y = v.y;
  36.  
  37. return *this;
  38. }
  39.  
  40. vec2 operator+(const vec2 v){
  41. return vec2(x+v.x, y+v.y);
  42. }
  43.  
  44. vec2 operator+(const float s){
  45. return vec2(x+s, y+s);
  46. }
  47.  
  48. vec2 operator-(const vec2 v){
  49. return vec2(x-v.x, y-v.y);
  50. }
  51.  
  52. vec2 operator-(const float s){
  53. return vec2(x-s, y-s);
  54. }
  55.  
  56. vec2 operator*(const vec2 &v){
  57. return vec2(x*v.x, y*v.y);
  58. }
  59.  
  60. vec2 operator*(const float s){
  61. return vec2(x*s, y*s);
  62. }
  63.  
  64. vec2 operator/(const vec2 &v){
  65. return vec2(x/v.x, y/v.y);
  66. }
  67.  
  68. vec2 operator/(const float s){
  69. return vec2(x/s, y/s);
  70. }
  71.  
  72. vec2 operator+=(const vec2 v){
  73. x += v.x;
  74. y += v.y;
  75.  
  76. return *this;
  77. }
  78.  
  79. vec2 operator+=(const float s){
  80. x += s;
  81. y += s;
  82.  
  83. return *this;
  84. }
  85.  
  86. vec2 operator-=(const vec2 v){
  87. x -= v.x;
  88. y -= v.y;
  89.  
  90. return *this;
  91. }
  92.  
  93. vec2 operator-=(const float s){
  94. x -= s;
  95. y -= s;
  96.  
  97. return *this;
  98. }
  99.  
  100. vec2 operator*=(const vec2 v){
  101. x *= v.x;
  102. y *= v.y;
  103.  
  104. return *this;
  105. }
  106.  
  107. vec2 operator*=(const float s){
  108. x *= s;
  109. y *= s;
  110.  
  111. return *this;
  112. }
  113.  
  114. vec2 operator/=(const vec2 v){
  115. x /= v.x;
  116. y /= v.y;
  117.  
  118. return *this;
  119. }
  120.  
  121. vec2 operator/=(const float s){
  122. x /= s;
  123. y /= s;
  124.  
  125. return *this;
  126. }
  127.  
  128. float length(){
  129. return sqrtf(dot());
  130. }
  131.  
  132. float dot(){
  133. return x*x+y*y;
  134. }
  135.  
  136. float dot(const vec2 v){
  137. return x*v.x+y*v.y;
  138. }
  139.  
  140. void normalize(){
  141. float l = length();
  142.  
  143. x /= l;
  144. y /= l;
  145. }
  146. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement