Geant4  10.03
TwoVector.icc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // ---------------------------------------------------------------------------
3 //
4 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
5 //
6 // This is the definitions of the inline member functions of the
7 // Hep2Vector class.
8 //
9 
10 #include <cmath>
11 
12 namespace CLHEP {
13 
14 inline double Hep2Vector::x() const {
15  return dx;
16 }
17 
18 inline double Hep2Vector::y() const {
19  return dy;
20 }
21 
22 inline Hep2Vector::Hep2Vector(double x1, double y1)
23 : dx(x1), dy(y1) {}
24 
25 inline Hep2Vector::Hep2Vector( const Hep3Vector & v3)
26 : dx(v3.x()), dy(v3.y()) {}
27 
28 inline void Hep2Vector::setX(double x1) {
29  dx = x1;
30 }
31 
32 inline void Hep2Vector::setY(double y1) {
33  dy = y1;
34 }
35 
36 inline void Hep2Vector::set(double x1, double y1) {
37  dx = x1;
38  dy = y1;
39 }
40 
41 double & Hep2Vector::operator[] (int i) { return operator()(i); }
42 double Hep2Vector::operator[] (int i) const { return operator()(i); }
43 
44 inline Hep2Vector::Hep2Vector(const Hep2Vector & p)
45 : dx(p.x()), dy(p.y()) {}
46 
47 inline Hep2Vector::~Hep2Vector() {}
48 
49 inline Hep2Vector & Hep2Vector::operator = (const Hep2Vector & p) {
50  dx = p.x();
51  dy = p.y();
52  return *this;
53 }
54 
55 inline bool Hep2Vector::operator == (const Hep2Vector& v) const {
56  return (v.x()==x() && v.y()==y()) ? true : false;
57 }
58 
59 inline bool Hep2Vector::operator != (const Hep2Vector& v) const {
60  return (v.x()!=x() || v.y()!=y()) ? true : false;
61 }
62 
63 inline Hep2Vector& Hep2Vector::operator += (const Hep2Vector & p) {
64  dx += p.x();
65  dy += p.y();
66  return *this;
67 }
68 
69 inline Hep2Vector& Hep2Vector::operator -= (const Hep2Vector & p) {
70  dx -= p.x();
71  dy -= p.y();
72  return *this;
73 }
74 
75 inline Hep2Vector Hep2Vector::operator - () const {
76  return Hep2Vector(-dx, -dy);
77 }
78 
79 inline Hep2Vector& Hep2Vector::operator *= (double a) {
80  dx *= a;
81  dy *= a;
82  return *this;
83 }
84 
85 inline double Hep2Vector::dot(const Hep2Vector & p) const {
86  return dx*p.x() + dy*p.y();
87 }
88 
89 inline double Hep2Vector::mag2() const {
90  return dx*dx + dy*dy;
91 }
92 
93 inline double Hep2Vector::mag() const {
94  return std::sqrt(mag2());
95 }
96 
97 inline double Hep2Vector::r() const {
98  return std::sqrt(mag2());
99 }
100 
101 inline Hep2Vector Hep2Vector::unit() const {
102  double tot = mag2();
103  Hep2Vector p(*this);
104  return tot > 0.0 ? p *= (1.0/std::sqrt(tot)) : Hep2Vector(1,0);
105 }
106 
107 inline Hep2Vector Hep2Vector::orthogonal() const {
108  double x1 = std::fabs(dx), y1 = std::fabs(dy);
109  if (x1 < y1) {
110  return Hep2Vector(dy,-dx);
111  }else{
112  return Hep2Vector(-dy,dx);
113  }
114 }
115 
116 inline double Hep2Vector::phi() const {
117  return dx == 0.0 && dy == 0.0 ? 0.0 : std::atan2(dy,dx);
118 }
119 
120 inline double Hep2Vector::angle(const Hep2Vector & q) const {
121  double ptot2 = mag2()*q.mag2();
122  return ptot2 <= 0.0 ? 0.0 : std::acos(dot(q)/std::sqrt(ptot2));
123 }
124 
125 inline void Hep2Vector::setMag(double r1){
126  double ph = phi();
127  setX( r1 * std::cos(ph) );
128  setY( r1 * std::sin(ph) );
129 }
130 
131 inline void Hep2Vector::setR(double r1){
132  setMag(r1);
133 }
134 
135 inline void Hep2Vector::setPhi(double phi1){
136  double ma = mag();
137  setX( ma * std::cos(phi1) );
138  setY( ma * std::sin(phi1) );
139 }
140 
141 inline void Hep2Vector::setPolar(double r1, double phi1){
142  setX( r1 * std::cos(phi1) );
143  setY( r1 * std::sin(phi1) );
144 }
145 
146 inline Hep2Vector operator + (const Hep2Vector & a, const Hep2Vector & b) {
147  return Hep2Vector(a.x() + b.x(), a.y() + b.y());
148 }
149 
150 inline Hep2Vector operator - (const Hep2Vector & a, const Hep2Vector & b) {
151  return Hep2Vector(a.x() - b.x(), a.y() - b.y());
152 }
153 
154 inline Hep2Vector operator * (const Hep2Vector & p, double a) {
155  return Hep2Vector(a*p.x(), a*p.y());
156 }
157 
158 inline Hep2Vector operator * (double a, const Hep2Vector & p) {
159  return Hep2Vector(a*p.x(), a*p.y());
160 }
161 
162 inline double operator * (const Hep2Vector & a, const Hep2Vector & b) {
163  return a.dot(b);
164 }
165 
166 inline double Hep2Vector::getTolerance () {
167  return tolerance;
168 }
169 
170 } // namespace CLHEP
171