ROOT  6.06/09
Reference Guide
TVector2.h
Go to the documentation of this file.
1 // @(#)root/physics:$Id$
2 // Author: Pasha Murat 12/02/99
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TVector2
13 #define ROOT_TVector2
14 
15 #include "TObject.h"
16 
17 
18 class TVector2 : public TObject {
19 //------------------------------------------------------------------------------
20 // data members
21 //------------------------------------------------------------------------------
22 protected:
23 
24  Double_t fX; // components of the vector
26 //------------------------------------------------------------------------------
27 // function members
28 //------------------------------------------------------------------------------
29 public:
30 
31  typedef Double_t Scalar; // to be able to use it with the ROOT::Math::VectorUtil functions
32 
33  TVector2 ();
34  TVector2 (Double_t *s);
35  TVector2 (Double_t x0, Double_t y0);
36  virtual ~TVector2();
37  // ****** unary operators
38 
39  TVector2& operator = (TVector2 const & v);
40  TVector2& operator += (TVector2 const & v);
41  TVector2& operator -= (TVector2 const & v);
45 
46  // ****** binary operators
47 
48  friend TVector2 operator + (const TVector2&, const TVector2&);
49  friend TVector2 operator + (const TVector2&, Double_t );
50  friend TVector2 operator + (Double_t , const TVector2&);
51  friend TVector2 operator - (const TVector2&, const TVector2&);
52  friend TVector2 operator - (const TVector2&, Double_t );
53  friend Double_t operator * (const TVector2&, const TVector2&);
54  friend TVector2 operator * (const TVector2&, Double_t );
55  friend TVector2 operator * (Double_t , const TVector2&);
56  friend TVector2 operator / (const TVector2&, Double_t );
57  friend Double_t operator ^ (const TVector2&, const TVector2&);
58 
59  // ****** setters
60  void Set(const TVector2& v);
61  void Set(Double_t x0, Double_t y0);
62  void Set(float x0, float y0);
63  void SetX(Double_t x0);
64  void SetY(Double_t y0);
65  // ****** other member functions
66 
67  Double_t Mod2() const { return fX*fX+fY*fY; };
68  Double_t Mod () const;
69 
70  Double_t Px() const { return fX; };
71  Double_t Py() const { return fY; };
72  Double_t X () const { return fX; };
73  Double_t Y () const { return fY; };
74 
75  // phi() is defined in [0,TWOPI]
76 
77  Double_t Phi () const;
78  Double_t DeltaPhi(const TVector2& v) const;
79  void SetMagPhi(Double_t mag, Double_t phi);
80 
81  // unit vector in the direction of *this
82 
83  TVector2 Unit() const;
84  TVector2 Ort () const;
85 
86  // projection of *this to the direction
87  // of TVector2 vector `v'
88 
89  TVector2 Proj(const TVector2& v) const;
90 
91  // component of *this normal to `v'
92 
93  TVector2 Norm(const TVector2& v) const;
94 
95  // rotates 2-vector by phi radians
96  TVector2 Rotate (Double_t phi) const;
97 
98  // returns phi angle in the interval [0,2*PI)
99  static Double_t Phi_0_2pi(Double_t x); // returns phi angle in the interval
100  // returns phi angle in the interval [-PI,PI)
101  static Double_t Phi_mpi_pi(Double_t x);
102 
103 
104  void Print(Option_t* option="") const;
105 
106  ClassDef(TVector2,3) // A 2D physics vector
107 
108 };
109 
110  // ****** unary operators
111 
112 inline TVector2& TVector2::operator = (TVector2 const& v) {fX = v.fX; fY = v.fY; return *this;}
113 inline TVector2& TVector2::operator += (TVector2 const& v) {fX += v.fX; fY += v.fY; return *this;}
114 inline TVector2& TVector2::operator -= (TVector2 const& v) {fX -= v.fX; fY -= v.fY; return *this;}
115 
116  // scalar product of 2 2-vectors
117 
118 inline Double_t TVector2::operator *= (const TVector2& v) { return(fX*v.fX+fY*v.fY); }
119 
120 inline TVector2& TVector2::operator *= (Double_t s) { fX *=s; fY *=s; return *this; }
121 inline TVector2& TVector2::operator /= (Double_t s) { fX /=s; fY /=s; return *this; }
122 
123  // ****** binary operators
124 
125 inline TVector2 operator + (const TVector2& v1, const TVector2& v2) {
126  return TVector2(v1.fX+v2.fX,v1.fY+v2.fY);
127 }
128 
129 inline TVector2 operator + (const TVector2& v1, Double_t bias) {
130  return TVector2 (v1.fX+bias,v1.fY+bias);
131 }
132 
133 inline TVector2 operator + (Double_t bias, const TVector2& v1) {
134  return TVector2 (v1.fX+bias,v1.fY+bias);
135 }
136 
137 inline TVector2 operator - (const TVector2& v1, const TVector2& v2) {
138  return TVector2(v1.fX-v2.fX,v1.fY-v2.fY);
139 }
140 
141 inline TVector2 operator - (const TVector2& v1, Double_t bias) {
142  return TVector2 (v1.fX-bias,v1.fY-bias);
143 }
144 
146  return TVector2 (v.fX*s,v.fY*s);
147 }
148 
150  return TVector2 (v.fX*s,v.fY*s);
151 }
152 
153 inline Double_t operator * (const TVector2& v1, const TVector2& v2) {
154  return v1.fX*v2.fX+v1.fY*v2.fY;
155 }
156 
158  return TVector2 (v.fX/s,v.fY/s);
159 }
160 
161 inline Double_t operator ^ (const TVector2& v1, const TVector2& v2) {
162  return v1.fX*v2.fY-v1.fY*v2.fX;
163 }
164 
165 inline Double_t TVector2::DeltaPhi(const TVector2& v) const { return Phi_mpi_pi(Phi()-v.Phi()); }
166 
167 inline TVector2 TVector2::Ort () const { return Unit(); }
168 
169 inline TVector2 TVector2::Proj(const TVector2& v) const { return v*(((*this)*v)/v.Mod2()); }
170 
171 inline TVector2 TVector2::Norm(const TVector2& v) const {return *this-Proj(v); }
172 
173  // ****** setters
174 
175 inline void TVector2::Set(const TVector2& v ) { fX = v.fX; fY = v.fY; }
176 inline void TVector2::Set(Double_t x0, Double_t y0) { fX = x0 ; fY = y0 ; }
177 inline void TVector2::Set(float x0, float y0) { fX = x0 ; fY = y0 ; }
178 inline void TVector2::SetX(Double_t x0) { fX = x0 ; }
179 inline void TVector2::SetY(Double_t y0) { fY = y0 ; }
180 
181 
182 #endif
friend TVector2 operator/(const TVector2 &, Double_t)
Definition: TVector2.h:157
static Double_t Phi_0_2pi(Double_t x)
(static function) returns phi angle in the interval [0,2*PI)
Definition: TVector2.cxx:82
TVector2 Unit() const
return module normalized to 1
Definition: TVector2.cxx:66
Double_t fY
Definition: TVector2.h:25
Double_t Mod() const
return modulo of this vector
Definition: TVector2.cxx:58
Double_t Py() const
Definition: TVector2.h:71
const Double_t * v1
Definition: TArcBall.cxx:33
void SetY(Double_t y0)
Definition: TVector2.h:179
const char Option_t
Definition: RtypesCore.h:62
friend TVector2 operator+(const TVector2 &, const TVector2 &)
Definition: TVector2.h:125
Double_t fX
Definition: TVector2.h:24
TVector2 operator-(const TVector2 &v1, const TVector2 &v2)
Definition: TVector2.h:137
TVector2 operator*(const TVector2 &v, Double_t s)
Definition: TVector2.h:145
TVector2 Norm(const TVector2 &v) const
Definition: TVector2.h:171
void Set(const TVector2 &v)
Definition: TVector2.h:175
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
static Double_t Phi_mpi_pi(Double_t x)
(static function) returns phi angle in the interval [-PI,PI)
Definition: TVector2.cxx:95
friend Double_t operator^(const TVector2 &, const TVector2 &)
Definition: TVector2.h:161
Double_t operator^(const TVector2 &v1, const TVector2 &v2)
Definition: TVector2.h:161
virtual ~TVector2()
Definition: TVector2.cxx:51
Double_t DeltaPhi(const TVector2 &v) const
Definition: TVector2.h:165
TVector2 operator+(const TVector2 &v1, const TVector2 &v2)
Definition: TVector2.h:125
TVector2 & operator/=(Double_t s)
Definition: TVector2.h:121
TVector2 Proj(const TVector2 &v) const
Definition: TVector2.h:169
Double_t operator*=(TVector2 const &v)
Definition: TVector2.h:118
friend Double_t operator*(const TVector2 &, const TVector2 &)
Definition: TVector2.h:153
SVector< double, 2 > v
Definition: Dict.h:5
TVector2 Ort() const
Definition: TVector2.h:167
friend TVector2 operator-(const TVector2 &, const TVector2 &)
Definition: TVector2.h:137
TVector2 operator/(const TVector2 &v, Double_t s)
Definition: TVector2.h:157
Double_t X() const
Definition: TVector2.h:72
double Double_t
Definition: RtypesCore.h:55
Double_t Phi() const
return vector phi
Definition: TVector2.cxx:74
Double_t Px() const
Definition: TVector2.h:70
TVector2 & operator-=(TVector2 const &v)
Definition: TVector2.h:114
Double_t Scalar
Definition: TVector2.h:31
TVector2 & operator=(TVector2 const &v)
Definition: TVector2.h:112
TVector2 Rotate(Double_t phi) const
rotation by phi
Definition: TVector2.cxx:108
void SetX(Double_t x0)
Definition: TVector2.h:178
Mother of all ROOT objects.
Definition: TObject.h:58
TVector2 & operator+=(TVector2 const &v)
Definition: TVector2.h:113
Double_t Y() const
Definition: TVector2.h:73
void SetMagPhi(Double_t mag, Double_t phi)
set vector using mag and phi
Definition: TVector2.cxx:116
void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TVector2.cxx:146
Double_t Mod2() const
Definition: TVector2.h:67