Advertisement
Guest User

vec3.cpp

a guest
Mar 15th, 2011
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. /*
  2.  * File:   vec3.cpp
  3.  * Author: peixinho
  4.  *
  5.  * Created on March 15, 2011, 10:19 AM
  6.  */
  7.  
  8. #include "vec3.h"
  9.  
  10. namespace SelFish3D {
  11.  
  12. vec3::vec3() {
  13.     x = y = z = 0;
  14. }
  15.  
  16. vec3::vec3(const float& x, const float& y, const float& z)
  17. {
  18.     this->x = x;    this->y = y;    this->z = z;
  19. }
  20.  
  21. float vec3::dotProduct(const vec3& v) const
  22. {
  23.     return (x*v.x) + (y*v.y) + (z*v.z);
  24. }
  25.  
  26. float vec3::magnitude() const
  27. {
  28.     return sqrt(magnitudeSQR());
  29. }
  30.  
  31. float vec3::magnitudeSQR() const
  32. {
  33.     return x*x + y*y + z*z;
  34. }
  35.  
  36. float vec3::distance(const vec3& v) const
  37. {
  38.     vec3 dist = this->operator -(v);
  39.     return dist.magnitude();
  40. }
  41.  
  42. float vec3::distanceSQR(const vec3& v) const
  43. {
  44.     vec3 dist = this->operator -(v);
  45.     return dist.magnitudeSQR();
  46. }
  47.  
  48. vec3 vec3::cross(const vec3& v) const
  49. {
  50.     vec3 vcross;
  51.     vcross.x = y*v.z-z*v.y;
  52.     vcross.y = z*v.x-x*v.z;
  53.     vcross.z = x*v.y-y*v.x;
  54.     return vcross;
  55. }
  56.  
  57. vec3 vec3::normalize() const
  58. {
  59.     float m = magnitude();
  60.     if (m==0) m=1;
  61.     return vec3(x/m, y/m, z/m);
  62. }
  63.  
  64. vec3 vec3::clone() const
  65. {
  66.     return vec3(x,y,z);
  67. }
  68.  
  69. vec3 vec3::negate() const
  70. {
  71.     return vec3(-x,-y,-z);
  72. }
  73.  
  74. vec3 vec3::operator+(const vec3 &v) const
  75. {
  76.     return vec3(x+v.x,y+v.y,z+v.z);
  77. }
  78. vec3 vec3::operator-(const vec3 &v) const
  79. {
  80.     return vec3(x-v.x,y-v.y,z-v.z);
  81. }
  82. vec3 vec3::operator*(const vec3 &v) const
  83. {
  84.     return vec3(x*v.x,y*v.y,z*v.z);
  85. }
  86. vec3 vec3::operator/(const vec3 &v) const
  87. {
  88.     return vec3(x/v.x,y/v.y,z/v.z);
  89. }
  90. vec3 vec3::operator+(const float &f) const
  91. {
  92.     return vec3(x+f,y+f,z+f);
  93. }
  94. vec3 vec3::operator-(const float &f) const
  95. {
  96.     return vec3(x-f,y-f,z-f);
  97. }
  98. vec3 vec3::operator*(const float &f) const
  99. {
  100.     return vec3(x*f,y*f,z*f);
  101. }
  102. vec3 vec3::operator/(const float &f) const
  103. {
  104.     return vec3(x/f,y/f,z/f);
  105. }
  106. void vec3::operator+=(const vec3 &v)
  107. {
  108.     x+=v.x; y+=v.y; z+=v.z;
  109. }
  110. void vec3::operator-=(const vec3 &v)
  111. {
  112.     x-=v.x; y-=v.y; z-=v.z;
  113. }
  114. void vec3::operator*=(const vec3 &v)
  115. {
  116.     x*=v.x; y*=v.y; z*=v.z;
  117. }
  118. void vec3::operator/=(const vec3 &v)
  119. {
  120.     x/=v.x; y/=v.y; z/=v.z;
  121. }
  122. void vec3::operator+=(const float &f)
  123. {
  124.     x+=f; y+=f; z+=f;
  125. }
  126. void vec3::operator-=(const float &f)
  127. {
  128.     x-=f; y-=f; z-=f;
  129. }
  130. void vec3::operator*=(const float &f)
  131. {
  132.     x*=f; y*=f; z*=f;
  133. }
  134. void vec3::operator/=(const float &f)
  135. {
  136.     x/=f; y/=f; z/=f;
  137. }
  138. bool vec3::operator==(const vec3 &v) const
  139. {
  140.     return (x==v.x && y==v.y && z==v.z);
  141. }
  142.  
  143. string vec3::toString() const
  144. {
  145.     ostringstream xString; xString << x;
  146.     ostringstream yString; yString << y;
  147.     ostringstream zString; zString << z;
  148.     return "Vector3(" + xString.str() + "," + yString.str() + "," + zString.str() + ")";
  149. }
  150.  
  151. vec3::~vec3() {
  152. }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement