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

Tools/Math/Vector2.h

Go to the documentation of this file.
00001 /**
00002  * @file Vector2.h
00003  * Contains template class Vector2 of type V
00004  *
00005  * @author <a href="mailto:martin.kallnik@gmx.de" > Martin Kallnik</a>
00006  * @author Max Risler
00007  */
00008 
00009 #ifndef __Vector2_h__
00010 #define __Vector2_h__
00011 
00012 #include <math.h>
00013 #include "Tools/Streams/InOut.h"
00014 
00015 /** This class represents a 2-vector */
00016 template <class V> class Vector2 {
00017   public:
00018   /** The vector values */
00019   V x,y;
00020 
00021   /** Default constructor. */
00022   Vector2<V>():x(0),y(0)
00023   {
00024   }
00025   /** Default constructor. */
00026   Vector2<V>(V x, V y):x(x),y(y)
00027   {
00028   }
00029 
00030   /** Assignment operator
00031   *\param other The other vector that is assigned to this one
00032   *\return A reference to this object after the assignment.
00033   */
00034   Vector2<V>& operator=(const Vector2<V>& other)
00035   {
00036     x=other.x;y=other.y;
00037     return *this;
00038   }
00039 
00040   /** Copy constructor
00041   *\param other The other vector that is copied to this one
00042   */
00043   Vector2<V>(const Vector2<V>& other) {*this = other;}
00044 
00045   /** Addition of another vector to this one.
00046   *\param other The other vector that will be added to this one
00047   *\return A reference to this object after the calculation.
00048   */
00049   Vector2<V>& operator+=(const Vector2<V>& other)
00050   {
00051     x += other.x;
00052     y += other.y;
00053     return *this;
00054   }
00055 
00056   /** Substraction of this vector from another one.
00057   *\param other The other vector this one will be substracted from
00058   *\return A reference to this object after the calculation.
00059   */
00060   Vector2<V>& operator-=(const Vector2<V>& other)
00061   {
00062     x -= other.x;
00063     y -= other.y;
00064     return *this;
00065   }
00066 
00067   /** Multiplication of this vector by a factor.
00068   *\param factor The factor this vector is multiplied by
00069   *\return A reference to this object after the calculation.
00070   */
00071   Vector2<V>& operator*=(const V& factor)
00072   {
00073     x *= factor;
00074     y *= factor;
00075     return *this;
00076   }
00077 
00078   /** Division of this vector by a factor.
00079   *\param factor The factor this vector is divided by
00080   *\return A reference to this object after the calculation.
00081   */
00082   Vector2<V>& operator/=(const V& factor)
00083   {
00084     if (factor == 0) return *this;
00085     x /= factor;
00086     y /= factor;
00087     return *this;
00088   }
00089 
00090   /** Addition of another vector to this one.
00091   *\param other The other vector that will be added to this one
00092   *\return A new object that contains the result of the calculation.
00093   */
00094   Vector2<V> operator+(const Vector2<V>& other) const
00095     {return Vector2<V>(*this) += other;}
00096 
00097   /** Subtraction of another vector to this one.
00098   *\param other The other vector that will be added to this one
00099   *\return A new object that contains the result of the calculation.
00100   */
00101   Vector2<V> operator-(const Vector2<V>& other) const
00102     {return Vector2<V>(*this) -= other;}
00103 
00104   /** Negation of this vector.
00105   *\return A new object that contains the result of the calculation.
00106   */
00107   Vector2<V> operator-() const
00108     {return Vector2<V>() -= *this;}
00109 
00110   /** Inner product of this vector and another one.
00111   *\param other The other vector this one will be multiplied by
00112   *\return The inner product.
00113   */
00114   V operator*(const Vector2<V>& other) const
00115   {
00116     return x * other.x + y * other.y;
00117   }
00118 
00119   /** Multiplication of this vector by a factor.
00120   *\param factor The factor this vector is multiplied by
00121   *\return A new object that contains the result of the calculation.
00122   */
00123   Vector2<V> operator*(const V& factor) const
00124     {return Vector2<V>(*this) *= factor;}
00125 
00126   /** Division of this vector by a factor.
00127   *
00128   *\param factor The factor this vector is divided by
00129   *\return A new object that contains the result of the calculation.
00130   */
00131   Vector2<V> operator/(const V& factor) const
00132     {return Vector2<V>(*this) /= factor;}
00133 
00134   /** Comparison of another vector with this one.
00135   *\param other The other vector that will be compared to this one
00136   *\return Whether the two vectors are equal.
00137   */
00138   bool operator==(const Vector2<V>& other) const
00139   {
00140     return (x==other.x && y==other.y);
00141   }
00142 
00143   /** Comparison of another vector with this one.
00144   *\param other The other vector that will be compared to this one.
00145   *\return Whether the two vectors are unequal.
00146   */
00147   bool operator!=(const Vector2<V>& other) const
00148     {return !(*this == other);}
00149 
00150   /** Calculation of the length of this vector.
00151   *\return The length.
00152   */
00153   V abs() const
00154   {return (V) sqrt(((double)x)*x+((double)y)*y);}
00155 
00156   /** normalize this vector.
00157   *\param len The length, the vector should be normalized to, default=1.
00158   *\return the normalized vector.
00159   */
00160   Vector2<V> normalize(V len)
00161   {
00162     if (abs() == 0) return *this;
00163     return *this = (*this * len) / abs();
00164    }
00165 
00166   /** normalize this vector.
00167   *\return the normalized vector.
00168   */
00169   Vector2<V> normalize()
00170   {
00171     if (abs() == 0) return *this;
00172     return *this /= abs();
00173   }
00174 
00175   /** transpose this vector.
00176   *\return the transposed vector.
00177   */
00178   Vector2<V> transpose()
00179   { V buffer = x;
00180     x = y;
00181     y = buffer;
00182     return *this;
00183   }
00184 
00185   /** the vector is rotated left by 90 degrees.
00186   *\return the rotated vector.
00187   */
00188   Vector2<V> rotateLeft()
00189   { V buffer = -y;
00190     y = x;
00191     x = buffer;
00192     return *this;
00193   }
00194 
00195   /** the vector is rotated right by 90 degrees.
00196   *\return the rotated vector.
00197   */
00198   Vector2<V> rotateRight()
00199   { V buffer = -x;
00200     x = y;
00201     y = buffer;
00202     return *this;
00203   }
00204 
00205   /**
00206   * array-like member access.
00207   * \param i index of coordinate
00208   * \return reference to x or y
00209   */
00210   V& operator[](int i)
00211   {
00212     return  (&x)[i];
00213   }
00214   
00215   /** Calculation of the angle of this vector */
00216   double angle() const
00217   {return atan2((double)y,(double)x);}
00218 };
00219 
00220 /**
00221 * Streaming operator that reads a Vector2<V> from a stream.
00222 * @param stream The stream from which is read.
00223 * @param vector2 The Vector2<V> object.
00224 * @return The stream.
00225 */ 
00226 template <class V> In& operator>>(In& stream, Vector2<V>& vector2)
00227 {
00228   stream >> vector2.x;
00229   stream >> vector2.y;
00230   return stream;
00231 }
00232 
00233 /**
00234 * Streaming operator that writes a Vector2<V> to a stream.
00235 * @param stream The stream to write on.
00236 * @param vector2 The Vector2<V> object.
00237 * @return The stream.
00238 */ 
00239 template <class V> Out& operator<<(Out& stream, const Vector2<V>& vector2)
00240 {
00241   stream << vector2.x;
00242   stream << vector2.y;
00243   return stream;
00244 }
00245 
00246 #endif // __Vector2_h__
00247 
00248 /*
00249 * Change log :
00250 *
00251 * $Log: Vector2.h,v $
00252 * Revision 1.3  2004/09/09 10:15:56  spranger
00253 * fixed doxygen-errors
00254 *
00255 * Revision 1.2  2004/06/14 23:18:40  kindler
00256 * - added [] operator for direct element access
00257 *
00258 * Revision 1.1.1.1  2004/05/22 17:37:14  cvsadm
00259 * created new repository GT2004_WM
00260 *
00261 * Revision 1.3  2004/01/25 14:25:17  roefer
00262 * Missing include added
00263 *
00264 * Revision 1.2  2003/12/02 13:46:00  cesarz
00265 * - added streaming operators
00266 * - added functions rotateLeft() and rotateRight()
00267 *
00268 * Revision 1.1  2003/10/07 10:13:24  cvsadm
00269 * Created GT2004 (M.J.)
00270 *
00271 * Revision 1.1.1.1  2003/07/02 09:40:28  cvsadm
00272 * created new repository for the competitions in Padova from the 
00273 * tamara CVS (Tuesday 2:00 pm)
00274 *
00275 * removed unused solutions
00276 *
00277 * Revision 1.10  2003/04/15 15:52:07  risler
00278 * DDD GO 2003 code integrated
00279 *
00280 * Revision 1.11  2003/04/09 14:50:32  kallnik
00281 * error in normalize(V len) fixed. (error if V is an int)
00282 *
00283 * Revision 1.10  2003/04/01 17:46:05  dthomas
00284 * added: negation operator
00285 *
00286 * Revision 1.9  2003/03/22 00:10:41  dueffert
00287 * abs() now works in larger range
00288 *
00289 * Revision 1.8  2003/03/11 09:01:24  dueffert
00290 * Greenhills compilability restored
00291 *
00292 * Revision 1.7  2003/03/10 18:22:38  dueffert
00293 * assignments replaced with initializations
00294 *
00295 * Revision 1.6  2003/02/17 10:40:05  dueffert
00296 * greenhills backport
00297 *
00298 * Revision 1.5  2002/11/19 15:43:04  dueffert
00299 * doxygen comments corrected
00300 *
00301 * Revision 1.4  2002/11/12 23:00:47  dueffert
00302 * started restore greenhills compatibility
00303 *
00304 * Revision 1.3  2002/10/14 13:14:25  dueffert
00305 * doxygen comments corrected
00306 *
00307 * Revision 1.2  2002/09/22 18:40:52  risler
00308 * added new math functions, removed GTMath library
00309 *
00310 * Revision 1.1  2002/09/22 13:10:50  risler
00311 * new Math headers added
00312 *
00313 *
00314 */

Generated on Thu Sep 23 19:57:39 2004 for GT2004 by doxygen 1.3.6