Point2D.cpp
Go to the documentation of this file.
1 //
2 // Point2D.cpp
3 // A point in the 2-D plane.
4 //
5 
6 #include <cmath>
7 #include "Point2D.hpp"
8 #include "../BasicDatatypes.hpp"
9 
10 namespace datatypes
11 {
12 
14 {
15  return hypot(m_x - point.m_x, m_y - point.m_y);
16 }
17 
19 {
20  const value_type x = getX() - point.getX();
21  const value_type y = getY() - point.getY();
22  return x * x + y * y;
23 }
24 
26 {
27  Point2D p;
28  p.setPolar(r, angle);
29  return p;
30 }
31 
32 
34 {
35  *this = rotated(angle);
36 }
37 
39 {
40  value_type dCos = cos(angle_rad);
41  value_type dSin = sin(angle_rad);
42  return Point2D(m_x * dCos - m_y * dSin,
43  m_x * dSin + m_y * dCos);
44 }
45 
47 {
48  Point2D result(*this);
49  result.normalize();
50  return result;
51 }
52 
54 {
55  if (isZero())
56  // Vector has zero length. Such a vector will be left unchanged.
57  return;
58 
59  // The division below will be done in float, not in double! Hence,
60  // we must retrieve the length in float already as well.
61  value_type len = dist();
62 
63  // If isZero() was false above, the distance cannot be zero
64  // anyway, so checking for this would only result in unreachable
65  // code.
66  assert (!fuzzyCompare(len, value_type(0.0)));
67 
68  *this /= len;
69 }
70 
75 std::string Point2D::toString(UINT16 digits) const
76 {
77  std::string text = "(" + ::toString(getX(), (int)digits) + ", " + ::toString(getY(), (int)digits) + ")";
78  return text;
79 }
80 
81 
82 } // namespace datatypes
std::string toString(UINT16 digits=2) const
Text output for debugging.
Definition: Point2D.cpp:75
value_type m_y
Definition: Point2D.hpp:38
value_type getX() const
Definition: Point2D.hpp:70
uint16_t UINT16
value_type m_x
Definition: Point2D.hpp:37
double hypot(double x, double y, double z)
Definition: MathToolbox.cpp:19
value_type dist() const
Definition: Point2D.hpp:353
value_type getY() const
Definition: Point2D.hpp:73
bool isZero() const
Definition: Point2D.hpp:296
void rotate(value_type angle)
Definition: Point2D.cpp:33
static Point2D fromPolar(value_type dist, value_type angle)
Definition: Point2D.cpp:25
void setPolar(value_type dist, value_type angle)
Definition: Point2D.hpp:261
Point2D rotated(value_type angle_rad) const
Definition: Point2D.cpp:38
Point2D normalized() const
Definition: Point2D.cpp:46
value_type angle() const
Definition: Point2D.hpp:358
double value_type
The type of the stored x and y coordinates.
Definition: Point2D.hpp:31
bool fuzzyCompare(double a, double b)
Tests if two double values are nearly equal.
Definition: MathToolbox.hpp:28
value_type distSquare(const Point2D &point) const
Definition: Point2D.cpp:18


libsick_ldmrs
Author(s): SICK AG , Martin Günther , Jochen Sprickerhof
autogenerated on Sat Jun 8 2019 17:57:33