|
INET Framework for OMNeT++/OMNEST
|
00001 /*************************************************************************** 00002 * file: Coord.h 00003 * 00004 * author: Christian Frank 00005 * 00006 * copyright: (C) 2004 Telecommunication Networks Group (TKN) at 00007 * Technische Universitaet Berlin, Germany. 00008 * 00009 * This program is free software; you can redistribute it 00010 * and/or modify it under the terms of the GNU General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2 of the License, or (at your option) any later 00013 * version. 00014 * For further information see file COPYING 00015 * in the top level directory 00016 *************************************************************************** 00017 * part of: framework implementation developed by tkn 00018 **************************************************************************/ 00019 00020 00021 #ifndef __INET_COORD_H 00022 #define __INET_COORD_H 00023 00024 #include <omnetpp.h> 00025 #include "INETDefs.h" 00026 #include "FWMath.h" 00027 00038 class INET_API Coord : public cPolymorphic 00039 { 00040 public: 00042 double x,y; 00043 00045 Coord(double _x=0, double _y=0) : x(_x), y(_y) {}; 00046 00047 00049 Coord(const Coord& pos) { 00050 x=pos.x; 00051 y=pos.y; 00052 } 00053 00055 Coord(const Coord* pos) { 00056 x=pos->x; 00057 y=pos->y; 00058 } 00059 00060 std::string info() const { 00061 std::stringstream os; 00062 os << "(" << x << "," << y << ")"; 00063 return os.str(); 00064 } 00065 00067 friend Coord operator+(const Coord& a, const Coord& b) { 00068 return Coord(a.x+b.x, a.y+b.y); 00069 } 00070 00072 friend Coord operator-(const Coord& a, const Coord& b) { 00073 return Coord(a.x-b.x, a.y-b.y); 00074 } 00075 00077 friend Coord operator*(const Coord& a, double f) { 00078 return Coord(a.x*f, a.y*f); 00079 } 00080 00082 friend Coord operator/(const Coord& a, double f) { 00083 return Coord(a.x/f, a.y/f); 00084 } 00085 00087 const Coord& operator+=(const Coord& a) { 00088 x+=a.x; 00089 y+=a.y; 00090 return *this; 00091 } 00092 00094 const Coord& operator=(const Coord& a) { 00095 x=a.x; 00096 y=a.y; 00097 return *this; 00098 } 00099 00101 const Coord& operator-=(const Coord& a) { 00102 x-=a.x; 00103 y-=a.y; 00104 return *this; 00105 } 00106 00112 friend bool operator==(const Coord& a, const Coord& b) { 00113 return FWMath::close(a.x,b.x) && FWMath::close(a.y,b.y); 00114 } 00115 00120 friend bool operator!=(const Coord& a, const Coord& b) { 00121 return !(a==b); 00122 } 00123 00127 double distance(const Coord& a) const { 00128 return sqrt(sqrdist(a)); 00129 } 00130 00134 double sqrdist(const Coord& a) const { 00135 double dx=x-a.x; 00136 double dy=y-a.y; 00137 return dx*dx + dy*dy; 00138 } 00139 00140 }; 00141 00142 inline std::ostream& operator<<(std::ostream& os, const Coord& coord) 00143 { 00144 return os << "(" << coord.x << "," << coord.y << ")"; 00145 } 00146 00147 #endif 00148