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