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 <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  * \param precision precision
172  * \return true if vectors are equal; else false
173  */
174  bool equals(const JVector2D& vector,
175  const double precision = std::numeric_limits<double>::min()) const
176  {
177  return (fabs(getX() - vector.getX()) <= precision &&
178  fabs(getY() - vector.getY()) <= precision);
179  }
180 
181 
182  /**
183  * Get length squared.
184  *
185  * \return square of length
186  */
187  double getLengthSquared() const
188  {
189  return getX()*getX() + getY()*getY();
190  }
191 
192 
193  /**
194  * Get length.
195  *
196  * \return length
197  */
198  double getLength() const
199  {
200  return sqrt(getLengthSquared());
201  }
202 
203 
204  /**
205  * Get squared of distance to point.
206  *
207  * \param point point
208  * \return square of distance
209  */
210  double getDistanceSquared(const JVector2D& point) const
211  {
212  return JVector2D(point).sub(*this).getLengthSquared();
213  }
214 
215 
216  /**
217  * Get distance to point.
218  *
219  * \param point point
220  * \return distance
221  */
222  double getDistance(const JVector2D& point) const
223  {
224  return sqrt(getDistanceSquared(point));
225  }
226 
227 
228  /**
229  * Get dot product.
230  *
231  * \param point vector
232  * \return dot product
233  */
234  double getDot(const JVector2D& point) const
235  {
236  return
237  getX() * point.getX() +
238  getY() * point.getY();
239  }
240 
241 
242  /**
243  * Get perpendicular dot product.
244  *
245  * \param point vector
246  * \return perpendicular dot product
247  */
248  double getPerpDot(const JVector2D& point) const
249  {
250  return
251  getX() * point.getY() -
252  getY() * point.getX();
253  }
254 
255  protected:
256  double __x;
257  double __y;
258  };
259 
260 
261  /**
262  * Check sequence of three points.
263  *
264  * \param a 1st point
265  * \param b 2nd point
266  * \param c 3rd point
267  * \return true if points a, b, c are counter clockwise; else false
268  */
269  inline bool getCCW(const JVector2D& a,
270  const JVector2D& b,
271  const JVector2D& c)
272  {
273  const double A = a.getX() - b.getX();
274  const double B = a.getY() - b.getY();
275  const double C = c.getX() - b.getX();
276  const double D = c.getY() - b.getY();
277 
278  return (A*D - B*C) <= 0.0;
279  }
280 }
281 
282 #endif
Data structure for vector in two dimensions.
Definition: JVector2D.hh:31
void transform(double &__x, double &__y) const
Transform.
double getLengthSquared() const
Get length squared.
Definition: JVector2D.hh:187
do echo Generating $dir eval D
Definition: JDrawLED.sh:50
JVector2D(const double x, const double y)
Constructor.
Definition: JVector2D.hh:50
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:234
JVector2D & mul(const double factor)
Scale vector.
Definition: JVector2D.hh:129
JVector2D()
Default constructor.
Definition: JVector2D.hh:38
double getDistanceSquared(const JVector2D &point) const
Get squared of distance to point.
Definition: JVector2D.hh:210
JVector2D & transform(const JMatrix2D &T)
Transform.
Definition: JVector2D.hh:159
double getDistance(const JVector2D &point) const
Get distance to point.
Definition: JVector2D.hh:222
JTOOLS::JRange< double > JRangeX
Type definition of range along x-axis.
Definition: JVector2D.hh:22
bool equals(const JVector2D &vector, const double precision=std::numeric_limits< double >::min()) const
Check equality.
Definition: JVector2D.hh:174
fi JEventTimesliceWriter a
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:73
JVector2D & sub(const JVector2D &vector)
Subtract vector.
Definition: JVector2D.hh:114
JVector2D & negate()
Negate vector.
Definition: JVector2D.hh:84
double getX() const
Get x position.
Definition: JVector2D.hh:62
do set_variable OUTPUT_DIRECTORY $WORKDIR T
JVector2D & div(const double factor)
Scale vector.
Definition: JVector2D.hh:144
2 x 2 matrix
JVector2D & add(const JVector2D &vector)
Add vector.
Definition: JVector2D.hh:99
Auxiliary class to define a range between two values.
JTOOLS::JRange< double > JRangeY
Type definition of range along y-axis.
Definition: JVector2D.hh:23
double getPerpDot(const JVector2D &point) const
Get perpendicular dot product.
Definition: JVector2D.hh:248
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:198
source $JPP_DIR setenv csh $JPP_DIR eval JShellParser o a A