// @(#)root/physics:$Id$
// 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.             *
 *************************************************************************/

#include "TROOT.h"
#include "TVector2.h"
#include "TClass.h"
#include "TMath.h"

Double_t const  kPI        = TMath::Pi();
Double_t const  kTWOPI     = 2.*kPI;

ClassImp(TVector2)

//______________________________________________________________________________
TVector2::TVector2()
{
   //constructor
   fX = 0.;
   fY = 0.;
}

//______________________________________________________________________________
TVector2::TVector2(Double_t *v)
{
   //constructor
   fX = v[0];
   fY = v[1];
}

//______________________________________________________________________________
TVector2::TVector2(Double_t x0, Double_t y0)
{
   //constructor
   fX = x0;
   fY = y0;
}

//______________________________________________________________________________
TVector2::~TVector2()
{
}

//______________________________________________________________________________
Double_t TVector2::Mod() const
{
   // return modulo of this vector
   return TMath::Sqrt(fX*fX+fY*fY);
}

//______________________________________________________________________________
TVector2 TVector2::Unit() const
{
   // return module normalized to 1
   return (Mod2()) ? *this/Mod() : TVector2(); 
}

//______________________________________________________________________________
Double_t TVector2::Phi() const
{
   // return vector phi
   return TMath::Pi()+TMath::ATan2(-fY,-fX); 
} 

//______________________________________________________________________________
Double_t TVector2::Phi_0_2pi(Double_t x) {
   // (static function) returns phi angle in the interval [0,2*PI)
   if(TMath::IsNaN(x)){
      gROOT->Error("TVector2::Phi_0_2pi","function called with NaN");
      return x;
   }
   while (x >= kTWOPI) x -= kTWOPI;
   while (x <     0.)  x += kTWOPI;
   return x;
}

//______________________________________________________________________________
Double_t TVector2::Phi_mpi_pi(Double_t x) {
   // (static function) returns phi angle in the interval [-PI,PI)
   if(TMath::IsNaN(x)){
      gROOT->Error("TVector2::Phi_mpi_pi","function called with NaN");
      return x;
   }
   while (x >= kPI) x -= kTWOPI;
   while (x < -kPI) x += kTWOPI;
   return x;
}

//______________________________________________________________________________
TVector2 TVector2::Rotate (Double_t phi) const
{
   //rotation by phi
   return TVector2( fX*TMath::Cos(phi)-fY*TMath::Sin(phi), fX*TMath::Sin(phi)+fY*TMath::Cos(phi) );
}

//______________________________________________________________________________
void TVector2::SetMagPhi(Double_t mag, Double_t phi) 
{
   //set vector using mag and phi
   Double_t amag = TMath::Abs(mag);
   fX = amag * TMath::Cos(phi);
   fY = amag * TMath::Sin(phi);
}
//______________________________________________________________________________
void TVector2::Streamer(TBuffer &R__b)
{
   // Stream an object of class TVector2.

   if (R__b.IsReading()) {
      UInt_t R__s, R__c;
      Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
      if (R__v > 2) {
         R__b.ReadClassBuffer(TVector2::Class(), this, R__v, R__s, R__c);
         return;
      }
      //====process old versions before automatic schema evolution
      if (R__v < 2) TObject::Streamer(R__b);
      R__b >> fX;
      R__b >> fY;
      R__b.CheckByteCount(R__s, R__c, TVector2::IsA());
      //====end of old versions

   } else {
      R__b.WriteClassBuffer(TVector2::Class(),this);
   }
}

void TVector2::Print(Option_t*)const
{
   //print vector parameters
   Printf("%s %s (x,y)=(%f,%f) (rho,phi)=(%f,%f)",GetName(),GetTitle(),X(),Y(),
                                          Mod(),Phi()*TMath::RadToDeg());
}

 TVector2.cxx:1
 TVector2.cxx:2
 TVector2.cxx:3
 TVector2.cxx:4
 TVector2.cxx:5
 TVector2.cxx:6
 TVector2.cxx:7
 TVector2.cxx:8
 TVector2.cxx:9
 TVector2.cxx:10
 TVector2.cxx:11
 TVector2.cxx:12
 TVector2.cxx:13
 TVector2.cxx:14
 TVector2.cxx:15
 TVector2.cxx:16
 TVector2.cxx:17
 TVector2.cxx:18
 TVector2.cxx:19
 TVector2.cxx:20
 TVector2.cxx:21
 TVector2.cxx:22
 TVector2.cxx:23
 TVector2.cxx:24
 TVector2.cxx:25
 TVector2.cxx:26
 TVector2.cxx:27
 TVector2.cxx:28
 TVector2.cxx:29
 TVector2.cxx:30
 TVector2.cxx:31
 TVector2.cxx:32
 TVector2.cxx:33
 TVector2.cxx:34
 TVector2.cxx:35
 TVector2.cxx:36
 TVector2.cxx:37
 TVector2.cxx:38
 TVector2.cxx:39
 TVector2.cxx:40
 TVector2.cxx:41
 TVector2.cxx:42
 TVector2.cxx:43
 TVector2.cxx:44
 TVector2.cxx:45
 TVector2.cxx:46
 TVector2.cxx:47
 TVector2.cxx:48
 TVector2.cxx:49
 TVector2.cxx:50
 TVector2.cxx:51
 TVector2.cxx:52
 TVector2.cxx:53
 TVector2.cxx:54
 TVector2.cxx:55
 TVector2.cxx:56
 TVector2.cxx:57
 TVector2.cxx:58
 TVector2.cxx:59
 TVector2.cxx:60
 TVector2.cxx:61
 TVector2.cxx:62
 TVector2.cxx:63
 TVector2.cxx:64
 TVector2.cxx:65
 TVector2.cxx:66
 TVector2.cxx:67
 TVector2.cxx:68
 TVector2.cxx:69
 TVector2.cxx:70
 TVector2.cxx:71
 TVector2.cxx:72
 TVector2.cxx:73
 TVector2.cxx:74
 TVector2.cxx:75
 TVector2.cxx:76
 TVector2.cxx:77
 TVector2.cxx:78
 TVector2.cxx:79
 TVector2.cxx:80
 TVector2.cxx:81
 TVector2.cxx:82
 TVector2.cxx:83
 TVector2.cxx:84
 TVector2.cxx:85
 TVector2.cxx:86
 TVector2.cxx:87
 TVector2.cxx:88
 TVector2.cxx:89
 TVector2.cxx:90
 TVector2.cxx:91
 TVector2.cxx:92
 TVector2.cxx:93
 TVector2.cxx:94
 TVector2.cxx:95
 TVector2.cxx:96
 TVector2.cxx:97
 TVector2.cxx:98
 TVector2.cxx:99
 TVector2.cxx:100
 TVector2.cxx:101
 TVector2.cxx:102
 TVector2.cxx:103
 TVector2.cxx:104
 TVector2.cxx:105
 TVector2.cxx:106
 TVector2.cxx:107
 TVector2.cxx:108
 TVector2.cxx:109
 TVector2.cxx:110
 TVector2.cxx:111
 TVector2.cxx:112
 TVector2.cxx:113
 TVector2.cxx:114
 TVector2.cxx:115
 TVector2.cxx:116
 TVector2.cxx:117
 TVector2.cxx:118
 TVector2.cxx:119
 TVector2.cxx:120
 TVector2.cxx:121
 TVector2.cxx:122
 TVector2.cxx:123
 TVector2.cxx:124
 TVector2.cxx:125
 TVector2.cxx:126
 TVector2.cxx:127
 TVector2.cxx:128
 TVector2.cxx:129
 TVector2.cxx:130
 TVector2.cxx:131
 TVector2.cxx:132
 TVector2.cxx:133
 TVector2.cxx:134
 TVector2.cxx:135
 TVector2.cxx:136
 TVector2.cxx:137
 TVector2.cxx:138
 TVector2.cxx:139
 TVector2.cxx:140
 TVector2.cxx:141