ROOT logo
// @(#)root/physics:$Id: TVector2.h 20882 2007-11-19 11:31:26Z rdm $
// Author: Pasha Murat   12/02/99

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TVector2
#define ROOT_TVector2

#include "TObject.h"


class TVector2 : public TObject {
//------------------------------------------------------------------------------
//  data members
//------------------------------------------------------------------------------
protected:

   Double_t    fX;    // components of the vector
   Double_t    fY;
//------------------------------------------------------------------------------
//  function members
//------------------------------------------------------------------------------
public:

   TVector2 ();
   TVector2 (Double_t *s);
   TVector2 (Double_t x0, Double_t y0);
   virtual ~TVector2();
                                        // ****** unary operators

   TVector2&       operator  = (TVector2 const & v);
   TVector2&       operator += (TVector2 const & v);
   TVector2&       operator -= (TVector2 const & v);
   Double_t        operator *= (TVector2 const & v);
   TVector2&       operator *= (Double_t s);
   TVector2&       operator /= (Double_t s);

                                        // ****** binary operators

   friend TVector2       operator + (const TVector2&, const TVector2&);
   friend TVector2       operator + (const TVector2&, Double_t  );
   friend TVector2       operator + (Double_t  , const TVector2&);
   friend TVector2       operator - (const TVector2&, const TVector2&);
   friend TVector2       operator - (const TVector2&, Double_t  );
   friend Double_t       operator * (const TVector2&, const TVector2&);
   friend TVector2       operator * (const TVector2&, Double_t  );
   friend TVector2       operator * (Double_t  , const TVector2&);
   friend TVector2       operator / (const TVector2&, Double_t  );
   friend Double_t       operator ^ (const TVector2&, const TVector2&);

                                        // ****** setters
   void Set(const TVector2& v);
   void Set(Double_t x0, Double_t y0);
   void Set(float  x0, float  y0);

                                        // ****** other member functions

   Double_t Mod2() const { return fX*fX+fY*fY; };
   Double_t Mod () const;

   Double_t Px()   const { return fX; };
   Double_t Py()   const { return fY; };
   Double_t X ()   const { return fX; };
   Double_t Y ()   const { return fY; };

                                        // phi() is defined in [0,TWOPI]

   Double_t Phi           () const;
   Double_t DeltaPhi(const TVector2& v) const;
   void     SetMagPhi(Double_t mag, Double_t phi);

                                        // unit vector in the direction of *this

   TVector2 Unit() const;
   TVector2 Ort () const;

                                        // projection of *this to the direction
                                        // of TVector2 vector `v'

   TVector2 Proj(const TVector2& v) const;

                                        // component of *this normal to `v'

   TVector2 Norm(const TVector2& v) const;

                                        // rotates 2-vector by phi radians
   TVector2 Rotate (Double_t phi) const;

                                        // returns phi angle in the interval [0,2*PI)
   static Double_t Phi_0_2pi(Double_t x);                                                                               // returns phi angle in the interval
                                        // returns phi angle in the interval [-PI,PI)
   static Double_t Phi_mpi_pi(Double_t x);


   void Print(Option_t* option="") const;

   ClassDef(TVector2,3)  // A 2D physics vector

};

                                        // ****** unary operators

inline TVector2& TVector2::operator  = (TVector2 const& v) {fX  = v.fX; fY  = v.fY; return *this;}
inline TVector2& TVector2::operator += (TVector2 const& v) {fX += v.fX; fY += v.fY; return *this;}
inline TVector2& TVector2::operator -= (TVector2 const& v) {fX -= v.fX; fY -= v.fY; return *this;}

                                        // scalar product of 2 2-vectors

inline Double_t   TVector2::operator *= (const TVector2& v) { return(fX*v.fX+fY*v.fY); }

inline TVector2& TVector2::operator *= (Double_t s) { fX *=s; fY *=s; return *this; }
inline TVector2& TVector2::operator /= (Double_t s) { fX /=s; fY /=s; return *this; }

                                        // ****** binary operators

inline TVector2  operator + (const TVector2& v1, const TVector2& v2) {
   return TVector2(v1.fX+v2.fX,v1.fY+v2.fY);
}

inline TVector2  operator + (const TVector2& v1, Double_t bias) {
   return TVector2 (v1.fX+bias,v1.fY+bias);
}

inline TVector2  operator + (Double_t bias, const TVector2& v1) {
   return TVector2 (v1.fX+bias,v1.fY+bias);
}

inline TVector2  operator - (const TVector2& v1, const TVector2& v2) {
   return TVector2(v1.fX-v2.fX,v1.fY-v2.fY);
}

inline TVector2  operator - (const TVector2& v1, Double_t bias) {
   return TVector2 (v1.fX-bias,v1.fY-bias);
}

inline TVector2  operator * (const TVector2& v, Double_t s) {
   return TVector2 (v.fX*s,v.fY*s);
}

inline TVector2    operator * (Double_t s, const TVector2& v) {
   return TVector2 (v.fX*s,v.fY*s);
}

inline Double_t operator * (const TVector2& v1, const TVector2& v2) {
   return  v1.fX*v2.fX+v1.fY*v2.fY;
}

inline TVector2     operator / (const TVector2& v, Double_t s) {
   return TVector2 (v.fX/s,v.fY/s);
}

inline Double_t   operator ^ (const TVector2& v1, const TVector2& v2) {
   return  v1.fX*v2.fY-v1.fY*v2.fX;
}

inline  Double_t TVector2::DeltaPhi(const TVector2& v) const { return Phi_mpi_pi(Phi()-v.Phi()); }

inline  TVector2 TVector2::Ort () const { return Unit(); }

inline  TVector2 TVector2::Proj(const TVector2& v) const { return v*(((*this)*v)/v.Mod2()); }

inline  TVector2 TVector2::Norm(const TVector2& v) const {return *this-Proj(v); }

                                     // ****** setters

inline void TVector2::Set(const TVector2& v   )     { fX = v.fX; fY = v.fY; }
inline void TVector2::Set(Double_t x0, Double_t y0) { fX = x0  ; fY = y0 ;  }
inline void TVector2::Set(float  x0, float  y0)     { fX = x0  ; fY = y0 ;  }

#endif
 TVector2.h:1
 TVector2.h:2
 TVector2.h:3
 TVector2.h:4
 TVector2.h:5
 TVector2.h:6
 TVector2.h:7
 TVector2.h:8
 TVector2.h:9
 TVector2.h:10
 TVector2.h:11
 TVector2.h:12
 TVector2.h:13
 TVector2.h:14
 TVector2.h:15
 TVector2.h:16
 TVector2.h:17
 TVector2.h:18
 TVector2.h:19
 TVector2.h:20
 TVector2.h:21
 TVector2.h:22
 TVector2.h:23
 TVector2.h:24
 TVector2.h:25
 TVector2.h:26
 TVector2.h:27
 TVector2.h:28
 TVector2.h:29
 TVector2.h:30
 TVector2.h:31
 TVector2.h:32
 TVector2.h:33
 TVector2.h:34
 TVector2.h:35
 TVector2.h:36
 TVector2.h:37
 TVector2.h:38
 TVector2.h:39
 TVector2.h:40
 TVector2.h:41
 TVector2.h:42
 TVector2.h:43
 TVector2.h:44
 TVector2.h:45
 TVector2.h:46
 TVector2.h:47
 TVector2.h:48
 TVector2.h:49
 TVector2.h:50
 TVector2.h:51
 TVector2.h:52
 TVector2.h:53
 TVector2.h:54
 TVector2.h:55
 TVector2.h:56
 TVector2.h:57
 TVector2.h:58
 TVector2.h:59
 TVector2.h:60
 TVector2.h:61
 TVector2.h:62
 TVector2.h:63
 TVector2.h:64
 TVector2.h:65
 TVector2.h:66
 TVector2.h:67
 TVector2.h:68
 TVector2.h:69
 TVector2.h:70
 TVector2.h:71
 TVector2.h:72
 TVector2.h:73
 TVector2.h:74
 TVector2.h:75
 TVector2.h:76
 TVector2.h:77
 TVector2.h:78
 TVector2.h:79
 TVector2.h:80
 TVector2.h:81
 TVector2.h:82
 TVector2.h:83
 TVector2.h:84
 TVector2.h:85
 TVector2.h:86
 TVector2.h:87
 TVector2.h:88
 TVector2.h:89
 TVector2.h:90
 TVector2.h:91
 TVector2.h:92
 TVector2.h:93
 TVector2.h:94
 TVector2.h:95
 TVector2.h:96
 TVector2.h:97
 TVector2.h:98
 TVector2.h:99
 TVector2.h:100
 TVector2.h:101
 TVector2.h:102
 TVector2.h:103
 TVector2.h:104
 TVector2.h:105
 TVector2.h:106
 TVector2.h:107
 TVector2.h:108
 TVector2.h:109
 TVector2.h:110
 TVector2.h:111
 TVector2.h:112
 TVector2.h:113
 TVector2.h:114
 TVector2.h:115
 TVector2.h:116
 TVector2.h:117
 TVector2.h:118
 TVector2.h:119
 TVector2.h:120
 TVector2.h:121
 TVector2.h:122
 TVector2.h:123
 TVector2.h:124
 TVector2.h:125
 TVector2.h:126
 TVector2.h:127
 TVector2.h:128
 TVector2.h:129
 TVector2.h:130
 TVector2.h:131
 TVector2.h:132
 TVector2.h:133
 TVector2.h:134
 TVector2.h:135
 TVector2.h:136
 TVector2.h:137
 TVector2.h:138
 TVector2.h:139
 TVector2.h:140
 TVector2.h:141
 TVector2.h:142
 TVector2.h:143
 TVector2.h:144
 TVector2.h:145
 TVector2.h:146
 TVector2.h:147
 TVector2.h:148
 TVector2.h:149
 TVector2.h:150
 TVector2.h:151
 TVector2.h:152
 TVector2.h:153
 TVector2.h:154
 TVector2.h:155
 TVector2.h:156
 TVector2.h:157
 TVector2.h:158
 TVector2.h:159
 TVector2.h:160
 TVector2.h:161
 TVector2.h:162
 TVector2.h:163
 TVector2.h:164
 TVector2.h:165
 TVector2.h:166
 TVector2.h:167
 TVector2.h:168
 TVector2.h:169
 TVector2.h:170
 TVector2.h:171
 TVector2.h:172
 TVector2.h:173
 TVector2.h:174
 TVector2.h:175
 TVector2.h:176