#include "Vector.h" #include #include Vector::Vector() { m_array[0] = 0.0; m_array[1] = 0.0; m_array[2] = 0.0; } Vector::Vector(double x, double y, double z) { m_array[0] = x; m_array[1] = y; m_array[2] = z; } Vector & Vector::normalize() { double length = mag(); m_array[0] /= length; m_array[1] /= length; m_array[2] /= length; return *this; } Vector Vector::operator-() const { Vector result; result[0] = -m_array[0]; result[1] = -m_array[1]; result[2] = -m_array[2]; return result; } double Vector::mag() const { return sqrt(m_array[0] * m_array[0] + m_array[1] * m_array[1] + m_array[2] * m_array[2]); } double Vector::mag2() const { return m_array[0] * m_array[0] + m_array[1] * m_array[1] + m_array[2] * m_array[2]; } double Vector::dist_to(const Vector & other) const { return (other - *this).mag(); } Vector Vector::proj(const Vector & target) const { Vector target_normalized = target; target_normalized.normalize(); return target_normalized * ((*this) % target_normalized); } Vector Vector::reflect(const Vector & target) const { Vector projected = proj(target); Vector me_to_proj = projected - (*this); return (*this) + me_to_proj * 2.0; } std::ostream & operator<<(std::ostream & out, const Vector & v) { out << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]"; return out; } /* Compute the dot-product of two vectors */ double operator%(const Vector & v1, const Vector & v2) { return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; } /* Compute the cross-product of two vectors */ Vector operator*(const Vector & v1, const Vector & v2) { Vector result; result[0] = v1[1] * v2[2] - v1[2] * v2[1]; result[1] = v1[2] * v2[0] - v1[0] * v2[2]; result[2] = v1[0] * v2[1] - v1[1] * v2[0]; return result; } Vector operator+(const Vector & v1, const Vector & v2) { Vector result; result[0] = v1[0] + v2[0]; result[1] = v1[1] + v2[1]; result[2] = v1[2] + v2[2]; return result; } Vector operator-(const Vector & v1, const Vector & v2) { Vector result; result[0] = v1[0] - v2[0]; result[1] = v1[1] - v2[1]; result[2] = v1[2] - v2[2]; return result; } Vector operator*(const Vector & v1, double scale) { Vector result = v1; result[0] *= scale; result[1] *= scale; result[2] *= scale; return result; } Vector operator/(const Vector & v1, double scale) { Vector result = v1; result[0] /= scale; result[1] /= scale; result[2] /= scale; return result; }