00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __GEOMETRY_H__
00021 #define __GEOMETRY_H__
00022
00023 #include <iostream>
00024 #include <vector>
00025
00029 struct Point
00030 {
00032 double x;
00034 double y;
00035
00036 Point( double cx=.0, double cy=.0 )
00037 : x(cx),
00038 y(cy)
00039 {}
00040
00041 Point( const Point& rhs )
00042 : x(rhs.x),
00043 y(rhs.y)
00044 {}
00045
00046 Point& operator=( const Point& rhs )
00047 {
00048 if( this != &rhs) {
00049 x = rhs.x;
00050 y = rhs.y;
00051 }
00052
00053 return *this;
00054 }
00055
00056 friend std::ostream& operator<<( std::ostream& output, const struct Point& rhs ) {
00057 output << "[" << std::endl
00058 << rhs.x << std::endl
00059 << "," << std::endl
00060 << rhs.y << std::endl
00061 << "];";
00062 return output;
00063 }
00064
00065 friend std::istream& operator>>( std::istream& input, struct Point& rhs ) {
00066 std::string tmp;
00067 input >> tmp >> rhs.x >> tmp >> rhs.y >> tmp;
00068 return input;
00069 }
00070
00071 friend bool operator==( const Point& lhs, const Point& rhs ){
00072 double round = 1e-2;
00073
00074 double dx = lhs.x - rhs.x;
00075 double dy = lhs.y - rhs.y;
00076
00077 return (dx < round && dx > -round ) && ( dy < round && dy > -round );
00078 }
00079
00080 std::ostream& Write( std::ostream& str ) const {
00081 return str << "[" << this->x << "," << this->y << "]";
00082 }
00083
00084 virtual ~Point() {}
00085
00089 double Distance( const Point& p, int sign=1 ) const;
00090 };
00091
00092 typedef std::vector<Point> PointsVector;
00093
00097 struct Position : public Point
00098 {
00099 Position( double cx=.0, double cy=.0, double cangle=.0 )
00100 : Point( cx, cy ), angle( cangle )
00101 {}
00102
00103 Position( const Point& p, double cangle=.0)
00104 : Point( p ), angle( cangle )
00105 {}
00106
00107 Position( const Position& p )
00108 : Point( p )
00109 { *this = p; }
00110
00111 Position& operator=( const Position& p )
00112 {
00113 if( this != &p ) {
00114 Point::operator=( p );
00115 angle = p.angle;
00116 }
00117 return *this;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 friend std::ostream& operator<<( std::ostream& output, const struct Position& rhs ) {
00131 output << "{" << std::endl;
00132 output << (Point&)rhs << std::endl;
00133 output << rhs.angle << std::endl
00134 << "};";
00135 return output;
00136 }
00137
00138 friend std::istream& operator>>( std::istream& input, struct Position& rhs ) {
00139 std::string tmp;
00140 input >> tmp >> (Point&)rhs >> rhs.angle >> tmp;
00141 return input;
00142 }
00143
00144 virtual ~Position() {}
00145
00147 double angle;
00148 };
00149
00154 struct Vector
00155 {
00156 Vector( const struct Point& s, const struct Point& e )
00157 : begin( s ), direction( .0 )
00158 {
00159 direction = (e.y-s.y)/(e.x-s.x);
00160 }
00161
00162 Vector( const struct Point& s, double angle );
00163 Vector( const struct Position& p );
00164
00165 Vector( const Vector& rhs )
00166 : begin( rhs.begin ), direction( rhs.direction )
00167 {}
00168
00169 Vector& operator=( const Vector& rhs ) {
00170 if( this != &rhs ) {
00171 begin = rhs.begin;
00172 direction = rhs.direction;
00173 }
00174 return *this;
00175 }
00176
00180 bool Intersect( const Vector&, struct Point& ) const;
00181 bool IsVertical() const;
00182 bool IsHorizontal() const;
00183
00184 double Y0() const;
00185
00186 struct Point begin;
00187 double direction;
00188 };
00189
00190 std::ostream& operator<<( std::ostream&, const Vector& );
00191
00192 struct Circle
00193 {
00194 Circle( const Point& c, const double r )
00195 : center( c ), radius( r )
00196 {}
00197
00198 Circle( const Circle& rhs )
00199 : center( rhs.center ), radius( rhs.radius )
00200 {}
00201
00202 Circle& operator=( const Circle& rhs ) {
00203 if( this != &rhs ) {
00204 this->center = rhs.center;
00205 this->radius = rhs.radius;
00206 }
00207 return *this;
00208 }
00209
00210 PointsVector Intersects( const Circle& second ) const;
00211 PointsVector Intersects( const Vector& second ) const;
00212 double Distance( const Position&, const Point&, int sign=1 , int last=1) const;
00213
00214 Point center;
00215 double radius;
00216 };
00217
00218 std::ostream& operator<<(std::ostream& output, const Circle& c );
00219
00220 namespace GFuncs
00221 {
00222 double RoundToDigits( double number, unsigned digits=5 );
00223 bool IsSameDouble( double lhs, double rhs );
00224 double StandardizeAngle( double angle );
00225 double DegToRad( double deg );
00226 double RadToDeg( double rad );
00227 double RadiusToAngle( double radius, double r );
00228 Position Transform( const struct Position& base, const Position& p );
00229 }
00230
00231 #endif
00232