Advertisement
Guest User

Vector3.cpp

a guest
Mar 5th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. //Vector3.cpp by Vassillen M. Chizhov
  2. #include "Vector3.h"
  3.  
  4. namespace myGameMathLib
  5. {
  6.     Vector3 Vector3::Zero(FP(0), FP(0), FP(0));
  7.  
  8.     //ctors
  9.  
  10.     Vector3::Vector3()
  11.         :x(FP(0)), y(FP(0))
  12.     {
  13.  
  14.     }
  15.  
  16.     Vector3::Vector3(FP value)
  17.         : x(value), y(value)
  18.     {
  19.  
  20.     }
  21.  
  22.     Vector3::Vector3(FP x, FP y, FP z)
  23.         : x(x), y(y), z(z)
  24.     {
  25.  
  26.     }
  27.  
  28.     Vector3::Vector3(const Vector3& another)
  29.         : x(another.x), y(another.y), z(another.z)
  30.     {
  31.  
  32.     }
  33.  
  34.  
  35.     //member operators
  36.     Vector3& Vector3::operator=(const Vector3& another)
  37.     {
  38.         x = another.x;
  39.         y = another.y;
  40.         z = another.z;
  41.  
  42.         return *this;
  43.     }
  44.  
  45.     Vector3& Vector3::operator+=(const Vector3& another)
  46.     {
  47.         x += another.x;
  48.         y += another.y;
  49.         z += another.z;
  50.  
  51.         return *this;
  52.     }
  53.  
  54.     Vector3& Vector3::operator-=(const Vector3& another)
  55.     {
  56.         x -= another.x;
  57.         y -= another.y;
  58.         z -= another.z;
  59.  
  60.         return *this;
  61.     }
  62.  
  63.     //componentwise multiplication
  64.     Vector3& Vector3::operator*=(const Vector3& another)
  65.     {
  66.         x *= another.x;
  67.         y *= another.y;
  68.         z *= another.z;
  69.  
  70.         return *this;
  71.     }
  72.  
  73.     //componentwise division
  74.     Vector3& Vector3::operator/=(const Vector3& another)
  75.     {
  76.         x /= another.x;
  77.         y /= another.y;
  78.         z /= another.z;
  79.  
  80.         return *this;
  81.     }
  82.  
  83.     Vector3& Vector3::operator*=(FP scalar)
  84.     {
  85.         x *= scalar;
  86.         y *= scalar;
  87.         z *= scalar;
  88.  
  89.         return *this;
  90.     }
  91.  
  92.     Vector3& Vector3::operator/=(FP scalar)
  93.     {
  94.         x /= scalar;
  95.         y /= scalar;
  96.         z /= scalar;
  97.  
  98.         return *this;
  99.     }
  100.  
  101.     Vector3 Vector3::operator+()
  102.     {
  103.         return *this;
  104.     }
  105.  
  106.     Vector3 Vector3::operator-()
  107.     {
  108.         return Vector3(-x, -y, -z);
  109.     }
  110.  
  111.     FP Vector3::lengthSquared() const
  112.     {
  113.         return (x*x + y*y + z*z);
  114.     }
  115.  
  116.     FP Vector3::length() const
  117.     {
  118.         return sqrtf(x*x + y*y + z*z);
  119.     }
  120.  
  121.     Vector3& Vector3::normalize()
  122.     {
  123.         FP length = sqrt(x*x + y*y + z*z);
  124.         x /= length;
  125.         y /= length;
  126.         z /= length;
  127.  
  128.         return *this;
  129.     }
  130.  
  131.     Vector3& Vector3::reflect(const Vector3& another)
  132.     {
  133.         Vector3 temp(another);
  134.         temp.normalize();
  135.         return (dotProduct(*this, temp)*temp - *this);
  136.     }
  137.  
  138.     Vector3& Vector3::reflectNormalized(const Vector3& another)
  139.     {
  140.         return (dotProduct(*this, another)*another - *this);
  141.     }
  142.  
  143.     Vector3& Vector3::clearTo(FP value)
  144.     {
  145.         x = value;
  146.         y = value;
  147.         z = value;
  148.  
  149.         return *this;
  150.     }
  151.  
  152.     //non-member operators/functions
  153.     //compare
  154.     bool operator==(const Vector3& vec1, const Vector3& vec2)
  155.     {
  156.         return (zero(vec1.x - vec2.x) && zero(vec1.y - vec2.y) && zero(vec1.z-vec2.z));
  157.     }
  158.  
  159.     bool operator!=(const Vector3& vec1, const Vector3& vec2)
  160.     {
  161.         return (!zero(vec1.x - vec2.x) || !zero(vec1.y - vec2.y) || !zero(vec1.z-vec2.z));
  162.     }
  163.  
  164.     //arithmetic
  165.     Vector3 operator+(const Vector3& vec1, const Vector3& vec2)
  166.     {
  167.         return Vector3(vec1.x + vec2.x, vec1.y + vec2.y, vec1.z+vec2.z);
  168.     }
  169.  
  170.     Vector3 operator-(const Vector3& vec1, const Vector3& vec2)
  171.     {
  172.         return Vector3(vec1.x - vec2.x, vec1.y - vec2.y, vec1.z-vec2.z);
  173.     }
  174.  
  175.     //componentwise multiplication
  176.     Vector3 operator*(const Vector3& vec1, const Vector3& vec2)
  177.     {
  178.         return Vector3(vec1.x * vec2.x, vec1.y * vec2.y, vec1.z*vec2.z);
  179.     }
  180.  
  181.     //componentwise division
  182.     Vector3 operator/(const Vector3& vec1, const Vector3& vec2)
  183.     {
  184.         return Vector3(vec1.x / vec2.x, vec1.y / vec2.y, vec1.z/vec2.z);
  185.     }
  186.  
  187.     //scalar multiplication and division
  188.     Vector3 operator*(const Vector3& vec, FP scalar)
  189.     {
  190.         return Vector3(vec.x * scalar, vec.y * scalar, vec.z*scalar);
  191.     }
  192.  
  193.     Vector3 operator*(FP scalar, const Vector3& vec)
  194.     {
  195.         return Vector3(scalar * vec.x, scalar*vec.y, scalar*vec.z);
  196.     }
  197.  
  198.     Vector3 operator/(const Vector3& vec, FP scalar)
  199.     {
  200.         return Vector3(vec.x / scalar, vec.y / scalar, vec.z / scalar);
  201.     }
  202.  
  203.     Vector3 operator/(FP scalar, const Vector3& vec)
  204.     {
  205.         return Vector3(scalar / vec.x, scalar / vec.y, scalar / vec.z);
  206.     }
  207.  
  208.     //dot product
  209.     FP dotProduct(const Vector3& vec1, const Vector3& vec2)
  210.     {
  211.         return (vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z);
  212.     }
  213.  
  214.     //cross Product
  215.     Vector3 crossProduct(const Vector3& vec1, const Vector3& vec2)
  216.     {
  217.         return Vector3(vec1.y*vec2.z - vec1.z*vec2.y, vec1.z*vec2.x - vec1.x*vec2.z, vec1.x*vec2.y - vec1.y*vec2.x);
  218.     }
  219.  
  220.     //length squared and length
  221.     FP lengthSquared(const Vector3& vec)
  222.     {
  223.         return vec.lengthSquared();
  224.     }
  225.  
  226.     FP length(const Vector3& vec)
  227.     {
  228.         //precision? - sqrtf?
  229.         return vec.length();
  230.     }
  231.  
  232.     Vector3& normalize(Vector3& vec)
  233.     {
  234.         return vec.normalize();
  235.     }
  236.  
  237.     Vector3& reflect(Vector3& vec, const Vector3& around)
  238.     {
  239.         return vec.reflect(around);
  240.     }
  241.  
  242.     Vector3& reflectNormalized(Vector3& vec, const Vector3& around)
  243.     {
  244.         return vec.reflectNormalized(around);
  245.     }
  246.  
  247.     Vector3& clearTo(Vector3& vec, FP value)
  248.     {
  249.         vec.clearTo(value);
  250.         return vec;
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement