updated some Vector operations and made them member functions
git-svn-id: svn://anubis/fart/trunk@276 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
cb4c84ee66
commit
9546c8dde4
@ -41,15 +41,6 @@ Vector & Vector::normalize()
|
||||
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]
|
||||
@ -101,54 +92,81 @@ std::ostream & operator<<(std::ostream & out, const Vector & v)
|
||||
return out;
|
||||
}
|
||||
|
||||
/* Compute the dot-product of two vectors */
|
||||
double operator%(const Vector & v1, const Vector & v2)
|
||||
Vector Vector::operator-() const
|
||||
{
|
||||
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
|
||||
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 operator*(const Vector & v1, const Vector & v2)
|
||||
Vector Vector::operator*(const Vector & v2) const
|
||||
{
|
||||
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];
|
||||
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 operator+(const Vector & v1, const Vector & v2)
|
||||
Vector Vector::operator+(const Vector & v2) const
|
||||
{
|
||||
Vector result;
|
||||
result[0] = v1[0] + v2[0];
|
||||
result[1] = v1[1] + v2[1];
|
||||
result[2] = v1[2] + v2[2];
|
||||
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 operator-(const Vector & v1, const Vector & v2)
|
||||
Vector Vector::operator-(const Vector & v2) const
|
||||
{
|
||||
Vector result;
|
||||
result[0] = v1[0] - v2[0];
|
||||
result[1] = v1[1] - v2[1];
|
||||
result[2] = v1[2] - v2[2];
|
||||
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 operator*(const Vector & v1, double scale)
|
||||
Vector Vector::operator*(double scale) const
|
||||
{
|
||||
Vector result = v1;
|
||||
result[0] *= scale;
|
||||
result[1] *= scale;
|
||||
result[2] *= scale;
|
||||
Vector result;
|
||||
result[0] = m_array[0] * scale;
|
||||
result[1] = m_array[1] * scale;
|
||||
result[2] = m_array[2] * scale;
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector operator/(const Vector & v1, double scale)
|
||||
Vector Vector::operator/(double scale) const
|
||||
{
|
||||
Vector result = v1;
|
||||
result[0] /= scale;
|
||||
result[1] /= scale;
|
||||
result[2] /= scale;
|
||||
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;
|
||||
}
|
||||
|
@ -12,7 +12,6 @@ class Vector
|
||||
static Vector randomVector();
|
||||
double & operator[](int idx) { return m_array[idx]; }
|
||||
double operator[](int idx) const { return m_array[idx]; }
|
||||
Vector operator-() const;
|
||||
Vector & normalize();
|
||||
double mag() const;
|
||||
double mag2() const;
|
||||
@ -21,17 +20,21 @@ class Vector
|
||||
Vector reflect(const Vector & target) const;
|
||||
Vector getPerpendicular() const;
|
||||
|
||||
Vector operator-() const;
|
||||
double operator%(const Vector & v2) const;
|
||||
Vector operator*(const Vector & v2) const;
|
||||
Vector operator+(const Vector & v2) const;
|
||||
Vector operator-(const Vector & v2) const;
|
||||
Vector operator*(double scale) const;
|
||||
Vector operator/(double scale) const;
|
||||
Vector & operator+=(const Vector & v2);
|
||||
Vector & operator-=(const Vector & v2);
|
||||
|
||||
protected:
|
||||
double m_array[3];
|
||||
};
|
||||
|
||||
std::ostream & operator<<(std::ostream & out, const Vector & v);
|
||||
double operator%(const Vector & v1, const Vector & v2);
|
||||
Vector operator*(const Vector & v1, const Vector & v2);
|
||||
Vector operator+(const Vector & v1, const Vector & v2);
|
||||
Vector operator-(const Vector & v1, const Vector & v2);
|
||||
Vector operator*(const Vector & v1, double scale);
|
||||
Vector operator/(const Vector & v1, double scale);
|
||||
static inline Vector operator*(double d, const Vector & v) { return v * d; }
|
||||
static inline Vector operator/(double d, const Vector & v) { return v / d; }
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user