Guest User

Untitled

a guest
Dec 28th, 2010
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #ifndef VECTOR3_H
  2. #define VECTOR3_H
  3.  
  4. #include <math.h>
  5.  
  6. class Vector3
  7. {
  8. public:
  9. Vector3(float X = 0.0f, float Y = 0.0f, float Z = 0.0f)
  10. {
  11. x = X;
  12. y = Y;
  13. z = Z;
  14. }
  15.  
  16. Vector3 operator+=(const Vector3 &vec)
  17. {
  18. return (*this = (*this + vec) );
  19. }
  20.  
  21. Vector3 operator+(const Vector3 &vec)
  22. {
  23. return Vector3(vec.x + x, vec.y + y, vec.z + z);
  24. }
  25.  
  26. Vector3 operator-=(const Vector3 &vec)
  27. {
  28. return (*this = (*this - vec) );
  29. }
  30.  
  31. Vector3 operator-(const Vector3 &vec)
  32. {
  33. return Vector3(x - vec.x, y - vec.y, z - vec.z);
  34. }
  35.  
  36. Vector3 operator*=(float num)
  37. {
  38. return (*this = (*this * num) );
  39. }
  40.  
  41. Vector3 operator*(float num)
  42. {
  43. return Vector3(x * num, y * num, z * num);
  44. }
  45.  
  46. Vector3 operator/=(float num)
  47. {
  48. return (*this = (*this / num) );
  49. }
  50.  
  51. Vector3 operator/(float num)
  52. {
  53. return Vector3(x / num, y / num, z / num);
  54. }
  55.  
  56. Vector3 operator-(void)
  57. {
  58. //invert direction of vector
  59. return Vector3(-x, -y, -z);
  60. }
  61.  
  62. float Dot(Vector3 &vec)
  63. {
  64. return (x * vec.x + y * vec.y + z * vec.z);
  65. }
  66.  
  67. Vector3 operator*(const Vector3 &vec)
  68. {
  69. //cross product
  70. return Vector3( y * vec.z - z * vec.y,
  71. z * vec.x - x * vec.z,
  72. x * vec.y - y * vec.x );
  73. }
  74.  
  75. float Length(void)
  76. {
  77. //length of vector
  78. return sqrt( x * x + y * y + z * z);
  79. }
  80.  
  81. Vector3 Normalize(void)
  82. {
  83. float length = Length();
  84. x /= length;
  85. y /= length;
  86. z /= length;
  87.  
  88. return *this;
  89. }
  90.  
  91. float Distance(Vector3 &vec)
  92. {
  93. //find distance between two separate vectors
  94. float disX = vec.x - x;
  95. float disY = vec.y - y;
  96. float disZ = vec.z - z;
  97.  
  98. return sqrt(disX * disX + disY * disY + disZ * disZ);
  99. }
  100.  
  101. bool operator==(Vector3 &vec)
  102. {
  103. return (vec.x == x && vec.y == y && vec.z == z);
  104. }
  105.  
  106. bool operator!=(Vector3 &vec)
  107. {
  108. return !(vec == *this);
  109. }
  110.  
  111. public:
  112. float x, y, z;
  113. };
  114.  
  115. #endif
Advertisement
Add Comment
Please, Sign In to add comment