Jpp
JVector2D.hh
Go to the documentation of this file.
1 #ifndef __JVECTOR2D__
2 #define __JVECTOR2D__
3 
4 #include <limits>
5 #include <cmath>
6 
7 #include "JTools/JRange.hh"
8 #include "JMath/JMath.hh"
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JGEOMETRY2D {}
17 namespace JPP { using namespace JGEOMETRY2D; }
18 
19 namespace JGEOMETRY2D {
20 
21 
22  typedef JTOOLS::JRange<double> JRangeX; //!< Type definition of range along x-axis.
23  typedef JTOOLS::JRange<double> JRangeY; //!< Type definition of range along y-axis.
24 
25 
26  /**
27  * Data structure for vector in two dimensions.
28  *
29  * This class implements the JMATH::JMath interface.
30  */
31  class JVector2D :
32  public JMATH::JMath<JVector2D>
33  {
34  public:
35  /**
36  * Default constructor.
37  */
39  __x(0.0),
40  __y(0.0)
41  {}
42 
43 
44  /**
45  * Constructor.
46  *
47  * \param x x value
48  * \param y y value
49  */
50  JVector2D(const double x,
51  const double y) :
52  __x(x),
53  __y(y)
54  {}
55 
56 
57  /**
58  * Get x position.
59  *
60  * \return x position
61  */
62  double getX() const
63  {
64  return __x;
65  }
66 
67 
68  /**
69  * Get y position.
70  *
71  * \return y position
72  */
73  double getY() const
74  {
75  return __y;
76  }
77 
78 
79  /**
80  * Negate vector.
81  *
82  * \return this vector
83  */
85  {
86  __x = -__x;
87  __y = -__y;
88 
89  return *this;
90  }
91 
92 
93  /**
94  * Add vector.
95  *
96  * \param vector vector
97  * \return this vector
98  */
99  JVector2D& add(const JVector2D& vector)
100  {
101  __x += vector.getX();
102  __y += vector.getY();
103 
104  return *this;
105  }
106 
107 
108  /**
109  * Subtract vector.
110  *
111  * \param vector vector
112  * \return this vector
113  */
114  JVector2D& sub(const JVector2D& vector)
115  {
116  __x -= vector.getX();
117  __y -= vector.getY();
118 
119  return *this;
120  }
121 
122 
123  /**
124  * Scale vector.
125  *
126  * \param factor multiplication factor
127  * \return this vector
128  */
129  JVector2D& mul(const double factor)
130  {
131  __x *= factor;
132  __y *= factor;
133 
134  return *this;
135  }
136 
137 
138  /**
139  * Scale vector.
140  *
141  * \param factor division factor
142  * \return this vector
143  */
144  JVector2D& div(const double factor)
145  {
146  __x /= factor;
147  __y /= factor;
148 
149  return *this;
150  }
151 
152 
153  /**
154  * Transform.
155  *
156  * \param T matrix
157  * \return this vector
158  */
160  {
161  T.transform(__x, __y);
162 
163  return *this;
164  }
165 
166 
167  /**
168  * Check equality.
169  *
170  * \param vector vector
171  * \return true if vectors are equal; else false
172  */
173  bool equals(const JVector2D& vector,
174  const double precision = std::numeric_limits<double>::min()) const
175  {
176  return (fabs(getX() - vector.getX()) <= precision &&
177  fabs(getY() - vector.getY()) <= precision);
178  }
179 
180 
181  /**
182  * Get length squared.
183  *
184  * \return square of length
185  */
186  double getLengthSquared() const
187  {
188  return getX()*getX() + getY()*getY();
189  }
190 
191 
192  /**
193  * Get length.
194  *
195  * \return length
196  */
197  double getLength() const
198  {
199  return sqrt(getLengthSquared());
200  }
201 
202 
203  /**
204  * Get squared of distance to point.
205  *
206  * \param point point
207  * \return square of distance
208  */
209  double getDistanceSquared(const JVector2D& point) const
210  {
211  return JVector2D(point).sub(*this).getLengthSquared();
212  }
213 
214 
215  /**
216  * Get distance to point.
217  *
218  * \param point point
219  * \return distance
220  */
221  double getDistance(const JVector2D& point) const
222  {
223  return sqrt(getDistanceSquared(point));
224  }
225 
226 
227  /**
228  * Get dot product.
229  *
230  * \param point vector
231  * \return dot product
232  */
233  double getDot(const JVector2D& point) const
234  {
235  return
236  getX() * point.getX() +
237  getY() * point.getY();
238  }
239 
240 
241  /**
242  * Get perpendicular dot product.
243  *
244  * \param point vector
245  * \return perpendicular dot product
246  */
247  double getPerpDot(const JVector2D& point) const
248  {
249  return
250  getX() * point.getY() -
251  getY() * point.getX();
252  }
253 
254  protected:
255  double __x;
256  double __y;
257  };
258 
259 
260  /**
261  * Check sequence of three points.
262  *
263  * \param a 1st point
264  * \param b 2nd point
265  * \param c 3rd point
266  * \return true if points a, b, c are counter clockwise; else false
267  */
268  inline bool getCCW(const JVector2D& a,
269  const JVector2D& b,
270  const JVector2D& c)
271  {
272  const double A = a.getX() - b.getX();
273  const double B = a.getY() - b.getY();
274  const double C = c.getX() - b.getX();
275  const double D = c.getY() - b.getY();
276 
277  return (A*D - B*C) <= 0.0;
278  }
279 }
280 
281 #endif
JGEOMETRY2D::JVector2D::transform
JVector2D & transform(const JMatrix2D &T)
Transform.
Definition: JVector2D.hh:159
JGEOMETRY2D::JVector2D::mul
JVector2D & mul(const double factor)
Scale vector.
Definition: JVector2D.hh:129
JGEOMETRY2D::getCCW
bool getCCW(const JVector2D &a, const JVector2D &b, const JVector2D &c)
Check sequence of three points.
Definition: JVector2D.hh:268
JGEOMETRY2D::JVector2D::div
JVector2D & div(const double factor)
Scale vector.
Definition: JVector2D.hh:144
JGEOMETRY2D::JVector2D::JVector2D
JVector2D(const double x, const double y)
Constructor.
Definition: JVector2D.hh:50
JGEOMETRY2D::JVector2D::getLengthSquared
double getLengthSquared() const
Get length squared.
Definition: JVector2D.hh:186
JTOOLS::JRange< double >
JTOOLS::C
static const double C
Speed of light in vacuum [m/ns].
Definition: JConstants.hh:22
JMATH::JMath
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
JGEOMETRY2D
Auxiliary classes and methods for 2D geometrical objects and operations.
Definition: JAngle2D.hh:18
JGEOMETRY2D::JVector2D::add
JVector2D & add(const JVector2D &vector)
Add vector.
Definition: JVector2D.hh:99
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JGEOMETRY2D::JVector2D::getDistanceSquared
double getDistanceSquared(const JVector2D &point) const
Get squared of distance to point.
Definition: JVector2D.hh:209
JRange.hh
JGEOMETRY2D::JVector2D::equals
bool equals(const JVector2D &vector, const double precision=std::numeric_limits< double >::min()) const
Check equality.
Definition: JVector2D.hh:173
JGEOMETRY2D::JVector2D::__x
double __x
Definition: JVector2D.hh:255
JMATH::JMatrix2D::transform
void transform(double &__x, double &__y) const
Transform.
Definition: JMath/JMatrix2D.hh:284
JGEOMETRY2D::JRangeX
JTOOLS::JRange< double > JRangeX
Type definition of range along x-axis.
Definition: JVector2D.hh:22
JGEOMETRY2D::JVector2D::negate
JVector2D & negate()
Negate vector.
Definition: JVector2D.hh:84
JGEOMETRY2D::JVector2D::getDistance
double getDistance(const JVector2D &point) const
Get distance to point.
Definition: JVector2D.hh:221
JMath.hh
JGEOMETRY2D::JRangeY
JTOOLS::JRange< double > JRangeY
Type definition of range along y-axis.
Definition: JVector2D.hh:23
JGEOMETRY2D::JVector2D::sub
JVector2D & sub(const JVector2D &vector)
Subtract vector.
Definition: JVector2D.hh:114
JGEOMETRY2D::JVector2D::JVector2D
JVector2D()
Default constructor.
Definition: JVector2D.hh:38
JMATH::JMatrix2D
2 x 2 matrix
Definition: JMath/JMatrix2D.hh:32
JGEOMETRY2D::JVector2D::getX
double getX() const
Get x position.
Definition: JVector2D.hh:62
JMatrix2D.hh
JGEOMETRY2D::JVector2D::getLength
double getLength() const
Get length.
Definition: JVector2D.hh:197
JGEOMETRY2D::JVector2D::getY
double getY() const
Get y position.
Definition: JVector2D.hh:73
JGEOMETRY2D::JVector2D::getPerpDot
double getPerpDot(const JVector2D &point) const
Get perpendicular dot product.
Definition: JVector2D.hh:247
JGEOMETRY2D::JVector2D::__y
double __y
Definition: JVector2D.hh:256
JGEOMETRY2D::JVector2D::getDot
double getDot(const JVector2D &point) const
Get dot product.
Definition: JVector2D.hh:233
JGEOMETRY2D::JVector2D
Data structure for vector in two dimensions.
Definition: JVector2D.hh:31