Board  0.9.5
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 <limits>
32 #include <vector>
33 #include "board/Tools.h"
34 
35 namespace LibBoard
36 {
37 
42 struct Point {
43  double x;
44  double y;
49  Point() : x(0.0), y(0.0) {}
50 
55  Point(const Point & other) : x(other.x), y(other.y) {}
56 
61  inline Point & operator=(const Point & other);
62 
69  Point(double x, double y) : x(x), y(y) {}
70 
77  inline void get(double & x, double & y) const;
78 
84  inline Point & rotate(double angle);
85 
93  inline Point rotated(double angle) const;
94 
103  inline Point & rotate(double angle, const Point & center);
104 
113  inline Point rotated(double angle, const Point & center) const;
114 
120  inline Point rotatedPI2() const;
121 
129  inline Point & operator+=(const Point & other);
130 
138  inline Point & translate(double dx, double dy);
139 
147  inline Point translated(double dx, double dy) const;
148 
156  inline Point & scale(double sx, double sy);
157 
165  inline Point scaled(double sx, double sy);
166 
174  inline Point & operator-=(const Point & other);
175 
183  inline Point & operator*=(double s);
184 
192  inline Point & operator/=(double s);
193 
199  inline Point operator-() const;
200 
206  inline double norm() const;
207 
213  inline Point & normalise();
214 
220  inline Point normalised() const;
221 
227  inline double argument() const;
228 
234  inline bool isInf() const;
235 
236  static Point Infinity;
237 };
238 
246 Point mid(const Point & a, const Point & b, double t);
247 
248 bool orthogonal(const Point & a, const Point & b);
249 
250 Point & Point::operator=(const Point & other)
251 {
252  x = other.x;
253  y = other.y;
254  return *this;
255 }
256 
257 inline void Point::get(double & x, double & y) const
258 {
259  x = Point::x;
260  y = Point::y;
261 }
262 
263 inline Point operator+(const Point & a, const Point & b)
264 {
265  return Point(a.x + b.x, a.y + b.y);
266 }
267 
268 inline Point operator-(const Point & a, const Point & b)
269 {
270  return Point(a.x - b.x, a.y - b.y);
271 }
272 
273 inline double operator*(const Point & a, const Point & b)
274 {
275  return a.x * b.x + a.y * b.y;
276 }
277 
278 inline Point operator*(const Point & p, double s)
279 {
280  return Point(p.x * s, p.y * s);
281 }
282 
283 inline Point operator*(double s, const Point & p)
284 {
285  return Point(s * p.x, s * p.y);
286 }
287 
288 inline Point operator/(const Point & p, double s)
289 {
290  return Point(p.x / s, p.y / s);
291 }
292 
293 inline Point & Point::operator+=(const Point & other)
294 {
295  x += other.x;
296  y += other.y;
297  return *this;
298 }
299 
300 inline Point & Point::operator-=(const Point & other)
301 {
302  x -= other.x;
303  y -= other.y;
304  return *this;
305 }
306 
307 inline Point & Point::operator*=(double s)
308 {
309  x *= s;
310  y *= s;
311  return *this;
312 }
313 
314 inline Point & Point::operator/=(double s)
315 {
316  x /= s;
317  y /= s;
318  return *this;
319 }
320 
321 inline bool operator==(const Point & a, const Point & b)
322 {
323 #pragma clang diagnostic push
324 #pragma clang diagnostic ignored "-Wfloat-equal"
325  return (a.x == b.x) && (a.y == b.y);
326 #pragma clang diagnostic pop
327 }
328 
329 inline bool operator!=(const Point & a, const Point & b)
330 {
331 #pragma clang diagnostic push
332 #pragma clang diagnostic ignored "-Wfloat-equal"
333  return (a.x != b.x) || (a.y != b.y);
334 #pragma clang diagnostic push
335 }
336 
337 inline bool almostEqual(const Point & a, const Point & b)
338 {
339  return Tools::almostEqual((a - b).norm(), 0.0);
340 }
341 
342 Point & Point::rotate(double angle)
343 {
344  const double x = cos(angle) * Point::x - sin(angle) * Point::y;
345  const double y = sin(angle) * Point::x + cos(angle) * Point::y;
346  Point::x = x;
347  Point::y = y;
348  return *this;
349 }
350 
351 Point Point::rotated(double angle) const
352 {
353  return Point(*this).rotate(angle);
354 }
355 
356 Point & Point::rotate(double angle, const Point & center)
357 {
358  (*this) -= center;
359  (*this).rotate(angle);
360  (*this) += center;
361  return *this;
362 }
363 
364 Point Point::rotated(double angle, const Point & center) const
365 {
366  return Point(*this).rotate(angle, center);
367 }
368 
370 {
371  return Point(-y, x);
372 }
373 
374 Point & Point::translate(double dx, double dy)
375 {
376  x += dx;
377  y += dy;
378  return *this;
379 }
380 
381 Point Point::translated(double dx, double dy) const
382 {
383  return Point(x + dx, y + dy);
384 }
385 
386 Point & Point::scale(double sx, double sy)
387 {
388  x *= sx;
389  y *= sy;
390  return *this;
391 }
392 
393 Point Point::scaled(double sx, double sy)
394 {
395  return Point(*this).scale(sx, sy);
396 }
397 
398 double Point::norm() const
399 {
400  return std::hypot(x, y);
401 }
402 
404 {
405  return (*this) / norm();
406 }
407 
409 {
410  double n = norm();
411  x /= n;
412  y /= n;
413  return *this;
414 }
415 
416 double Point::argument() const
417 {
418  return std::atan2(y, x);
419 }
420 
421 bool Point::isInf() const
422 {
423  return (*this) == Point::Infinity;
424 }
425 
427 {
428  return Point(-x, -y);
429 }
430 
431 } // namespace LibBoard
432 
433 std::ostream & operator<<(std::ostream & out, const LibBoard::Point &);
434 
435 std::ostream & operator<<(std::ostream & out, const std::vector<LibBoard::Point> &);
436 
437 #endif // BOARD_POINT_H
LibBoard::Point::operator/=
Point & operator/=(double s)
Definition: Point.h:314
LibBoard::Point::operator-
Point operator-() const
Definition: Point.h:426
LibBoard::Point::get
void get(double &x, double &y) const
Definition: Point.h:257
LibBoard::Point::normalised
Point normalised() const
Definition: Point.h:403
LibBoard::Point::Point
Point(const Point &other)
Definition: Point.h:55
LibBoard::Point::scale
Point & scale(double sx, double sy)
Definition: Point.h:386
LibBoard::Point::rotate
Point & rotate(double angle)
Definition: Point.h:342
LibBoard::Point::argument
double argument() const
Definition: Point.h:416
LibBoard::Point::operator=
Point & operator=(const Point &other)
Definition: Point.h:250
LibBoard::Point::Point
Point(double x, double y)
Definition: Point.h:69
LibBoard::Point::x
double x
Definition: Point.h:43
LibBoard::Point
Struct representing a 2D point.
Definition: Point.h:42
LibBoard::Point::operator+=
Point & operator+=(const Point &other)
Definition: Point.h:293
LibBoard::Point::rotatedPI2
Point rotatedPI2() const
Definition: Point.h:369
LibBoard::Point::translate
Point & translate(double dx, double dy)
Definition: Point.h:374
LibBoard::Point::scaled
Point scaled(double sx, double sy)
Definition: Point.h:393
LibBoard::Point::operator-=
Point & operator-=(const Point &other)
Definition: Point.h:300
LibBoard::Point::normalise
Point & normalise()
Definition: Point.h:408
LibBoard::Point::isInf
bool isInf() const
Definition: Point.h:421
LibBoard::Point::translated
Point translated(double dx, double dy) const
Definition: Point.h:381
LibBoard::Point::operator*=
Point & operator*=(double s)
Definition: Point.h:307
LibBoard::Point::norm
double norm() const
Definition: Point.h:398
Tools.h
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
LibBoard::Point::Point
Point()
Definition: Point.h:49
LibBoard::Point::rotated
Point rotated(double angle) const
Definition: Point.h:351
LibBoard::Point::y
double y
Definition: Point.h:44