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 #include "Tools/Streams/Streamable.h"
    00015
    00016 /** This class represents a 2-vector */
    00017 template <class V> class Vector2 : public Streamable{
    00018   public:
    00019     virtual void serialize(In* in, Out* out)
    00020     {
    00021       STREAM_REGISTER_BEGIN();
    00022       STREAM( x);
    00023       STREAM( y);
    00024       STREAM_REGISTER_FINISH();
    00025     }
    00026   /** The vector values */
    00027   V x,y;
    00028
    00029   /** Default constructor. */
    00030   Vector2<V>():x(0),y(0)
    00031   {
    00032   }
    00033   /** Default constructor. */
    00034   Vector2<V>(V x, V y):x(x),y(y)
    00035   {
    00036   }
    00037
    00038   /** Assignment operator
    00039   *\param other The other vector that is assigned to this one
    00040   *\return A reference to this object after the assignment.
    00041   */
    00042   Vector2<V>& operator=(const Vector2<V>& other)
    00043   {
    00044     x=other.x;y=other.y;
    00045     return *this;
    00046   }
    00047
    00048   /** Copy constructor
    00049   *\param other The other vector that is copied to this one
    00050   */
    00051   Vector2<V>(const Vector2<V>& other):Streamable() {*this = other;}
    00052
    00053   /** Addition of another vector to this one.
    00054   *\param other The other vector that will be added to this one
    00055   *\return A reference to this object after the calculation.
    00056   */
    00057   Vector2<V>& operator+=(const Vector2<V>& other)
    00058   {
    00059     x += other.x;
    00060     y += other.y;
    00061     return *this;
    00062   }
    00063
    00064   /** Substraction of this vector from another one.
    00065   *\param other The other vector this one will be substracted from
    00066   *\return A reference to this object after the calculation.
    00067   */
    00068   Vector2<V>& operator-=(const Vector2<V>& other)
    00069   {
    00070     x -= other.x;
    00071     y -= other.y;
    00072     return *this;
    00073   }
    00074
    00075   /** Multiplication of this vector by a factor.
    00076   *\param factor The factor this vector is multiplied by
    00077   *\return A reference to this object after the calculation.
    00078   */
    00079   Vector2<V>& operator*=(const V& factor)
    00080   {
    00081     x *= factor;
    00082     y *= factor;
    00083     return *this;
    00084   }
    00085
    00086   /** Division of this vector by a factor.
    00087   *\param factor The factor this vector is divided by
    00088   *\return A reference to this object after the calculation.
    00089   */
    00090   Vector2<V>& operator/=(const V& factor)
    00091   {
    00092     if (factor == 0) return *this;
    00093     x /= factor;
    00094     y /= factor;
    00095     return *this;
    00096   }
    00097
    00098   /** Addition of another vector to this one.
    00099   *\param other The other vector that will be added to this one
    00100   *\return A new object that contains the result of the calculation.
    00101   */
    00102   Vector2<V> operator+(const Vector2<V>& other) const
    00103     {return Vector2<V>(*this) += other;}
    00104
    00105   /** Subtraction of another vector to this one.
    00106   *\param other The other vector that will be added to this one
    00107   *\return A new object that contains the result of the calculation.
    00108   */
    00109   Vector2<V> operator-(const Vector2<V>& other) const
    00110     {return Vector2<V>(*this) -= other;}
    00111
    00112   /** Negation of this vector.
    00113   *\return A new object that contains the result of the calculation.
    00114   */
    00115   Vector2<V> operator-() const
    00116     {return Vector2<V>() -= *this;}
    00117
    00118   /** Inner product of this vector and another one.
    00119   *\param other The other vector this one will be multiplied by
    00120   *\return The inner product.
    00121   */
    00122   V operator*(const Vector2<V>& other) const
    00123   {
    00124     return x * other.x + y * other.y;
    00125   }
    00126
    00127   /** Multiplication of this vector by a factor.
    00128   *\param factor The factor this vector is multiplied 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   /** Division of this vector by a factor.
    00135   *
    00136   *\param factor The factor this vector is divided by
    00137   *\return A new object that contains the result of the calculation.
    00138   */
    00139   Vector2<V> operator/(const V& factor) const
    00140     {return Vector2<V>(*this) /= factor;}
    00141
    00142   /** Comparison of another vector with this one.
    00143   *\param other The other vector that will be compared to this one
    00144   *\return Whether the two vectors are equal.
    00145   */
    00146   bool operator==(const Vector2<V>& other) const
    00147   {
    00148     return (x==other.x && y==other.y);
    00149   }
    00150
    00151   /** Comparison of another vector with this one.
    00152   *\param other The other vector that will be compared to this one.
    00153   *\return Whether the two vectors are unequal.
    00154   */
    00155   bool operator!=(const Vector2<V>& other) const
    00156     {return !(*this == other);}
    00157
    00158   /** Calculation of the length of this vector.
    00159   *\return The length.
    00160   */
    00161   V abs() const
    00162   {return (V) sqrt(((double)x)*x+((double)y)*y);}
    00163
    00164   /** normalize this vector.
    00165   *\param len The length, the vector should be normalized to, default=1.
    00166   *\return the normalized vector.
    00167   */
    00168   Vector2<V> normalize(V len)
    00169   {
    00170     if (abs() == 0) return *this;
    00171     return *this = (*this * len) / abs();
    00172    }
    00173
    00174   /** normalize this vector.
    00175   *\return the normalized vector.
    00176   */
    00177   Vector2<V> normalize()
    00178   {
    00179     if (abs() == 0) return *this;
    00180     return *this /= abs();
    00181   }
    00182
    00183   /** transpose this vector.
    00184   *\return the transposed vector.
    00185   */
    00186   Vector2<V> transpose()
    00187   { V buffer = x;
    00188     x = y;
    00189     y = buffer;
    00190     return *this;
    00191   }
    00192
    00193   /** the vector is rotated left by 90 degrees.
    00194   *\return the rotated vector.
    00195   */
    00196   Vector2<V> rotateLeft()
    00197   { V buffer = -y;
    00198     y = x;
    00199     x = buffer;
    00200     return *this;
    00201   }
    00202
    00203   /** the vector is rotated right by 90 degrees.
    00204   *\return the rotated vector.
    00205   */
    00206   Vector2<V> rotateRight()
    00207   { V buffer = -x;
    00208     x = y;
    00209     y = buffer;
    00210     return *this;
    00211   }
    00212
    00213   /**
    00214   * array-like member access.
    00215   * \param i index of coordinate
    00216   * \return reference to x or y
    00217   */
    00218   V& operator[](int i)
    00219   {
    00220     return  (&x)[i];
    00221   }
    00222
    00223   /** Calculation of the angle of this vector */
    00224   double angle() const
    00225   {return atan2((double)y,(double)x);}
    00226 };
    00227
    00228 /**
    00229 * Streaming operator that reads a Vector2<V> from a stream.
    00230 * @param stream The stream from which is read.
    00231 * @param vector2 The Vector2<V> object.
    00232 * @return The stream.
    00233 */
    00234 /*
    00235 template <class V> In& operator>>(In& stream, Vector2<V>& vector2)
    00236 {
    00237   stream >> vector2.x;
    00238   stream >> vector2.y;
    00239   return stream;
    00240 }
    00241 */
    00242 /**
    00243 * Streaming operator that writes a Vector2<V> to a stream.
    00244 * @param stream The stream to write on.
    00245 * @param vector2 The Vector2<V> object.
    00246 * @return The stream.
    00247 */
    00248 /*
    00249 template <class V> Out& operator<<(Out& stream, const Vector2<V>& vector2)
    00250 {
    00251   stream << vector2.x;
    00252   stream << vector2.y;
    00253   return stream;
    00254 }
    00255 */
    00256 #endif // __Vector2_h__