173 lines
3.7 KiB
C++
173 lines
3.7 KiB
C++
|
|
#include <stdlib.h> /* rand() */
|
|
#include <math.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include "Vector.h"
|
|
|
|
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::randomVector()
|
|
{
|
|
double x, y, z;
|
|
do
|
|
{
|
|
x = 2.0 * rand() / RAND_MAX - 1.0;
|
|
y = 2.0 * rand() / RAND_MAX - 1.0;
|
|
z = 2.0 * rand() / RAND_MAX - 1.0;
|
|
} while (x*x + y*y + z*z > 1.0);
|
|
return Vector(x, y, z).normalize();
|
|
}
|
|
|
|
Vector & Vector::normalize()
|
|
{
|
|
double length = mag();
|
|
m_array[0] /= length;
|
|
m_array[1] /= length;
|
|
m_array[2] /= length;
|
|
return *this;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
Vector Vector::getPerpendicular() const
|
|
{
|
|
Vector t = *this;
|
|
t.normalize();
|
|
Vector p = t * Vector(0, 0, 1);
|
|
if (p.mag() <= 0.1)
|
|
{
|
|
p = t * Vector(1, 0, 0);
|
|
}
|
|
return p;
|
|
}
|
|
|
|
std::ostream & operator<<(std::ostream & out, const Vector & v)
|
|
{
|
|
out << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]";
|
|
return out;
|
|
}
|
|
|
|
Vector Vector::operator-() const
|
|
{
|
|
Vector result;
|
|
result[0] = -m_array[0];
|
|
result[1] = -m_array[1];
|
|
result[2] = -m_array[2];
|
|
return result;
|
|
}
|
|
|
|
/* Compute the dot-product of two vectors */
|
|
double Vector::operator%(const Vector & v2) const
|
|
{
|
|
return m_array[0] * v2.m_array[0]
|
|
+ m_array[1] * v2.m_array[1]
|
|
+ m_array[2] * v2.m_array[2];
|
|
}
|
|
|
|
/* Compute the cross-product of two vectors */
|
|
Vector Vector::operator*(const Vector & v2) const
|
|
{
|
|
Vector result;
|
|
result[0] = m_array[1] * v2.m_array[2] - m_array[2] * v2.m_array[1];
|
|
result[1] = m_array[2] * v2.m_array[0] - m_array[0] * v2.m_array[2];
|
|
result[2] = m_array[0] * v2.m_array[1] - m_array[1] * v2.m_array[0];
|
|
return result;
|
|
}
|
|
|
|
Vector Vector::operator+(const Vector & v2) const
|
|
{
|
|
Vector result;
|
|
result[0] = m_array[0] + v2.m_array[0];
|
|
result[1] = m_array[1] + v2.m_array[1];
|
|
result[2] = m_array[2] + v2.m_array[2];
|
|
return result;
|
|
}
|
|
|
|
Vector Vector::operator-(const Vector & v2) const
|
|
{
|
|
Vector result;
|
|
result[0] = m_array[0] - v2.m_array[0];
|
|
result[1] = m_array[1] - v2.m_array[1];
|
|
result[2] = m_array[2] - v2.m_array[2];
|
|
return result;
|
|
}
|
|
|
|
Vector Vector::operator*(double scale) const
|
|
{
|
|
Vector result;
|
|
result[0] = m_array[0] * scale;
|
|
result[1] = m_array[1] * scale;
|
|
result[2] = m_array[2] * scale;
|
|
return result;
|
|
}
|
|
|
|
Vector Vector::operator/(double scale) const
|
|
{
|
|
Vector result;
|
|
result[0] = m_array[0] / scale;
|
|
result[1] = m_array[0] / scale;
|
|
result[2] = m_array[0] / scale;
|
|
return result;
|
|
}
|
|
|
|
Vector & Vector::operator+=(const Vector & v2)
|
|
{
|
|
m_array[0] += v2.m_array[0];
|
|
m_array[1] += v2.m_array[1];
|
|
m_array[2] += v2.m_array[2];
|
|
return *this;
|
|
}
|
|
|
|
Vector & Vector::operator-=(const Vector & v2)
|
|
{
|
|
m_array[0] -= v2.m_array[0];
|
|
m_array[1] -= v2.m_array[1];
|
|
m_array[2] -= v2.m_array[2];
|
|
return *this;
|
|
}
|