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