Board  0.9.2
Point.h
Go to the documentation of this file.
1 /* -*- mode: c++ -*- */
26 #ifndef _BOARD_POINT_H_
27 #define _BOARD_POINT_H_
28 
29 #include <cmath>
30 #include <iostream>
31 
32 namespace LibBoard {
33 
38 struct Point {
39  double x;
40  double y;
48  Point():x(0.0),y(0.0) { }
49 
56  Point( const Point & other ):x(other.x),y(other.y) { }
57 
64  Point( double x, double y ):x(x),y(y) { }
65 
72  inline void get( double & x, double & y ) const;
73 
79  inline Point & rotate( double angle );
80 
88  inline Point rotated( double angle ) const;
89 
98  inline Point & rotate( double angle, const Point & center );
99 
108  inline Point rotated( double angle, const Point & center ) const;
109 
117  inline Point & operator+=( const Point & other );
118 
126  inline Point & operator-=( const Point & other );
127 
128 
136  inline Point & operator*=( double s );
137 
145  inline Point & operator/=( double s );
146 
152  inline Point operator-() const;
153 
159  inline double norm() const;
160 
161 };
162 
163 inline void
164 Point::get( double & x, double & y ) const
165 {
166  x = Point::x;
167  y = Point::y;
168 }
169 
170 inline Point
171 operator+( const Point & a, const Point & b )
172 {
173  return Point( a.x + b.x, a.y + b.y );
174 }
175 
176 inline Point
177 operator-( const Point & a, const Point & b )
178 {
179  return Point( a.x - b.x, a.y - b.y );
180 }
181 
182 inline double
183 operator*( const Point & a, const Point & b )
184 {
185  return a.x * b.x + a.y * b.y;
186 }
187 
188 inline Point
189 operator*( const Point & p, double s )
190 {
191  return Point( p.x * s, p.y * s );
192 }
193 
194 inline Point
195 operator*( double s, const Point & p )
196 {
197  return Point( s * p.x, s * p.y );
198 }
199 
200 inline Point
201 operator/( const Point & p, double s )
202 {
203  return Point( p.x / s, p.y / s );
204 }
205 
206 inline Point &
207 Point::operator+=( const Point & other )
208 {
209  x += other.x;
210  y += other.y;
211  return *this;
212 }
213 
214 inline Point &
215 Point::operator-=( const Point & other )
216 {
217  x -= other.x;
218  y -= other.y;
219  return *this;
220 }
221 
222 inline Point &
223 Point::operator*=( double s )
224 {
225  x *= s;
226  y *= s;
227  return *this;
228 }
229 
230 inline Point &
231 Point::operator/=( double s )
232 {
233  x /= s;
234  y /= s;
235  return *this;
236 }
237 
238 inline bool
239 operator==( const Point & a, const Point & b )
240 {
241  return ( a.x == b.x ) && ( a.y == b.y ) ;
242 }
243 
244 inline bool
245 operator!=( const Point & a, const Point & b )
246 {
247  return ( a.x != b.x ) || ( a.y != b.y ) ;
248 }
249 
250 Point &
251 Point::rotate( double angle )
252 {
253  double x = cos( angle ) * Point::x - sin( angle ) * Point::y;
254  double y = sin( angle ) * Point::x + cos( angle ) * Point::y;
255  Point::x = x;
256  Point::y = y;
257  return *this;
258 }
259 
260 Point
261 Point::rotated( double angle ) const
262 {
263  return Point(*this).rotate( angle );
264 }
265 
266 Point &
267 Point::rotate( double angle, const Point & center )
268 {
269  (*this) -= center;
270  (*this).rotate( angle );
271  (*this) += center;
272  return *this;
273 }
274 
275 Point
276 Point::rotated( double angle, const Point & center ) const
277 {
278  return Point(*this).rotate( angle, center );
279 }
280 
281 double
282 Point::norm() const
283 {
284  return sqrt( x*x + y*y );
285 }
286 
287 Point
289 {
290  return Point( -x, -y );
291 }
292 
293 inline
294 std::ostream &
295 operator<<( std::ostream & out, const Point & p )
296 {
297  return out << "Point(" << p.x << "," << p.y << ")";
298 }
299 
300 } // mamespace BoardLib
301 
302 #endif // _POINT_H_
Point operator-() const
Definition: Point.h:288
Point & rotate(double angle)
Definition: Point.h:251
Definition: Board.h:40
Point(const Point &other)
Definition: Point.h:56
Struct representing a 2D point.
Definition: Point.h:38
Point(double x, double y)
Definition: Point.h:64
Point rotated(double angle) const
Definition: Point.h:261
double x
Definition: Point.h:39
Point & operator+=(const Point &other)
Definition: Point.h:207
Point & operator*=(double s)
Definition: Point.h:223
double norm() const
Definition: Point.h:282
Point & operator-=(const Point &other)
Definition: Point.h:215
Point & operator/=(double s)
Definition: Point.h:231
double y
Definition: Point.h:40
Point()
Definition: Point.h:48
void get(double &x, double &y) const
Definition: Point.h:164