- Main Page
- Classes
- Files
- Examples

- File List
- File Members

# include/board/Point.h

Go to the documentation of this file.

    00001 /* -*- mode: c++ -*- */
    00010 #ifndef _BOARD_POINT_H_
    00011 #define _BOARD_POINT_H_
    00012
    00013 #include <cmath>
    00014
    00015 namespace LibBoard {
    00016
    00021 struct Point {
    00022   double x;
    00023   double y;
    00031   Point():x(0.0),y(0.0) { }
    00032
    00039   Point( const Point & other ):x(other.x),y(other.y) { }
    00040
    00047   Point( double x, double y ):x(x),y(y) { }
    00048
    00054   inline Point & rotate( double angle );
    00055
    00062   inline void get( double & x, double & y ) const;
    00063
    00071   inline Point rotated( double angle ) const;
    00072
    00073   inline Point & rotate( double angle, const Point & center );
    00074
    00075   inline Point & rotated( double angle, const Point & center ) const;
    00076
    00077   inline Point & operator+=( const Point & other );
    00078
    00079   inline Point & operator-=( const Point & other );
    00080
    00081   inline Point & operator*=( double s );
    00082
    00083   inline Point & operator/=( double s );
    00084
    00085   inline Point operator-();
    00086
    00087   inline double norm() const;
    00088
    00089 };
    00090
    00091 inline void
    00092 Point::get( double & x, double & y ) const
    00093 {
    00094   x = Point::x;
    00095   y = Point::y;
    00096 }
    00097
    00098 inline Point
    00099 operator+( const Point & a, const Point & b )
    00100 {
    00101   return Point( a.x + b.x, a.y + b.y );
    00102 }
    00103
    00104 inline Point
    00105 operator-( const Point & a, const Point & b )
    00106 {
    00107   return Point( a.x - b.x, a.y - b.y );
    00108 }
    00109
    00110 inline double
    00111 operator*( const Point & a, const Point & b )
    00112 {
    00113   return a.x * b.x + a.y * b.y;
    00114 }
    00115
    00116 inline Point
    00117 operator*( const Point & p, double s  )
    00118 {
    00119   return Point( p.x * s, p.y * s );
    00120 }
    00121
    00122 inline Point
    00123 operator*( double s, const Point & p )
    00124 {
    00125   return Point( s * p.x, s * p.y );
    00126 }
    00127
    00128 inline Point
    00129 operator/( const Point & p, double s )
    00130 {
    00131   return Point( p.x / s, p.y / s );
    00132 }
    00133
    00134 inline Point &
    00135 Point::operator+=( const Point & other )
    00136 {
    00137   x += other.x;
    00138   y += other.y;
    00139   return *this;
    00140 }
    00141
    00142 inline Point &
    00143 Point::operator-=( const Point & other )
    00144 {
    00145   x -= other.x;
    00146   y -= other.y;
    00147   return *this;
    00148 }
    00149
    00150 inline Point &
    00151 Point::operator*=( double s )
    00152 {
    00153   x *= s;
    00154   y *= s;
    00155   return *this;
    00156 }
    00157
    00158 inline Point &
    00159 Point::operator/=( double s )
    00160 {
    00161   x /= s;
    00162   y /= s;
    00163   return *this;
    00164 }
    00165
    00166 inline bool
    00167 operator==( const Point & a, const Point & b )
    00168 {
    00169   return ( a.x == b.x ) && ( a.y == b.y ) ;
    00170 }
    00171
    00172 inline bool
    00173 operator!=( const Point & a, const Point & b )
    00174 {
    00175   return ( a.x != b.x ) || ( a.y != b.y ) ;
    00176 }
    00177
    00178 Point &
    00179 Point::rotate( double angle )
    00180 {
    00181   double x = cos( angle ) * Point::x - sin( angle ) * Point::y;
    00182   double y = sin( angle ) * Point::x + cos( angle ) * Point::y;
    00183   Point::x = x;
    00184   Point::y = y;
    00185   return *this;
    00186 }
    00187
    00188 Point
    00189 Point::rotated( double angle ) const
    00190 {
    00191   return Point(*this).rotate( angle );
    00192 }
    00193
    00194 Point &
    00195 Point::rotate( double angle, const Point & center )
    00196 {
    00197   (*this) -= center;
    00198   (*this).rotate( angle );
    00199   (*this) += center;
    00200   return *this;
    00201 }
    00202
    00203 Point &
    00204 Point::rotated( double angle, const Point & center ) const
    00205 {
    00206   return Point(*this).rotate( angle, center );
    00207 }
    00208
    00209 double
    00210 Point::norm() const
    00211 {
    00212   return sqrt( x*x + y*y );
    00213 }
    00214
    00215 Point Point::operator-()
    00216 {
    00217   return Point( -x, -y );
    00218 }
    00219
    00220 } // mamespace BoardLib
    00221
    00222 #endif // _POINT_H_
    00223