// =================================================================== // $Id: vector2.cpp,v 1.3 2005/11/30 15:46:50 havran Exp $ // // vector2.cpp // CVector2D class implements an 2D vector // // Licence: the use and distribution of this file is severely limited, please // see the file 'doc/Licence.txt'. Any non-authorized use can be prosecuted under // International Law. For further questions, please, e-mail to VHavran@seznam.cz // or mail to Vlastimil Havran, Pohodli 27, 57001 Litomysl, the Czech Republic. // REPLACEMENT_STRING // // Initial coding by Jiri Bittner // GOLEM headers #include "Vector2.h" namespace GtpVisibilityPreprocessor { // Overload << operator for C++-style output ostream& operator<< (ostream &s, const Vector2 &A) { return s << "(" << A.x() << ", " << A.y() << ")"; } // Overload >> operator for C++-style input istream& operator>> (istream &s, Vector2 &A) { char a; // read "(x, y)" return s >> a >> A.x() >> a >> A.y() >> a; } // Precompute which. which is 0 if the normal is dominant // in the X direction, 1 if the Y direction. int Vector2::DominantAxis() { return (fabs(x()) > fabs(y())) ? 0 : 1; } void Vector2::ExtractVerts(float *p, int dominant) const { if (dominant == 0) *p = y(); else *p = x(); } float Vector2::Normalize() { float s = Size(); if (s != 0.0) { xx /= s; yy /= s; } return s; } Vector2 Vector2::operator-(const Vector2 &v) const { Vector2 u; u.SetX(x()-v.x()); u.SetY(y()-v.y()); return u; } Vector2 Vector2::operator+(const Vector2 &v) const { Vector2 u; u.SetX(x()+v.x()); u.SetY(y()+v.y()); return u; } Vector2 Vector2::operator*(const float t) const { Vector2 u; u.SetX(t*x()); u.SetY(t*y()); return u; } int Vector2::Equal(const Vector2 &u, float trash) const { return ( (fabs(x() - u.x()) < trash) && (fabs(y() - u.y()) < trash) ); } // get the angle between two vectors in range 0 - PI float Vector2::Angle(const Vector2 &v) const { float cosine; cosine = DotProd(*this, v) / (Size() * v.Size()); return acos(cosine); } float Vector2::Cosine(const Vector2 &v) const { return DotProd(*this, v) / (Size() * v.Size()); } // cosine assuming that this vector is normalized float Vector2::CosineN(const Vector2 &v) const { return DotProd(*this, v) / v.Size(); } // if a given vector is smaller in one of coordinates than this, update Vector2& Vector2::UpdateMin(const Vector2 &v) { if (x() > v.x()) SetX(v.x()); if (y() > v.y()) SetY(v.y()); return *this; } Vector2& Vector2::UpdateMax(const Vector2 &v) { if ( x() < v.x() ) SetX(v.x()); if ( y() < v.y() ) SetY(v.y()); return *this; } }