- Main Page
- Classes
- Files

- File List
- File Members

# C:/Users/Public/AGS Final Folder/BFLETCHER D4010283/Source/Game/Source/Vector2.cpp

Go to the documentation of this file.

    00001 //Vector2.cpp
    00002 #include "Vector2.h"
    00003 #include <math.h>
    00004 #include <algorithm>
    00005
    00006 Vector2 Vector2::operator + (const Vector2 &vector) const
    00007 {
    00008         Vector2 temp(x + vector.x, y + vector.y);
    00009         return temp;
    00010 }
    00011
    00012 Vector2& Vector2::operator += (const Vector2 &vector)
    00013 {
    00014         x += vector.x;
    00015         y += vector.y;;
    00016         return *this;
    00017 }
    00018
    00019 Vector2 Vector2::operator - (const Vector2 &vector) const
    00020 {
    00021         Vector2 temp(x - vector.x, y - vector.y);
    00022         return temp;
    00023 }
    00024
    00025 Vector2& Vector2::operator -= (const Vector2 &vector)
    00026 {
    00027         x -= vector.x;
    00028         y -= vector.y;
    00029         return *this;
    00030 }
    00031
    00032 Vector2 Vector2::operator * (const Vector2 &vector) const
    00033 {
    00034         return Vector2(x * vector.x, y * vector.y);
    00035 }
    00036
    00037 Vector2 Vector2::operator * (const float &f) const
    00038 {
    00039         Vector2 temp(x * f, y * f);
    00040         return temp;
    00041 }
    00042
    00043 Vector2& Vector2::operator *= (const float &f)
    00044 {
    00045         x *= f;
    00046         y *= f;
    00047         return *this;
    00048 }
    00049
    00050 Vector2 Vector2::operator / (const float &f) const
    00051 {
    00052         float tempFloat = 1.0f / f;
    00053         Vector2 tempVec(x * tempFloat, y * tempFloat);
    00054         return tempVec;
    00055 }
    00056
    00057 Vector2& Vector2::operator /= (const float &f)
    00058 {
    00059         float temp = 1.0f / f;
    00060         x *= temp;
    00061         y *= temp;
    00062         return *this;
    00063 }
    00064
    00065 Vector2& Vector2::operator = (const Vector2 &vector)
    00066 {
    00067         x = vector.x;
    00068         y = vector.y;
    00069         return *this;
    00070 }
    00071
    00072 bool Vector2::operator == (const Vector2 &vector) const
    00073 {
    00074         return (x == vector.x && y == vector.y);
    00075 }
    00076
    00077 void Vector2::normalise()
    00078 {
    00079         float mag = this->getLength();
    00080         if(mag > 0)
    00081         {
    00082                 x /= mag;
    00083                 y /= mag;
    00084         }
    00085 }
    00086
    00087 Vector2 Vector2::perpendicular()
    00088 {
    00089         return Vector2(-y, x);
    00090 }
    00091
    00092 float Vector2::dot(const Vector2 &left, const Vector2 &right)
    00093 {
    00094         return (left.x * right.x) + (left.y * right.y);
    00095 }
    00096
    00097 float Vector2::cross(const Vector2 &left, const Vector2 &right)
    00098 {
    00099         return (left.x * right.y) - (left.y * right.x);
    00100 }
    00101
    00102 Vector2 Vector2::vmin(const Vector2 &a, const Vector2 &b)
    00103 {
    00104         Vector2 retVec;
    00105         retVec.x = a.x > b.x ? b.x : a.x;
    00106         retVec.y = a.y > b.y ? b.y : a.y;
    00107         return retVec;
    00108 }
    00109
    00110 Vector2 Vector2::vmax(const Vector2 &a, const Vector2 &b)
    00111 {
    00112         Vector2 retVec;
    00113         retVec.x = a.x < b.x ? b.x : a.x;
    00114         retVec.y = a.y < b.y ? b.y : a.y;
    00115         return retVec;
    00116 }
    00117
    00118 float Vector2::getLength() const
    00119 {
    00120         float temp = sqrt((x * x) + (y * y));
    00121         return temp;
    00122 }
    00123
    00124 float Vector2::getLengthSqrd() const
    00125 {
    00126         float temp = (x * x) + (y * y);
    00127         return temp;
    00128 }
    00129
    00130 Vector2::operator const float* () const
    00131 {
    00132         return &x;
    00133 }
    00134
    00135 bool Vector2::isRoughlyEqual(const Vector2 &a, const Vector2 &b)
    00136 {
    00137         int ax = (int)a.x; int ay = (int)a.y;
    00138         int bx = (int)b.x; int by = (int)b.y;
    00139
    00140         if(ax == bx && ay == by)
    00141                 return true;
    00142         else
    00143                 return false;
    00144 }
    00145
    00146
    00147
    00148
    00149 Vector2i Vector2i::operator + (const Vector2i &vector) const
    00150 {
    00151         Vector2i temp(x + vector.x, y + vector.y);
    00152         return temp;
    00153 }
    00154
    00155 Vector2i& Vector2i::operator += (const Vector2i &vector)
    00156 {
    00157         x += vector.x;
    00158         y += vector.y;;
    00159         return *this;
    00160 }
    00161
    00162 Vector2i Vector2i::operator - (const Vector2i &vector) const
    00163 {
    00164         Vector2i temp(x - vector.x, y - vector.y);
    00165         return temp;
    00166 }
    00167
    00168 Vector2i& Vector2i::operator -= (const Vector2i &vector)
    00169 {
    00170         x -= vector.x;
    00171         y -= vector.y;
    00172         return *this;
    00173 }
    00174
    00175 Vector2i Vector2i::operator * (const Vector2i &vector) const
    00176 {
    00177         return Vector2i(x * vector.x, y * vector.y);
    00178 }
    00179
    00180 Vector2i Vector2i::operator * (const int &n) const
    00181 {
    00182         Vector2i temp(x * n, y * n);
    00183         return temp;
    00184 }
    00185
    00186 Vector2i& Vector2i::operator *= (const int &n)
    00187 {
    00188         x *= n;
    00189         y *= n;
    00190         return *this;
    00191 }
    00192
    00193 Vector2i Vector2i::operator / (const int &n) const
    00194 {
    00195         Vector2i tempVec(x / n, y / n);
    00196         return tempVec;
    00197 }
    00198
    00199 Vector2i& Vector2i::operator /= (const int &n)
    00200 {
    00201         x /= n;
    00202         y /= n;
    00203         return *this;
    00204 }
    00205
    00206 Vector2i& Vector2i::operator = (const Vector2i &vector)
    00207 {
    00208         x = vector.x;
    00209         y = vector.y;
    00210         return *this;
    00211 }
    00212
    00213 bool Vector2i::operator == (const Vector2i &vector) const
    00214 {
    00215         return (x == vector.x && y == vector.y);
    00216 }
    00217
    00218
    00219 Vector2i Vector2i::perpendicular()
    00220 {
    00221         return Vector2i(-y, x);
    00222 }
    00223
    00224 int Vector2i::dot(const Vector2i &left, const Vector2i &right)
    00225 {
    00226         return (left.x * right.x) + (left.y * right.y);
    00227 }
    00228
    00229 int Vector2i::cross(const Vector2i &left, const Vector2i &right)
    00230 {
    00231         return (left.x * right.y) - (left.y * right.x);
    00232 }
    00233
    00234 Vector2i Vector2i::vmin(const Vector2i &a, const Vector2i &b)
    00235 {
    00236         Vector2i retVec;
    00237         retVec.x = a.x > b.x ? b.x : a.x;
    00238         retVec.y = a.y > b.y ? b.y : a.y;
    00239         return retVec;
    00240 }
    00241
    00242 Vector2i Vector2i::vmax(const Vector2i &a, const Vector2i &b)
    00243 {
    00244         Vector2i retVec;
    00245         retVec.x = a.x < b.x ? b.x : a.x;
    00246         retVec.y = a.y < b.y ? b.y : a.y;
    00247         return retVec;
    00248 }
    00249
    00250 float Vector2i::getLength() const
    00251 {
    00252         float temp = sqrt((float)((x * x) + (y * y)));
    00253         return temp;
    00254 }
    00255
    00256 int Vector2i::getLengthSqrd() const
    00257 {
    00258         int temp = (x * x) + (y * y);
    00259         return temp;
    00260 }
    00261
    00262 Vector2i::operator const int* () const
    00263 {
    00264         return &x;
    00265 }
    00266