Advertisement
Guest User

Vector3f.hpp

a guest
Jun 26th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #ifndef VECTOR3F_HPP
  2. #define VECTOR3F_HPP
  3.  
  4. #include <cmath>
  5.  
  6. class Vector3f
  7. {
  8.     float x, y, z;
  9.     const float pi = 3.141592653589793;
  10.  
  11. public:
  12.     Vector3f()
  13.     {
  14.         x = 0.0;
  15.         y = 0.0;
  16.         z = 0.0;
  17.     }
  18.  
  19.     Vector3f(float _x, float _y, float _z)
  20.         : x(_x)
  21.         , y(_y)
  22.         , z(_z)
  23.     {
  24.     }
  25.  
  26.     Vector3f operator =(Vector3f& other)
  27.     {
  28.         x = other.getX();
  29.         y = other.getY();
  30.         z = other.getZ();
  31.  
  32.         return *this;
  33.     }
  34.  
  35.     float length()
  36.     {
  37.         return float(sqrt(x * x + y * y + z * z));
  38.     }
  39.  
  40.     float dot(Vector3f other)
  41.     {
  42.         return x * other.getX() + y * other.getY() + z * other.getZ();
  43.     }
  44.  
  45.     Vector3f normalize()
  46.     {
  47.         float _length = length();
  48.         x /= _length;
  49.         y /= _length;
  50.         z /= _length;
  51.  
  52.         return *this;
  53.     }
  54.  
  55.     Vector3f operator +(float n)
  56.     {
  57.         return add(n);
  58.     }
  59.  
  60.     Vector3f operator +(Vector3f other)
  61.     {
  62.         return add(other);
  63.     }
  64.  
  65.     Vector3f add(Vector3f other)
  66.     {
  67.         return Vector3f{x + other.getX(), y + other.getY(), z + other.getZ()};
  68.     }
  69.  
  70.     Vector3f add(float n)
  71.     {
  72.         return Vector3f{x + n, y + n, z + n};
  73.     }
  74.  
  75.     Vector3f operator -(float n)
  76.     {
  77.         return sub(n);
  78.     }
  79.  
  80.     Vector3f operator -(Vector3f other)
  81.     {
  82.         return sub(other);
  83.     }
  84.  
  85.     Vector3f sub(Vector3f other)
  86.     {
  87.         return Vector3f{x - other.getX(), y - other.getY(), z - other.getZ()};
  88.     }
  89.  
  90.     Vector3f sub(float n)
  91.     {
  92.         return Vector3f{x - n, y - n, z - n};
  93.     }
  94.  
  95.     Vector3f operator *(float n)
  96.     {
  97.         return mul(n);
  98.     }
  99.  
  100.     Vector3f operator *(Vector3f other)
  101.     {
  102.         return mul(other);
  103.     }
  104.  
  105.     Vector3f mul(Vector3f other)
  106.     {
  107.         return Vector3f{x * other.getX(), y * other.getY(), z * other.getZ()};
  108.     }
  109.  
  110.     Vector3f mul(float n)
  111.     {
  112.         return Vector3f{x * n, y * n, z * n};
  113.     }
  114.  
  115.     Vector3f operator /(float n)
  116.     {
  117.         return div(n);
  118.     }
  119.  
  120.     Vector3f operator /(Vector3f other)
  121.     {
  122.         return div(other);
  123.     }
  124.  
  125.     Vector3f div(Vector3f other)
  126.     {
  127.         return Vector3f{x / other.getX(), y / other.getY(), z / other.getZ()};
  128.     }
  129.  
  130.     Vector3f div(float n)
  131.     {
  132.         return Vector3f{x / n, y / n, z / n};
  133.     }
  134.  
  135.     inline bool operator ==(Vector3f other)
  136.     {
  137.         return x == other.getX() && y == other.getY() && z == other.getZ();
  138.     }
  139.  
  140.     inline bool operator !=(Vector3f other)
  141.     {
  142.         return x != other.getX() && y != other.getY() && z != other.getZ();
  143.     }
  144.  
  145.     Vector3f cross(Vector3f other)
  146.     {
  147.         float _x = y * other.getZ() - z * other.getY();
  148.         float _y = z * other.getX() - x * other.getZ();
  149.         float _z = x * other.getY() - y * other.getX();
  150.  
  151.         return Vector3f{_x, _y, _z};
  152.     }
  153.  
  154.     float getX()
  155.     {
  156.         return x;
  157.     }
  158.  
  159.     float getY()
  160.     {
  161.         return y;
  162.     }
  163.  
  164.     float getZ()
  165.     {
  166.         return z;
  167.     }
  168. };
  169.  
  170. #endif // VECTOR3F_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement