BWAPI
Skynet/Skynet/Vector.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include <math.h>
00004 
00005 #include "Interface.h"
00006 
00007 class Vector
00008 {
00009 public:
00010         float x, y;
00011 
00012         Vector()
00013                 : x(0), y(0)
00014         { }
00015 
00016         Vector(const float x, const float y)
00017                 : x(x), y(y)
00018         { }
00019 
00020         Vector(const int x, const int y)
00021                 : x(float(x)), y(float(y))
00022         { }
00023 
00024         Vector(const Vector &v)
00025                 : x(v.x), y(v.y)
00026         { }
00027 
00028         Vector(const Position &p)
00029                 : x( float(p.x()) ), y( float(p.y()) )
00030         { }
00031 
00032         Vector(const TilePosition &p)
00033                 : x( float(p.x() * 32) ), y( float(p.y() * 32) )
00034         { }
00035 
00036         inline operator Position()
00037         {
00038                 return Position( int(x), int(y) );
00039         }
00040 
00041         inline operator TilePosition()
00042         {
00043                 return TilePosition( int(x / 32), int(y / 32) );
00044         }
00045 
00046         inline Vector& operator=(const float scalar)
00047         {
00048                 x = scalar;
00049                 y = scalar;
00050 
00051                 return *this;
00052         }
00053 
00054         inline bool operator==(const Vector &v) const
00055         {
00056                 return (x == v.x && y == v.y);
00057         }
00058 
00059         inline bool operator!=(const Vector &v) const
00060         {
00061                 return !(x == v.x && y == v.y);
00062         }
00063 
00064         inline Vector operator+(const Vector& v) const
00065         {
00066                 return Vector(x + v.x, y + v.y);
00067         }
00068 
00069         inline Vector operator-(const Vector& v) const
00070         {
00071                 return Vector(x - v.x, y - v.y);
00072         }
00073 
00074         inline Vector operator*(const float& scalar) const
00075         {
00076                 return Vector(x * scalar, y * scalar);
00077         }
00078 
00079         inline Vector operator*(const Vector& v) const
00080         {
00081                 return Vector(x * v.x, y * v.y);
00082         }
00083 
00084         inline Vector operator/(const float& scalar) const
00085         {
00086                 return Vector(x / scalar, y / scalar);
00087         }
00088 
00089         inline Vector operator/(const Vector& v) const
00090         {
00091                 return Vector(x / v.x, y / v.y);
00092         }
00093 
00094         inline Vector operator+() const
00095         {
00096                 return *this;
00097         }
00098 
00099         inline Vector operator-() const
00100         {
00101                 return Vector(-x, -y);
00102         }
00103 
00104         inline friend Vector operator*(const float scalar, const Vector& v)
00105         {
00106                 return Vector(scalar * v.x, scalar * v.y);
00107         }
00108 
00109         inline friend Vector operator/(const float scalar, const Vector& v)
00110         {
00111                 return Vector(scalar / v.x, scalar / v.y);
00112         }
00113 
00114         inline friend Vector operator+(const Vector& lhs, const float rhs)
00115         {
00116                 return Vector(lhs.x + rhs, lhs.y + rhs);
00117         }
00118 
00119         inline friend Vector operator+(const float lhs, const Vector& rhs)
00120         {
00121                 return Vector(lhs + rhs.x, lhs + rhs.y);
00122         }
00123 
00124         inline friend Vector operator-(const Vector& lhs, const float rhs)
00125         {
00126                 return Vector(lhs.x - rhs, lhs.y - rhs);
00127         }
00128 
00129         inline friend Vector operator-(const float lhs, const Vector& rhs)
00130         {
00131                 return Vector(lhs - rhs.x, lhs - rhs.y);
00132         }
00133 
00134         inline Vector& operator+=(const Vector& v)
00135         {
00136                 x += v.x;
00137                 y += v.y;
00138 
00139                 return *this;
00140         }
00141 
00142         inline Vector& operator+=(const float scalar)
00143         {
00144                 x += scalar;
00145                 y += scalar;
00146 
00147                 return *this;
00148         }
00149 
00150         inline Vector& operator-=(const Vector& v)
00151         {
00152                 x -= v.x;
00153                 y -= v.y;
00154 
00155                 return *this;
00156         }
00157 
00158         inline Vector& operator-=(const float scalar)
00159         {
00160                 x -= scalar;
00161                 y -= scalar;
00162 
00163                 return *this;
00164         }
00165 
00166         inline Vector& operator*=(const float scalar)
00167         {
00168                 x *= scalar;
00169                 y *= scalar;
00170 
00171                 return *this;
00172         }
00173 
00174         inline Vector& operator*=(const Vector& v)
00175         {
00176                 x *= v.x;
00177                 y *= v.y;
00178 
00179                 return *this;
00180         }
00181 
00182         inline Vector& operator/=(const float scalar)
00183         {
00184                 x /= scalar;
00185                 y /= scalar;
00186 
00187                 return *this;
00188         }
00189 
00190         inline Vector& operator/=(const Vector& v)
00191         {
00192                 x /= v.x;
00193                 y /= v.y;
00194 
00195                 return *this;
00196         }
00197 
00198         inline float length() const
00199         {
00200                 return sqrt(x * x + y * y);
00201         }
00202 
00203         inline float squaredLength() const
00204         {
00205                 return x * x + y * y;
00206         }
00207 
00208         inline float distance(const Vector& v) const
00209         {
00210                 return (*this - v).length();
00211         }
00212 
00213         inline float squaredDistance(const Vector& v) const
00214         {
00215                 return (*this - v).squaredLength();
00216         }
00217 
00218         inline float dotProduct(const Vector& v) const
00219         {
00220                 return x * v.x + y * v.y;
00221         }
00222 
00223         inline float normalise()
00224         {
00225                 float iLength = sqrt( x * x + y * y );
00226 
00227                 if(iLength > 0)
00228                 {
00229                         x /= iLength;
00230                         y /= iLength;
00231                 }
00232 
00233                 return iLength;
00234         }
00235 
00236         inline Vector midPoint(const Vector& v) const
00237         {
00238                 return Vector((x + v.x) * 0.5f, (y + v.y ) * 0.5f);
00239         }
00240 
00241         inline bool operator<(const Vector& v) const
00242         {
00243                 if(x == v.x)
00244                         return y < v.y;
00245 
00246                 return x < v.x;
00247         }
00248 
00249         inline bool operator>(const Vector& v) const
00250         {
00251                 if(x == v.x)
00252                         return y > v.y;
00253 
00254                 return x > v.x;
00255         }
00256 
00257         inline void makeFloor(const Vector& v)
00258         {
00259                 if(v.x < x) x = v.x;
00260                 if(v.y < y) y = v.y;
00261         }
00262 
00263         inline void makeCeil(const Vector& v)
00264         {
00265                 if(v.x > x) x = v.x;
00266                 if(v.y > y) y = v.y;
00267         }
00268 
00269         inline Vector perpendicular() const
00270         {
00271                 return Vector(-y, x);
00272         }
00273 
00274         inline float crossProduct(const Vector& v) const
00275         {
00276                 return x * v.y - y * v.x;
00277         }
00278 
00279         inline Vector randomDeviant(float angle) const
00280         {
00281                 angle *= (float(rand()) / float(RAND_MAX)) * 3.1415f * 2.0f;
00282                 float cosa = cos(angle);
00283                 float sina = sin(angle);
00284                 return Vector(cosa * x - sina * y, sina * x + cosa * y);
00285         }
00286 
00287         inline bool isZeroLength() const
00288         {
00289                 return (x == 0 && y == 0);
00290         }
00291 
00292         inline Vector normalisedCopy() const
00293         {
00294                 Vector ret = *this;
00295                 ret.normalise();
00296                 return ret;
00297         }
00298 
00299         inline Vector reflect(const Vector& normal) const
00300         {
00301                 return Vector( *this - ( 2.0f * dotProduct(normal) * normal ) );
00302         }
00303 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines