00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 #ifndef _COORD_H
00022 #define _COORD_H
00023 
00024 #include <omnetpp.h>
00025 #include "FWMath.h"
00026 
00037 class Coord : public cPolymorphic
00038 {
00039  public:
00041     double x,y;
00042 
00044     Coord(double _x=0, double _y=0) : x(_x), y(_y) {};
00045 
00046 
00048     Coord( const Coord& pos ) {
00049         x=pos.x;
00050         y=pos.y;
00051     }
00052 
00054     Coord( const Coord* pos ) {
00055         x=pos->x;
00056         y=pos->y;
00057     }
00058 
00059     std::string info() const {
00060         std::stringstream os;
00061         os << "(" << x << "," << y << ")";
00062         return os.str();
00063     }
00064 
00066     friend Coord operator+(const Coord& a, const Coord& b) {
00067         return Coord(a.x+b.x, a.y+b.y);
00068     }
00069 
00071     friend Coord operator-(const Coord& a, const Coord& b) {
00072         return Coord(a.x-b.x, a.y-b.y);
00073     }
00074 
00076     friend Coord operator*(const Coord& a, double f) {
00077         return Coord(a.x*f, a.y*f);
00078     }
00079 
00081     friend Coord operator/(const Coord& a, double f) {
00082         return Coord(a.x/f, a.y/f);
00083     }
00084 
00086     Coord operator+=(const Coord& a) {
00087         x+=a.x;
00088         y+=a.y;
00089         return *this;
00090     }
00091 
00093     Coord operator=(const Coord& a) {
00094         x=a.x;
00095         y=a.y;
00096         return *this;
00097     }
00098 
00100     Coord operator-=(const Coord& a) {
00101         x-=a.x;
00102         y-=a.y;
00103         return *this;
00104     }
00105 
00111     friend bool operator==(const Coord& a, const Coord& b) {
00112         return FWMath::close(a.x,b.x) && FWMath::close(a.y,b.y);
00113     }
00114 
00119     friend bool operator!=(const Coord& a, const Coord& b) {
00120         return !(a==b);
00121     }
00122 
00126     double distance( const Coord& a ) const {
00127         Coord dist=*this-a;
00128         return sqrt( dist.x*dist.x + dist.y*dist.y);
00129     }
00130 
00134     double sqrdist( const Coord& a ) const {
00135         Coord dist=*this-a;
00136         return dist.x*dist.x + dist.y*dist.y;
00137     }
00138 
00142     double sqrTorusDist(const Coord& b, const Coord& playGround) const {
00143         double minTorDist;
00144         double torDist;
00145         torDist = FWMath::torDist(this->x,              b.x, this->y,                  b.y);
00146         minTorDist = torDist;
00147         torDist = FWMath::torDist(this->x+playGround.x, b.x, this->y,                  b.y);
00148         if(torDist < minTorDist) minTorDist = torDist;
00149         torDist = FWMath::torDist(this->x-playGround.x, b.x, this->y,                  b.y);
00150         if(torDist < minTorDist) minTorDist = torDist;
00151         torDist = FWMath::torDist(this->x,              b.x, this->y+playGround.y,     b.y);
00152         if(torDist < minTorDist) minTorDist = torDist;
00153         torDist = FWMath::torDist(this->x,              b.x, this->y-playGround.y,     b.y);
00154         if(torDist < minTorDist) minTorDist = torDist;
00155         torDist = FWMath::torDist(this->x+playGround.x, b.x, this->y+playGround.y,     b.y);
00156         if(torDist < minTorDist) minTorDist = torDist;
00157         torDist = FWMath::torDist(this->x+playGround.x, b.x, this->y-playGround.y,     b.y);
00158         if(torDist < minTorDist) minTorDist = torDist;
00159         torDist = FWMath::torDist(this->x-playGround.x, b.x, this->y+playGround.y,     b.y);
00160         if(torDist < minTorDist) minTorDist = torDist;
00161         torDist = FWMath::torDist(this->x-playGround.x, b.x, this->y-playGround.y,     b.y);
00162         if(torDist < minTorDist) minTorDist = torDist;
00163         return minTorDist;
00164     }
00165 };
00166 
00167 #endif
00168