Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/Vector2

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Vector2 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.4 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
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 // Operations
00141 
00142 inline Vector2 operator+(const Vector2& v1, const Vector2& v2) throw() // Addition
00143 { Vector2 r(v1); return r+=v2; }
00144 
00145 inline Vector2 operator-(const Vector2& v1, const Vector2& v2) throw() // Subtraction
00146 { Vector2 r(v1); return r-=v2; }
00147 
00148 inline Vector2 operator-(const Vector2& v1) throw() // Negation
00149 { Vector2 r(v1); return -r; }
00150 
00151 inline Vector2 operator*(const Vector2& v1, const Real& s) throw() // Scalar multiplication
00152 { Vector2 r(v1); return r*=s; }
00153 
00154 inline Vector2 operator*(const Real& s, const Vector2& v1) throw() // Scalar multiplication
00155 { Vector2 r(v1); return r*=s; }
00156 
00157 inline Vector2 operator/(const Vector2& v1, const Real& s) // Scalar division
00158 { Vector2 r(v1); return r/= s; }
00159 
00160 inline std::ostream& operator<<(std::ostream& out, const Vector2& v) // Output
00161 { return out << "(" << v.x << "," << v.y << ")"; }
00162 
00163 
00164 inline Vector2 midpoint(const Vector2& v1, const Vector2& v2) throw() // midpoint between
00165 { Vector2 r(v1); r+=v2; return r/=2; }
00166 
00167 inline Real dot(const Vector2& v1, const Vector2& v2) throw() // dot product v1.v2
00168 { Vector2 r(v1); return r.dot(v2); }
00169 
00170 inline Real operator*(const Vector2& v1, const Vector2& v2) // dot procuct v1.v2
00171 { return dot(v1,v2); }
00172 
00173 
00174 
00175 } // base
00176 
00177 #endif

Generated on Thu Jul 29 15:56:16 2004 for OpenSim by doxygen 1.3.6