daily pastebin goal
90%
SHARE
TWEET

Untitled

a guest Feb 17th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "vector.h"
  2. #include <math.h>
  3.  
  4. Vector::Vector() { dx = dy = dz = 0.0f; }
  5.  
  6. Vector::Vector(float v_dx, float v_dy, float v_dz) {
  7.   dx = v_dx;
  8.   dy = v_dy;
  9.   dz = v_dz;
  10. }
  11.  
  12. Vector::~Vector() {}
  13.  
  14. float Vector::operator[](int i) const {
  15.   switch (i) {
  16.   case 0:
  17.     return dx; // dx is equivalent to index 0
  18.   case 1:
  19.     return dy; // dy is equivalent to index 1
  20.   case 2:
  21.     return dz; // dz is equivalent to index 2
  22.   default:
  23.     std::cout << "Error: . . . .";
  24.     return dx;
  25.   }
  26. }
  27.  
  28. float Vector::norm() const {
  29.     float x_squared = dx*dx;
  30.     float y_squared = dy*dy;
  31.     float z_squared = dz*dz;
  32.     return sqrt(x_squared+y_squared+z_squared);
  33. }
  34.  
  35. const Vector Vector::normalised() const {
  36.   float vector_norm = this->norm();
  37.  
  38.   return Vector(dx/vector_norm, dy/vector_norm, dz/vector_norm);
  39. }
  40.  
  41. float Vector::dot(const Vector &v) const { //dot product
  42.   return dx*v.dx + dy*v.dy + dz*v.dz;
  43. }
  44.  
  45. const Vector Vector::cross(const Vector &v) const {
  46.   //a2b3-a3b1
  47.   float x_cross = dy*v.dz-dz*v.dy;
  48.   //a3b1-a1b3
  49.   float y_cross = dz*v.dx-dx*v.dz;
  50.   //a1b2-a2b1
  51.   float z_cross = dx*v.dy-dy*v.dx;
  52.  
  53.   return Vector(x_cross, y_cross, z_cross);
  54. }
  55.  
  56. // C++ print routine
  57. std::ostream &operator<<(std::ostream &ostr, Vector v) {
  58.   ostr << "[" << v.dx << ", " << v.dy << ", " << v.dz << "]";
  59.   return ostr;
  60. }
  61.  
  62. // Unary Minuse
  63. const Vector operator-(const Vector &v) {
  64.   return Vector(-v.dx, -v.dy, -v.dz);
  65. }
  66.  
  67. // Addition
  68. const Point operator+(const Point &p, const Vector &v) {
  69.   return Point(p.x+v.dx, p.y+v.dy, p.z+v.dz);
  70. }
  71.  
  72. const Vector operator+(const Vector &v, const Vector &w) {
  73.   return Vector(v.dx+w.dx, v.dy+w.dy, v.dz+w.dz);
  74. }
  75.  
  76. // Subtraction
  77. const Point operator-(Point p, Vector v) {
  78.     return Point(p.x-v.dx, p.y-v.dy, p.z-v.dz);
  79. }
  80.  
  81. const Vector operator-(Vector v, Vector w) {
  82.    return Vector(v.dx-w.dx, v.dy-w.dy, v.dz-w.dz);
  83. }
  84.  
  85. const Vector operator-(const Point &tail, const Point &head) {
  86.   return Vector(head.x-tail.x, head.y-tail.y, head.z-tail.z);
  87. }
  88.  
  89. // Scalar Multiplication
  90. const Vector operator*(float s, const Vector &v) {
  91.   return Vector(s*v.dx, s*v.dy, s*v.dz);
  92. }
  93.  
  94. const Vector operator*(const Vector &v, float s) {
  95.   return Vector(v.dx*s, v.dy*s, v.dz*s);
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top