Vector2D.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (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.
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 
00024 #include <Vector2D.h>
00025 #include <algorithm>
00026 
00027 Vector2D::Vector2D()
00028 {
00029     x = 0.0;
00030     y = 0.0;
00031 }
00032 
00033 Vector2D::Vector2D(double x, double y)
00034 {
00035     this->x = x;
00036     this->y = y;
00037 }
00038 
00039 void Vector2D::normalize()
00040 {
00041     double temp;
00042     temp = sqrt(x * x + y * y);
00043     if(temp != 0.0) {
00044         x /= temp;
00045         y /= temp;
00046     }
00047 }
00048 
00049 double Vector2D::distanceSqr(const Vector2D v) const
00050 {
00051     double dx, dy;
00052     dx = x - v.x;
00053     dy = y - v.y;
00054     return dx * dx + dy * dy;
00055 }
00056 
00057 double Vector2D::xyMaxDistance(const Vector2D v) const
00058 {
00059     return std::max(abs(x - v.x), abs(y - v.y));
00060 }
00061 
00062 double Vector2D::cosAngle(const Vector2D& v) const
00063 {
00064     return (x * v.x + y * v.y) / (sqrt(x * x + y * y) * sqrt(v.x * v.x + v.y * v.y));
00065 }
00066 
00078 int Vector2D::getQuadrant(const Vector2D& v) const
00079 {
00080     int quad = 0;
00081     // v.y <= this.y -> quadrant 1 or 2
00082     if( v.y <= y ) quad = 1;
00083     // v.x <= this.x -> quadrant 2 or 3
00084     if( v.x <= x ) quad ^= 3;
00085     return quad;
00086 }
00087 
00088 Vector2D& Vector2D::operator=(const Vector2D& v)
00089 {
00090     x = v.x;
00091     y = v.y;
00092     return *this;
00093 }
00094 
00095 Vector2D& Vector2D::operator+=(const Vector2D& v)
00096 {
00097     x += v.x;
00098     y += v.y;
00099     return *this;
00100 }
00101 
00102 Vector2D& Vector2D::operator-=(const Vector2D& v)
00103 {
00104     x -= v.x;
00105     y -= v.y;
00106     return *this;
00107 }
00108 
00109 Vector2D& Vector2D::operator*=(const double s)
00110 {
00111     x *= s;
00112     y *= s;
00113     return *this;
00114 }
00115 
00116 Vector2D& Vector2D::operator/=(const double s)
00117 {
00118     x /= s;
00119     y /= s;
00120     return *this;
00121 }
00122 
00123 Vector2D Vector2D::operator+(const Vector2D& v) const
00124 {
00125     Vector2D temp;
00126     temp.x = x + v.x;
00127     temp.y = y + v.y;
00128     return temp;
00129 }
00130 
00131 Vector2D Vector2D::operator-(const Vector2D& v) const
00132 {
00133     Vector2D temp;
00134     temp.x = x - v.x;
00135     temp.y = y - v.y;
00136     return temp;
00137 }
00138 
00139 Vector2D Vector2D::operator*(const double s) const
00140 {
00141     Vector2D temp;
00142     temp.x = x * s;
00143     temp.y = y * s;
00144     return temp;
00145 }
00146 
00147 Vector2D Vector2D::operator/(const double s) const
00148 {
00149     Vector2D temp;
00150     temp.x = x / s;
00151     temp.y = y / s;
00152     return temp;
00153 }
00154 
00155 bool Vector2D::operator==(const Vector2D& v) const
00156 {
00157     if(x == v.x && y == v.y)
00158         return true;
00159     else
00160         return false;
00161 }
00162 
00163 bool Vector2D::operator!=(const Vector2D& v) const
00164 {
00165     if(x != v.x || y != v.y)
00166         return true;
00167     else
00168         return false;
00169 }
00170 
00171 bool operator<(const Vector2D& a, const Vector2D& b)
00172 {
00173     if(a.y == b.y)
00174         return a.x < b.x;
00175     else
00176         return a.y < b.y;
00177 }
00178 
00179 std::ostream& operator<<(std::ostream& Stream, const Vector2D& v)
00180 {
00181     return Stream << std::fixed << "[" << v.x << ", " << v.y << "]";
00182 }
00183 
00184 void Vector2D::netPack(cCommBuffer *b)
00185 {
00186     //cMessage::netPack(b);
00187     doPacking(b, this->x);
00188     doPacking(b, this->y);
00189 }
00190 
00191 void Vector2D::netUnpack(cCommBuffer *b)
00192 {
00193     //cMessage::netUnpack(b);
00194     doUnpacking(b, this->x);
00195     doUnpacking(b, this->y);
00196 }
Generated on Wed May 26 16:21:15 2010 for OverSim by  doxygen 1.6.3