00001
00010 #ifndef _BOARD_POINT_H_
00011 #define _BOARD_POINT_H_
00012
00013 #include <cmath>
00014
00015 namespace BoardLib {
00016
00021 struct Point {
00022 double x;
00023 double y;
00030 Point( double x = 0.0, double y = 0.0 ):x(x),y(y) { }
00031
00037 inline Point & rotate( double angle );
00038
00045 inline void get( double & x, double & y ) const;
00046
00054 inline Point getRotate( double angle ) const;
00055
00056 inline Point & rotate( double angle, const Point & center );
00057
00058 inline Point & getRotate( double angle, const Point & center ) const;
00059
00060 inline Point & operator+=( const Point & other );
00061
00062 inline Point & operator-=( const Point & other );
00063
00064 inline Point & operator*=( double s );
00065
00066 inline Point & operator/=( double s );
00067
00068 inline double norm() const;
00069
00070 };
00071
00072 inline void
00073 Point::get( double & x, double & y ) const
00074 {
00075 x = Point::x;
00076 y = Point::y;
00077 }
00078
00079 inline Point
00080 operator+( const Point & a, const Point & b )
00081 {
00082 return Point( a.x + b.x, a.y + b.y );
00083 }
00084
00085 inline Point
00086 operator-( const Point & a, const Point & b )
00087 {
00088 return Point( a.x - b.x, a.y - b.y );
00089 }
00090
00091 inline double
00092 operator*( const Point & a, const Point & b )
00093 {
00094 return a.x * b.x + a.y * b.y;
00095 }
00096
00097 inline Point
00098 operator*( const Point & p, double s )
00099 {
00100 return Point( p.x * s, p.y * s );
00101 }
00102
00103 inline Point
00104 operator*( double s, const Point & p )
00105 {
00106 return Point( s * p.x, s * p.y );
00107 }
00108
00109 inline Point
00110 operator/( const Point & p, double s )
00111 {
00112 return Point( p.x / s, p.y / s );
00113 }
00114
00115 inline Point &
00116 Point::operator+=( const Point & other )
00117 {
00118 x += other.x;
00119 y += other.y;
00120 return *this;
00121 }
00122
00123 inline Point &
00124 Point::operator-=( const Point & other )
00125 {
00126 x -= other.x;
00127 y -= other.y;
00128 return *this;
00129 }
00130
00131 inline Point &
00132 Point::operator*=( double s )
00133 {
00134 x *= s;
00135 y *= s;
00136 return *this;
00137 }
00138
00139 inline Point &
00140 Point::operator/=( double s )
00141 {
00142 x /= s;
00143 y /= s;
00144 return *this;
00145 }
00146
00147 inline bool
00148 operator==( const Point & a, const Point & b )
00149 {
00150 return ( a.x == b.x ) && ( a.y == b.y ) ;
00151 }
00152
00153 inline bool
00154 operator!=( const Point & a, const Point & b )
00155 {
00156 return ( a.x != b.x ) || ( a.y != b.y ) ;
00157 }
00158
00159 Point &
00160 Point::rotate( double angle )
00161 {
00162 double x = cos( angle ) * Point::x - sin( angle ) * Point::y;
00163 double y = sin( angle ) * Point::x + cos( angle ) * Point::y;
00164 Point::x = x;
00165 Point::y = y;
00166 return *this;
00167 }
00168
00169 Point
00170 Point::getRotate( double angle ) const
00171 {
00172 return Point(*this).rotate( angle );
00173 }
00174
00175 Point &
00176 Point::rotate( double angle, const Point & center )
00177 {
00178 (*this) -= center;
00179 (*this).rotate( angle );
00180 (*this) += center;
00181 return *this;
00182 }
00183
00184 Point &
00185 Point::getRotate( double angle, const Point & center ) const
00186 {
00187 return Point(*this).rotate( angle, center );
00188 }
00189
00190 double
00191 Point::norm() const
00192 {
00193 return sqrt( x*x + y*y );
00194 }
00195
00196 }
00197
00198 #endif // _POINT_H_
00199