Board 0.9.6
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
35namespace LibBoard
36{
37
42struct 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
237};
238
246Point mix(const Point & a, const Point & b, double t);
247
254bool orthogonal(const Point & a, const Point & b);
255
262inline Point operator+(const Point & a, const Point & b)
263{
264 return Point(a.x + b.x, a.y + b.y);
265}
266
273inline Point operator-(const Point & a, const Point & b)
274{
275 return Point(a.x - b.x, a.y - b.y);
276}
277
284inline double operator*(const Point & a, const Point & b)
285{
286 return a.x * b.x + a.y * b.y;
287}
288
295inline Point operator*(const Point & p, double s)
296{
297 return Point(p.x * s, p.y * s);
298}
299
306inline Point operator*(double s, const Point & p)
307{
308 return Point(s * p.x, s * p.y);
309}
310
317inline Point operator/(const Point & p, double s)
318{
319 return Point(p.x / s, p.y / s);
320}
321
328inline bool operator==(const Point & a, const Point & b)
329{
330#pragma clang diagnostic push
331#pragma clang diagnostic ignored "-Wfloat-equal"
332 return (a.x == b.x) && (a.y == b.y);
333#pragma clang diagnostic pop
334}
335
342inline bool operator!=(const Point & a, const Point & b)
343{
344#pragma clang diagnostic push
345#pragma clang diagnostic ignored "-Wfloat-equal"
346 return (a.x != b.x) || (a.y != b.y);
347#pragma clang diagnostic push
348}
349
356inline bool almostEqual(const Point & a, const Point & b)
357{
358 return Tools::almostEqual((a - b).norm(), 0.0);
359}
360
361// Inline methods
362
363inline Point & Point::operator+=(const Point & other)
364{
365 x += other.x;
366 y += other.y;
367 return *this;
368}
369
370inline Point & Point::operator-=(const Point & other)
371{
372 x -= other.x;
373 y -= other.y;
374 return *this;
375}
376
377inline Point & Point::operator*=(double s)
378{
379 x *= s;
380 y *= s;
381 return *this;
382}
383
384inline Point & Point::operator/=(double s)
385{
386 x /= s;
387 y /= s;
388 return *this;
389}
390
392{
393 x = other.x;
394 y = other.y;
395 return *this;
396}
397
398void Point::get(double & x, double & y) const
399{
400 x = Point::x;
401 y = Point::y;
402}
403
404Point & Point::rotate(double angle)
405{
406 const double x = cos(angle) * Point::x - sin(angle) * Point::y;
407 const double y = sin(angle) * Point::x + cos(angle) * Point::y;
408 Point::x = x;
409 Point::y = y;
410 return *this;
411}
412
413Point Point::rotated(double angle) const
414{
415 return Point(*this).rotate(angle);
416}
417
418Point & Point::rotate(double angle, const Point & center)
419{
420 (*this) -= center;
421 (*this).rotate(angle);
422 (*this) += center;
423 return *this;
424}
425
426Point Point::rotated(double angle, const Point & center) const
427{
428 return Point(*this).rotate(angle, center);
429}
430
432{
433 return Point(-y, x);
434}
435
436Point & Point::translate(double dx, double dy)
437{
438 x += dx;
439 y += dy;
440 return *this;
441}
442
443Point Point::translated(double dx, double dy) const
444{
445 return Point(x + dx, y + dy);
446}
447
448Point & Point::scale(double sx, double sy)
449{
450 x *= sx;
451 y *= sy;
452 return *this;
453}
454
455Point Point::scaled(double sx, double sy)
456{
457 return Point(*this).scale(sx, sy);
458}
459
460double Point::norm() const
461{
462 return std::hypot(x, y);
463}
464
466{
467 return (*this) / norm();
468}
469
471{
472 double n = norm();
473 x /= n;
474 y /= n;
475 return *this;
476}
477
478double Point::argument() const
479{
480 return std::atan2(y, x);
481}
482
483bool Point::isInf() const
484{
485 return (*this) == Point::Infinity;
486}
487
489{
490 return Point(-x, -y);
491}
492
493} // namespace LibBoard
494
495std::ostream & operator<<(std::ostream & out, const LibBoard::Point &);
496
497std::ostream & operator<<(std::ostream & out, const std::vector<LibBoard::Point> &);
498
499#endif // BOARD_POINT_H
std::ostream & operator<<(std::ostream &out, const LibBoard::Point &)
Definition Point.cpp:48
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
bool almostEqual(const double &a, const double &b)
Definition Tools.h:150
Definition Board.h:55
Path mix(const Path &a, const Path &b, double time)
Interpolate two paths according to a time (0 is a, 1 is b)
Definition Path.cpp:390
Point operator+(const Point &a, const Point &b)
Compute the sum of two vectors.
Definition Point.h:262
Point operator-(const Point &a, const Point &b)
Compute the difference between two vectors.
Definition Point.h:273
Point operator/(const Point &p, double s)
Compute the division of a vector by a scalar.
Definition Point.h:317
bool orthogonal(const Point &a, const Point &b)
Check if two vectors are orthogonals.
Definition Point.cpp:41
bool almostEqual(const Point &a, const Point &b)
Check if two points are almost equal according to Tools::almostEqual.
Definition Point.h:356
bool operator!=(const Point &a, const Point &b)
Check if two points are different.
Definition Point.h:342
bool operator==(const Point &a, const Point &b)
Check if two points are equal.
Definition Point.h:328
double operator*(const Point &a, const Point &b)
Compute the scalar product of two vectors.
Definition Point.h:284
Struct representing a 2D point.
Definition Point.h:42
Point(double x, double y)
Definition Point.h:69
Point & operator-=(const Point &other)
Definition Point.h:370
Point & operator+=(const Point &other)
Definition Point.h:363
Point & operator/=(double s)
Definition Point.h:384
double y
Definition Point.h:44
Point translated(double dx, double dy) const
Definition Point.h:443
Point normalised() const
Definition Point.h:465
Point & operator*=(double s)
Definition Point.h:377
void get(double &x, double &y) const
Definition Point.h:398
bool isInf() const
Definition Point.h:483
Point rotated(double angle) const
Definition Point.h:413
static Point Infinity
Definition Point.h:236
Point operator-() const
Definition Point.h:488
Point(const Point &other)
Definition Point.h:55
double x
Definition Point.h:43
Point & rotate(double angle)
Definition Point.h:404
double norm() const
Definition Point.h:460
double argument() const
Definition Point.h:478
Point & normalise()
Definition Point.h:470
Point rotatedPI2() const
Definition Point.h:431
Point & translate(double dx, double dy)
Definition Point.h:436
Point()
Definition Point.h:49
Point & scale(double sx, double sy)
Definition Point.h:448
Point scaled(double sx, double sy)
Definition Point.h:455
Point & operator=(const Point &other)
Definition Point.h:391