Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <iostream>
00011 #include <sstream>
00012
00013 #include "vec2.h"
00014 #include <float.h>
00015
00016 #ifndef POINT2D_H
00017 #define POINT2D_H
00018
00025 class Point2D
00026 {
00027
00028 public:
00029
00033 inline Point2D()
00034 {
00035 m_X = 0.0;
00036 m_Y = 0.0;
00037 }
00038
00044 inline Point2D ( double x, double y )
00045 {
00046 m_X = x;
00047 m_Y = y;
00048 }
00049
00050
00051
00055 inline Point2D (const Point2D& p){
00056 m_X = p.x();
00057 m_Y = p.y();
00058 }
00059
00064 inline Point2D ( const CVec2& v )
00065 {
00066 m_X = v[0];
00067 m_Y = v[1];
00068 }
00069
00073 inline ~Point2D()
00074 {
00075 }
00076
00081 inline double x() const
00082 {
00083 return m_X;
00084 }
00085
00090 inline double y() const
00091 {
00092 return m_Y;
00093 }
00094
00100 inline void set ( double x, double y )
00101 {
00102 m_X = x;
00103 m_Y = y;
00104 }
00105
00110 inline void setX ( double x )
00111 {
00112 m_X = x;
00113 }
00114
00119 inline void setY ( double y )
00120 {
00121 m_Y = y;
00122 }
00123
00128 inline Point2D& operator= ( const Point2D& p) {
00129 m_X = p.x();
00130 m_Y = p.y();
00131 return *this;
00132 }
00133
00134 inline Point2D operator+ ( const CVec2& v ) const
00135 {
00136 return Point2D ( m_X + v[0], m_Y + v[1] );
00137 }
00138
00139 inline Point2D operator+ ( const Point2D& p ) const
00140 {
00141 return Point2D ( m_X + p.x(), m_Y + p.y() );
00142 }
00143
00144 inline CVec2 operator- ( const Point2D& p ) const
00145 {
00146 return CVec2 ( m_X - p.x(), m_Y - p.y() );
00147 }
00148
00149 inline Point2D operator- ( const CVec2& v ) const
00150 {
00151 return Point2D ( m_X - v[0], m_Y - v[1] );
00152 }
00153
00154 inline Point2D operator* ( double scalar ) const
00155 {
00156 return Point2D ( m_X * scalar, m_Y * scalar );
00157 }
00158
00159 inline Point2D operator/ ( double scalar ) const
00160 {
00161 return Point2D ( m_X / scalar, m_Y / scalar );
00162 }
00163
00164 inline Point2D& operator+= ( const CVec2& v )
00165 {
00166 m_X += v[0];
00167 m_Y += v[1];
00168 return ( *this );
00169 }
00170
00171 inline Point2D& operator-= ( const CVec2& v )
00172 {
00173 m_X -= v[0];
00174 m_Y -= v[1];
00175 return ( *this );
00176 }
00177
00178 inline Point2D& operator*= ( double scalar )
00179 {
00180 m_X *= scalar;
00181 m_Y *= scalar;
00182 return ( *this );
00183 }
00184
00185 inline Point2D& operator/= ( double scalar )
00186 {
00187 m_X /= scalar;
00188 m_Y /= scalar;
00189 return ( *this );
00190 }
00191
00192 inline double operator [] ( unsigned int i ) const
00193 {
00194 return ( ( double* ) this ) [i];
00195 }
00196
00197 inline double& operator [] ( unsigned int i )
00198 {
00199 return ( ( double* ) this ) [i];
00200 }
00201
00202 inline bool operator== ( const Point2D& point ) const
00203 {
00204 return ( fabs(m_X - point.x()) < 0.001 && fabs(m_Y - point.y()) < 0.001 );
00205 }
00206
00207 inline bool operator!= ( const Point2D& point ) const
00208 {
00209 return !((*this)== point);
00210 }
00211
00217 inline double distance ( const Point2D& point ) const
00218 {
00219 return sqrt ( ( m_X-point.x() ) * ( m_X-point.x() ) + ( m_Y-point.y() ) * ( m_Y-point.y() ) );
00220 }
00221
00226 inline double distance ( ) const
00227 {
00228 return sqrt ( m_X * m_X + m_Y * m_Y );
00229 }
00230
00236 inline bool equal ( const Point2D& point ) const
00237 {
00238 if ( ( *this - point ).magnitude() < 0.0001 )
00239 {
00240 return true;
00241 }
00242 else
00243 {
00244 return false;
00245 }
00246 }
00247
00252 inline CVec2 toVector() const
00253 {
00254 return CVec2 ( m_X, m_Y );
00255 }
00256
00261 float getPolarAngle () const;
00262
00268 inline void rotate ( const Point2D& center, float angle )
00269 {
00270 double x0=m_X-center.m_X;
00271 double y0=m_Y-center.m_Y;
00272 double xRot = x0*cos ( angle ) - y0*sin ( angle );
00273 double yRot = x0*sin ( angle ) + y0*cos ( angle );
00274 m_X = xRot+center.m_X;
00275 m_Y = yRot+center.m_Y;
00276 }
00277
00282 inline void rotate ( float angle )
00283 {
00284 double xRot = m_X*cos ( angle ) - m_Y*sin ( angle );
00285 double yRot = m_X*sin ( angle ) + m_Y*cos ( angle );
00286 m_X = xRot;
00287 m_Y = yRot;
00288 }
00289
00294 inline std::string toString() const
00295 {
00296 std::ostringstream str;
00297 str << m_X << " " << m_Y;
00298 return str.str();
00299 }
00300
00302 static Point2D invalidPoint() { return Point2D( DBL_MAX, DBL_MAX ); }
00303
00304 bool isValid() { return ( ( m_X != DBL_MAX ) || ( m_Y != DBL_MAX ) ); }
00305
00306 protected:
00307
00308 double m_X;
00309 double m_Y;
00310 };
00311
00312 #endif