Advertisement
Guest User

Untitled

a guest
Nov 26th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. //////////////
  2. // Vector.h //
  3. //////////////
  4.  
  5. #include <math.h>
  6. #include "Main.h"
  7.  
  8. typedef float Scalar;
  9. typedef bool  Bool;
  10.  
  11. class Vector3
  12. {
  13. public:
  14.     Scalar X;
  15.     Scalar Y;
  16.     Scalar Z;
  17.  
  18.     Vector3();
  19.     Vector3(Scalar X, Scalar Y, Scalar Z);
  20.  
  21.     void Invert();
  22.     void Normalize();
  23.     void CrossProduct(const Vector3& a, const Vector3& b);
  24.        
  25.     Vector3&    operator+=(const Vector3& Vector);
  26.     Vector3&    operator-=(const Vector3& Vector);
  27.     Vector3&    operator*=(const Vector3& Vector);
  28.     Vector3&    operator/=(const Vector3& Vector);
  29.  
  30.     Bool        operator==(const Scalar& Compare) const;
  31.     Bool        operator==(const Vector3& Vector) const;
  32.     Bool        operator!=(const Vector3& Vector) const;
  33. };
  34.  
  35. inline Vector3 operator+(const Vector3& a, const Vector3& b)
  36. {
  37.     return Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
  38. }
  39.  
  40. inline Vector3 operator-(const Vector3& a, const Vector3& b)
  41. {
  42.     return Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
  43. }
  44.  
  45. inline Vector3 operator*(const Vector3& a, const Vector3& b)
  46. {
  47.     return Vector3(a.X * b.X, a.Y * b.Y, a.Z * b.Z);
  48. }
  49.  
  50. inline Vector3 operator/(const Vector3& a, const Vector3& b)
  51. {
  52.     return Vector3(a.X / b.X, a.Y / b.Y, a.Z / b.Z);
  53. }
  54.  
  55. inline Vector3 operator*(const Vector3& v, const float* m)
  56. {
  57.     static Vector3 Result;
  58.    
  59.     Result.X = m[0] * v.X + m[4] * v.Y + m[8]  * v.Z + m[12];
  60.     Result.Y = m[1] * v.X + m[5] * v.Y + m[9]  * v.Z + m[13];
  61.     Result.Z = m[2] * v.X + m[6] * v.Y + m[10] * v.Z + m[14];
  62.     return Result;
  63. }
  64.  
  65. ////////////////
  66. // Vector.cpp //
  67. ////////////////
  68.  
  69. #include "Vector.h"
  70.  
  71. Vector3::Vector3()
  72. {
  73.     X = 0.0f;
  74.     Y = 0.0f;
  75.     Z = 0.0f;
  76. }
  77.  
  78. Vector3::Vector3(Scalar X, Scalar Y, Scalar Z)
  79. {
  80.     this->X = X;
  81.     this->Y = Y;
  82.     this->Z = Z;
  83. }
  84.  
  85. void Vector3::Invert()
  86. {
  87.     this->X = -this->X;
  88.     this->Y = -this->Y;
  89.     this->Z = -this->Z;
  90. }
  91.  
  92. void Vector3::Normalize()
  93. {
  94.     Scalar Magnitude = sqrt(this->X * this->X + this->Y * this->Y + this->Z * this->Z);
  95.     if(Magnitude == 0.0f) return;
  96.  
  97.     this->X /= Magnitude;
  98.     this->Y /= Magnitude;
  99.     this->Z /= Magnitude;
  100. }
  101.  
  102. void Vector3::CrossProduct(const Vector3& a, const Vector3& b)
  103. {
  104.     this->X = a.Y * b.Z - a.Z * b.Y;
  105.     this->Y = a.Z * b.X - a.X * b.Z; // -a.X * b.Z + b.X * a.Z
  106.     this->Z = a.X * b.Y - a.Y * b.X;
  107. }
  108.  
  109. Vector3& Vector3::operator+=(const Vector3& Vector)
  110. {
  111.     this->X += Vector.X;
  112.     this->Y += Vector.Y;
  113.     this->Z += Vector.Z;
  114.     return *this;
  115. }
  116.  
  117. Vector3& Vector3::operator-=(const Vector3& Vector)
  118. {
  119.     this->X -= Vector.X;
  120.     this->Y -= Vector.Y;
  121.     this->Z -= Vector.Z;
  122.     return *this;
  123. }
  124.  
  125. Vector3& Vector3::operator*=(const Vector3& Vector)
  126. {
  127.     this->X *= Vector.X;
  128.     this->Y *= Vector.Y;
  129.     this->Z *= Vector.Z;
  130.     return *this;
  131. }
  132.  
  133. Vector3& Vector3::operator/=(const Vector3& Vector)
  134. {
  135.     this->X /= Vector.X;
  136.     this->Y /= Vector.Y;
  137.     this->Z /= Vector.Z;
  138.     return *this;
  139. }
  140.  
  141. Bool Vector3::operator==(const Scalar& Compare) const
  142. {
  143.     return (FloatCloseEnough(this->X, Compare) && FloatCloseEnough(this->Y, Compare) && FloatCloseEnough(this->Z, Compare));
  144. }
  145.  
  146. Bool Vector3::operator==(const Vector3& Vector) const
  147. {
  148.     return (FloatCloseEnough(this->X, Vector.X) && FloatCloseEnough(this->Y, Vector.Y) && FloatCloseEnough(this->Z, Vector.Z));
  149. }
  150.  
  151. Bool Vector3::operator!=(const Vector3& Vector) const
  152. {
  153.     return !(*this == Vector);
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement