00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _BASE_VECTOR2_
00026 #define _BASE_VECTOR2_
00027
00028 #include <iostream>
00029 #include <math.h>
00030
00031 #include <base/base>
00032 #include <base/array>
00033 #include <base/consts>
00034
00035
00036 namespace base {
00037
00038
00039 class Vector2
00040 {
00041
00042 public:
00043 Vector2() { x=y=Real(0); }
00044 Vector2(Real xc, Real yc) { x=xc; y=yc; }
00045 Vector2(const Vector2& v) { this->operator=(v); }
00046 ~Vector2() {}
00047
00048 enum Coords { X=1, Y=2, Coords2D=3 };
00049
00050 void setZero() throw()
00051 { x=y=Real(0); }
00052
00053 Real& e(Int i) throw()
00054 { return (i==1)?x:y; }
00055
00056 const Real& e(Int i) const throw()
00057 { return (i==1)?x:y; }
00058
00059 Real& at(Int i) throw(std::out_of_range)
00060 {
00061 if ((i < 1) || (i > 2))
00062 throw std::out_of_range(Exception("vector index out of bounds"));
00063
00064 return (i==1)?x:y;
00065 }
00066
00067 const Real& at(Int i) const throw(std::out_of_range)
00068 {
00069 if ((i < 1) || (i > 2))
00070 throw std::out_of_range(Exception("vector index out of bounds"));
00071
00072 return (i==1)?x:y;
00073 }
00074
00075
00076 Vector2& operator=(const Vector2& src) throw()
00077 {
00078 if (&src != this) {
00079 x=src.x; y=src.y;
00080 }
00081 return *this;
00082 }
00083
00084
00085 bool operator==(const Vector2& v) const throw()
00086 {
00087 if (&v == this) return true;
00088
00089 return ((x==v.x)&&(y==v.y));
00090 }
00091
00092 bool equals(const Vector2& v, Real epsilon = consts::epsilon) const throw()
00093 {
00094 if (&v == this) return true;
00095
00096 return (base::equals(x,v.x,epsilon) && base::equals(y,v.y,epsilon));
00097 }
00098
00099
00100 Real magnitude() const throw()
00101 { return sqrt(x*x+y*y); }
00102
00103 Real length() const throw()
00104 { return magnitude(); }
00105
00106 Vector2& negate() throw()
00107 { x=-x; y=-y; return *this; }
00108
00109 Vector2& normalize()
00110 { Real l=length(); x/=l; y/=l; return *this; }
00111
00112 Real magNormalize()
00113 { Real l=length(); x/=l; y/=l; return l; }
00114
00115 Real dot(const Vector2& p) const throw()
00116 { return x*p.x+y*p.y; }
00117
00118
00119 Vector2& operator+=(const Vector2& v2) throw()
00120 { x+=v2.x; y+=v2.y; return *this; }
00121
00122 Vector2& operator-=(const Vector2& v2) throw()
00123 { x-=v2.x; y-=v2.y; return *this; }
00124
00125 Vector2& operator*=(const Real& s) throw()
00126 { x*=s; y*=s; return *this; }
00127
00128 Vector2& operator/=(const Real& s)
00129 { x/=s; y/=s; return *this; }
00130
00131 const Real* c_array() const { return &x; }
00132 Real* c_array() { return &x; }
00133
00134
00135 Real x,y;
00136
00137 };
00138
00139
00140
00141
00142 inline Vector2 operator+(const Vector2& v1, const Vector2& v2) throw()
00143 { Vector2 r(v1); return r+=v2; }
00144
00145 inline Vector2 operator-(const Vector2& v1, const Vector2& v2) throw()
00146 { Vector2 r(v1); return r-=v2; }
00147
00148 inline Vector2 operator-(const Vector2& v1) throw()
00149 { Vector2 r(v1); return -r; }
00150
00151 inline Vector2 operator*(const Vector2& v1, const Real& s) throw()
00152 { Vector2 r(v1); return r*=s; }
00153
00154 inline Vector2 operator*(const Real& s, const Vector2& v1) throw()
00155 { Vector2 r(v1); return r*=s; }
00156
00157 inline Vector2 operator/(const Vector2& v1, const Real& s)
00158 { Vector2 r(v1); return r/= s; }
00159
00160 inline std::ostream& operator<<(std::ostream& out, const Vector2& v)
00161 { return out << "(" << v.x << "," << v.y << ")"; }
00162
00163
00164 inline Vector2 midpoint(const Vector2& v1, const Vector2& v2) throw()
00165 { Vector2 r(v1); r+=v2; return r/=2; }
00166
00167 inline Real dot(const Vector2& v1, const Vector2& v2) throw()
00168 { Vector2 r(v1); return r.dot(v2); }
00169
00170 inline Real operator*(const Vector2& v1, const Vector2& v2)
00171 { return dot(v1,v2); }
00172
00173
00174
00175 }
00176
00177 #endif