Position2D.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A position in a 2D-world
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 #ifndef Position2D_h
00020 #define Position2D_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #include <iostream>
00027 #include <cmath>
00028 
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 // ===========================================================================
00036 // class definitions
00037 // ===========================================================================
00042 class Position2D {
00043 public:
00045     Position2D() : myX(0.0), myY(0.0) { }
00046 
00048     Position2D(SUMOReal x, SUMOReal y)
00049             : myX(x), myY(y) { }
00050 
00052     ~Position2D() { }
00053 
00055     SUMOReal x() const {
00056         return myX;
00057     }
00058 
00060     SUMOReal y() const {
00061         return myY;
00062     }
00063 
00065     void set(SUMOReal x, SUMOReal y) {
00066         myX = x;
00067         myY = y;
00068     }
00069 
00071     void set(const Position2D &pos) {
00072         myX = pos.myX;
00073         myY = pos.myY;
00074     }
00075 
00076 
00078     void mul(SUMOReal val) {
00079         myX *= val;
00080         myY *= val;
00081     }
00082 
00084     void mul(SUMOReal mx, SUMOReal my) {
00085         myX *= mx;
00086         myY *= my;
00087     }
00088 
00090     void add(const Position2D &pos) {
00091         myX += pos.myX;
00092         myY += pos.myY;
00093     }
00094 
00096     void add(SUMOReal dx, SUMOReal dy) {
00097         myX += dx;
00098         myY += dy;
00099     }
00100 
00102     void sub(SUMOReal dx, SUMOReal dy) {
00103         myX -= dx;
00104         myY -= dy;
00105     }
00106 
00108     void sub(const Position2D &pos) {
00109         myX -= pos.myX;
00110         myY -= pos.myY;
00111     }
00112 
00113     void norm() {
00114         SUMOReal val = sqrt(myX*myX + myY*myY);
00115         myX = myX / val;
00116         myY = myY / val;
00117     }
00118 
00119     void reshiftRotate(SUMOReal xoff, SUMOReal yoff, SUMOReal rot) {
00120         SUMOReal x = myX * cos(rot) -myY * sin(rot) + xoff;
00121         SUMOReal y = myX * sin(rot) + yoff + myY * cos(rot);
00122         myX = x;
00123         myY = y;
00124     }
00125 
00126 
00128     friend std::ostream &operator<<(std::ostream &os, const Position2D &p) {
00129         os << p.x() << "," << p.y();
00130         return os;
00131     }
00132 
00133     bool operator==(const Position2D &p2) const {
00134         return myX==p2.myX && myY==p2.myY;
00135     }
00136 
00137     bool operator!=(const Position2D &p2) const {
00138         return myX!=p2.myX || myY!=p2.myY;
00139     }
00140 
00141 
00142     bool almostSame(const Position2D &p2, SUMOReal maxDiv=POSITION_EPS) const {
00143         return fabs(myX-p2.myX)<maxDiv && fabs(myY-p2.myY)<maxDiv;
00144     }
00145 
00146 
00147     inline SUMOReal distanceTo(const Position2D &p2) const {
00148         return sqrt(distanceSquaredTo(p2));
00149     }
00150 
00151 
00152     inline SUMOReal distanceSquaredTo(const Position2D &p2) const {
00153         return (myX-p2.myX)*(myX-p2.myX) + (myY-p2.myY)*(myY-p2.myY);
00154     }
00155 
00156 
00157 private:
00159     SUMOReal myX;
00160 
00162     SUMOReal myY;
00163 
00164 };
00165 
00166 
00167 #endif
00168 
00169 /****************************************************************************/
00170 

Generated on Wed May 5 00:06:35 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6