Board  0.9.4
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 #include <vector>
32 
33 namespace LibBoard {
34 
39 struct Point {
40  double x;
41  double y;
49  Point():x(0.0),y(0.0) { }
50 
57  Point( const Point & other ):x(other.x),y(other.y) { }
58 
65  Point( double x, double y ):x(x),y(y) { }
66 
73  inline void get( double & x, double & y ) const;
74 
80  inline Point & rotate( double angle );
81 
89  inline Point rotated( double angle ) const;
90 
99  inline Point & rotate( double angle, const Point & center );
100 
109  inline Point rotated( double angle, const Point & center ) const;
110 
116  inline Point rotatedPI2() const;
117 
125  inline Point & operator+=( const Point & other );
126 
134  inline Point & operator-=( const Point & other );
135 
136 
144  inline Point & operator*=( double s );
145 
153  inline Point & operator/=( double s );
154 
160  inline Point operator-() const;
161 
167  inline double norm() const;
168 
174  inline Point & normalise();
175 
181  inline Point normalised() const;
182 
188  inline double argument() const;
189 
195  inline bool isInf() const;
196 
197  static Point Infinity;
198 
199 };
200 
201 inline void
202 Point::get( double & x, double & y ) const
203 {
204  x = Point::x;
205  y = Point::y;
206 }
207 
208 inline Point
209 operator+( const Point & a, const Point & b )
210 {
211  return Point( a.x + b.x, a.y + b.y );
212 }
213 
214 inline Point
215 operator-( const Point & a, const Point & b )
216 {
217  return Point( a.x - b.x, a.y - b.y );
218 }
219 
220 inline double
221 operator*( const Point & a, const Point & b )
222 {
223  return a.x * b.x + a.y * b.y;
224 }
225 
226 inline Point
227 operator*( const Point & p, double s )
228 {
229  return Point( p.x * s, p.y * s );
230 }
231 
232 inline Point
233 operator*( double s, const Point & p )
234 {
235  return Point( s * p.x, s * p.y );
236 }
237 
238 inline Point
239 operator/( const Point & p, double s )
240 {
241  return Point( p.x / s, p.y / s );
242 }
243 
244 inline Point &
245 Point::operator+=( const Point & other )
246 {
247  x += other.x;
248  y += other.y;
249  return *this;
250 }
251 
252 inline Point &
253 Point::operator-=( const Point & other )
254 {
255  x -= other.x;
256  y -= other.y;
257  return *this;
258 }
259 
260 inline Point &
261 Point::operator*=( double s )
262 {
263  x *= s;
264  y *= s;
265  return *this;
266 }
267 
268 inline Point &
269 Point::operator/=( double s )
270 {
271  x /= s;
272  y /= s;
273  return *this;
274 }
275 
276 inline bool
277 operator==( const Point & a, const Point & b )
278 {
279  return ( a.x == b.x ) && ( a.y == b.y ) ;
280 }
281 
282 inline bool
283 operator!=( const Point & a, const Point & b )
284 {
285  return ( a.x != b.x ) || ( a.y != b.y ) ;
286 }
287 
288 Point &
289 Point::rotate( double angle )
290 {
291  double x = cos( angle ) * Point::x - sin( angle ) * Point::y;
292  double y = sin( angle ) * Point::x + cos( angle ) * Point::y;
293  Point::x = x;
294  Point::y = y;
295  return *this;
296 }
297 
298 Point
299 Point::rotated( double angle ) const
300 {
301  return Point(*this).rotate( angle );
302 }
303 
304 Point &
305 Point::rotate( double angle, const Point & center )
306 {
307  (*this) -= center;
308  (*this).rotate( angle );
309  (*this) += center;
310  return *this;
311 }
312 
313 Point
314 Point::rotated( double angle, const Point & center ) const
315 {
316  return Point(*this).rotate( angle, center );
317 }
318 
319 Point
321 {
322  return Point(-y,x);
323 }
324 
325 double
326 Point::norm() const
327 {
328  return std::sqrt( x*x + y*y );
329 }
330 
331 Point
333 {
334  return (*this) / norm();
335 }
336 
337 Point &
339 {
340  double n = norm();
341  x /= n;
342  y /= n;
343  return *this;
344 }
345 
346 double
348 {
349  return std::atan2( y, x );
350 }
351 
352 bool
354 {
355  return (*this) == Point::Infinity;
356 }
357 
358 
359 Point
361 {
362  return Point( -x, -y );
363 }
364 
365 } // mamespace LibBoard
366 
367 std::ostream & operator<<( std::ostream & out, const LibBoard::Point & p );
368 
369 #endif // _POINT_H_
bool isInf() const
Definition: Point.h:353
Point operator-() const
Definition: Point.h:360
Point & rotate(double angle)
Definition: Point.h:289
Definition: Board.h:41
Point(const Point &other)
Definition: Point.h:57
Point normalised() const
Definition: Point.h:332
Point rotatedPI2() const
Definition: Point.h:320
Struct representing a 2D point.
Definition: Point.h:39
Point(double x, double y)
Definition: Point.h:65
Point rotated(double angle) const
Definition: Point.h:299
double x
Definition: Point.h:40
Point & operator+=(const Point &other)
Definition: Point.h:245
double argument() const
Definition: Point.h:347
Point & operator*=(double s)
Definition: Point.h:261
double norm() const
Definition: Point.h:326
Point & operator-=(const Point &other)
Definition: Point.h:253
Point & operator/=(double s)
Definition: Point.h:269
double y
Definition: Point.h:41
Point & normalise()
Definition: Point.h:338
Point()
Definition: Point.h:49
void get(double &x, double &y) const
Definition: Point.h:202