Skip to content

Instantly share code, notes, and snippets.

@neildanson
Created October 4, 2014 21:11
Show Gist options
  • Save neildanson/2b4cba718d1943f53493 to your computer and use it in GitHub Desktop.
Save neildanson/2b4cba718d1943f53493 to your computer and use it in GitHub Desktop.
#include "Vector.h"
Vector::Vector(double px, double py, double pz) : x(px), y(py), z(pz) {
}
double Vector::Dot(Vector v) {
return (this->x * v.x) + (this->y * v.y) + (this->z * v.z);
}
double Vector::Length(){
return sqrt(this->Dot(*this));
}
Vector Vector::Cross(Vector v) {
return Vector(this->y*v.z - this->z * v.y,
this->z*v.x - this->x * v.z,
this->x*v.y - this->y * v.x);
}
Vector Vector::Add(Vector v) {
return Vector(this->x + v.x, this->y + v.y, this->z + v.z);
}
Vector Vector::Subtract(Vector v) {
return Vector(this->x - v.x, this->y - v.y, this->z - v.z);
}
Vector Vector::Scale(double s) {
return Vector(this->x * s, this->y * s, this->z * s);
}
Vector Vector::Normalize() {
double mag = this->Length();
if (mag == 0.0) {
return this->Scale(INFINITY);
}
else {
return this->Scale(1.0 / mag);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment