// =================================================================== // $Id: vector2.h,v 1.5 2005/11/02 12:58:48 xkrivanj Exp $ // // vector2.h // Header file for CVector2D class - implements 2-dimensional vector // // Class: CVector2D // // 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 #ifndef __VECTOR2_H__ #define __VECTOR2_H__ // #include "basmacr.h" //#include "basmath.h" #include "common.h" #include using namespace std; namespace GtpVisibilityPreprocessor { // --------------------------------------------------------------------------- // This class describes two-dimensional vector. // --------------------------------------------------------------------------- class Vector2 { public: float xx, yy; // coordinates of the vector // constructors Vector2(float xs, float ys):xx(xs), yy(ys) {} // default constructor Vector2() {} friend ostream& operator<< (ostream &s, const Vector2 &A); friend istream& operator>> (istream &s, Vector2 &A); const float& x() const { return xx; } const float& y() const { return yy; } float& x() {return xx;} float& y() {return yy;} // Functions to get at the vector components float& operator[] (int inx) { if (inx == 0) return xx; else return yy; } const float& operator[] (int inx) const { if (inx == 0) return xx; else return yy; } // returns x-coordinate for which==0, x-coordinate for which==1 void ExtractVerts(float *p, int which) const; float& SetX(const float x) {return xx = x;} float& SetY(const float y) {return yy = y;} Vector2& operator=(Vector2 const &v) { xx = v.x(); yy = v.y(); return *this; } void SetValue(float xs, float ys ) { xx = xs; yy = ys; } float Size() const { return (float) sqrt(xx*xx + yy*yy);} // returns the squared magnitude of a vector friend inline float SqrMagnitude(const Vector2 &v); // returns the squared distance between two vectors friend inline float SqrDistance(const Vector2 &v1, const Vector2 &v2); // normalize the vector to the size=1.0 float Normalize(); // Assignment operators Vector2& operator+= (const Vector2 &a); Vector2& operator-= (const Vector2 &a); Vector2& operator*= (const Vector2 &a); Vector2& operator*= (float a); Vector2& operator/= (float a); Vector2 operator-(const Vector2 &) const; Vector2 operator+(const Vector2 &) const; Vector2 operator*(const float t) const; // Vector2 operator*(const Matrix3C &mat) const; Vector2 operator-() const { return Vector2(-x(),-y()); } int operator==(const Vector2 &u) const {return Equal(u,Limits::Small);} int operator!=(const Vector2 &u) const {return !operator == (u);} int Equal(const Vector2 &u,float trash) const; // dot product of the two vectors friend inline double DotProd(const Vector2 &u, const Vector2 &v) { return ( u.x() * v.x() + u.y() * v.y() ); } // the angle between two vectors $\in <0, PI>$ float Angle(const Vector2 &v) const; // cosine of the angle between the two vectors $\in <-1,1>$ float Cosine(const Vector2 &v) const; // supposes this vector is normalized float CosineN(const Vector2 &v) const; // checks if the this vector is not opposite to a given vector int IsOpposite(const Vector2 &v) const { return (Abs(DotProd(*this,v) + Size() * v.Size()) < Limits::Small); } // computes the distance between this and a given vector float Distance(const Vector2 &v) const { return (float) sqrt( sqr(xx-v.x()) + sqr(yy - v.y())); } // computes the squared distance between this and a given vector float SqrDistance(const Vector2 &v) const { return sqr(x()-v.x())+sqr(y()-v.y()); } // if a given vector has a smaller(larger) coordinate, then this is updated Vector2& UpdateMin(const Vector2 &v); Vector2& UpdateMax(const Vector2 &v); // checks if both coordinates are smaller than a given one int operator<=(Vector2 &v) { return x() < (v.x() + Limits::Small) && y() < (v.y()+Limits::Small); } // checks if both coordinates are larger than a given one int operator>=(Vector2 &v) { return x() > (v.x()-Limits::Small) && y() > (v.y()-Limits::Small); } // returns which coordinates has larger size int DominantAxis(); }; const Vector2 ZeroVector2(0,0); inline Vector2& Vector2::operator+= (const Vector2 &a) { xx += a.xx; yy += a.yy; return *this; } inline Vector2& Vector2::operator-= (const Vector2 &a) { xx -= a.xx; yy -= a.yy; return *this; } inline Vector2& Vector2::operator*= (float a) { xx *= a; yy *= a; return *this; } inline Vector2& Vector2::operator/= (float a) { xx /= a; yy /= a; return *this; } inline Vector2& Vector2::operator*= (const Vector2 &a) { xx *= a.xx; yy *= a.yy; return *this; } inline float SqrMagnitude(const Vector2 &v) { return v.x() * v.x() + v.y() * v.y(); } inline float SqrDistance(const Vector2 &v1, const Vector2 &v2) { return sqr(v1.xx - v2.xx) + sqr(v1.yy - v2.yy); } } #endif // __VECTOR2_H__