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 
00026 Vector2D::Vector2D()
00027 {
00028     x = 0.0;
00029     y = 0.0;
00030 }
00031 
00032 Vector2D::Vector2D(double x, double y)
00033 {
00034     this->x = x;
00035     this->y = y;
00036 }
00037 
00038 void Vector2D::normalize()
00039 {
00040     double temp;
00041     temp = sqrt(x * x + y * y);
00042     if(temp != 0.0) {
00043         x /= temp;
00044         y /= temp;
00045     }
00046 }
00047 
00048 double Vector2D::distanceSqr(const Vector2D v) const
00049 {
00050     double dx, dy;
00051     dx = x - v.x;
00052     dy = y - v.y;
00053     return dx * dx + dy * dy;
00054 }
00055 
00056 double Vector2D::cosAngle(const Vector2D& v) const
00057 {
00058     return (x * v.x + y * v.y) / (sqrt(x * x + y * y) * sqrt(v.x * v.x + v.y * v.y));
00059 }
00060 
00061 Vector2D& Vector2D::operator=(const Vector2D& v)
00062 {
00063     x = v.x;
00064     y = v.y;
00065     return *this;
00066 }
00067 
00068 Vector2D& Vector2D::operator+=(const Vector2D& v)
00069 {
00070     x += v.x;
00071     y += v.y;
00072     return *this;
00073 }
00074 
00075 Vector2D& Vector2D::operator-=(const Vector2D& v)
00076 {
00077     x -= v.x;
00078     y -= v.y;
00079     return *this;
00080 }
00081 
00082 Vector2D& Vector2D::operator*=(const double s)
00083 {
00084     x *= s;
00085     y *= s;
00086     return *this;
00087 }
00088 
00089 Vector2D& Vector2D::operator/=(const double s)
00090 {
00091     x /= s;
00092     y /= s;
00093     return *this;
00094 }
00095 
00096 Vector2D Vector2D::operator+(const Vector2D& v) const
00097 {
00098     Vector2D temp;
00099     temp.x = x + v.x;
00100     temp.y = y + v.y;
00101     return temp;
00102 }
00103 
00104 Vector2D Vector2D::operator-(const Vector2D& v) const
00105 {
00106     Vector2D temp;
00107     temp.x = x - v.x;
00108     temp.y = y - v.y;
00109     return temp;
00110 }
00111 
00112 Vector2D Vector2D::operator*(const double s) const
00113 {
00114     Vector2D temp;
00115     temp.x = x * s;
00116     temp.y = y * s;
00117     return temp;
00118 }
00119 
00120 Vector2D Vector2D::operator/(const double s) const
00121 {
00122     Vector2D temp;
00123     temp.x = x / s;
00124     temp.y = y / s;
00125     return temp;
00126 }
00127 
00128 bool Vector2D::operator==(const Vector2D& v) const
00129 {
00130     if(x == v.x && y == v.y)
00131         return true;
00132     else
00133         return false;
00134 }
00135 
00136 bool Vector2D::operator!=(const Vector2D& v) const
00137 {
00138     if(x != v.x || y != v.y)
00139         return true;
00140     else
00141         return false;
00142 }
00143 
00144 bool operator<(const Vector2D& a, const Vector2D& b)
00145 {
00146     if(a.y == b.y)
00147         return a.x < b.x;
00148     else
00149         return a.y < b.y;
00150 }
00151 
00152 std::ostream& operator<<(std::ostream& Stream, const Vector2D& v)
00153 {
00154     return Stream << std::fixed << "[" << v.x << ", " << v.y << "]";
00155 }
00156 
00157 void Vector2D::netPack(cCommBuffer *b)
00158 {
00159     //cMessage::netPack(b);
00160     doPacking(b, this->x);
00161     doPacking(b, this->y);
00162 }
00163 
00164 void Vector2D::netUnpack(cCommBuffer *b)
00165 {
00166     //cMessage::netUnpack(b);
00167     doUnpacking(b, this->x);
00168     doUnpacking(b, this->y);
00169 }

Generated on Wed Mar 25 16:32:50 2009 for OverSim by  doxygen 1.5.5