source: trunk/CLHEP/Vector/TwoVector.icc@ 14

Last change on this file since 14 was 4, checked in by Pavel Demin, 16 years ago

first commit

File size: 3.7 KB
Line 
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
12namespace CLHEP {
13
14inline double Hep2Vector::x() const {
15 return dx;
16}
17
18inline double Hep2Vector::y() const {
19 return dy;
20}
21
22inline Hep2Vector::Hep2Vector(double x, double y)
23: dx(x), dy(y) {}
24
25inline Hep2Vector::Hep2Vector( const Hep3Vector & s)
26: dx(s.x()), dy(s.y()) {}
27
28inline void Hep2Vector::setX(double x) {
29 dx = x;
30}
31
32inline void Hep2Vector::setY(double y) {
33 dy = y;
34}
35
36inline void Hep2Vector::set(double x, double y) {
37 dx = x;
38 dy = y;
39}
40
41double & Hep2Vector::operator[] (int i) { return operator()(i); }
42double Hep2Vector::operator[] (int i) const { return operator()(i); }
43
44inline Hep2Vector::Hep2Vector(const Hep2Vector & p)
45: dx(p.x()), dy(p.y()) {}
46
47inline Hep2Vector::~Hep2Vector() {}
48
49inline Hep2Vector & Hep2Vector::operator = (const Hep2Vector & p) {
50 dx = p.x();
51 dy = p.y();
52 return *this;
53}
54
55inline bool Hep2Vector::operator == (const Hep2Vector& v) const {
56 return (v.x()==x() && v.y()==y()) ? true : false;
57}
58
59inline bool Hep2Vector::operator != (const Hep2Vector& v) const {
60 return (v.x()!=x() || v.y()!=y()) ? true : false;
61}
62
63inline Hep2Vector& Hep2Vector::operator += (const Hep2Vector & p) {
64 dx += p.x();
65 dy += p.y();
66 return *this;
67}
68
69inline Hep2Vector& Hep2Vector::operator -= (const Hep2Vector & p) {
70 dx -= p.x();
71 dy -= p.y();
72 return *this;
73}
74
75inline Hep2Vector Hep2Vector::operator - () const {
76 return Hep2Vector(-dx, -dy);
77}
78
79inline Hep2Vector& Hep2Vector::operator *= (double a) {
80 dx *= a;
81 dy *= a;
82 return *this;
83}
84
85inline double Hep2Vector::dot(const Hep2Vector & p) const {
86 return dx*p.x() + dy*p.y();
87}
88
89inline double Hep2Vector::mag2() const {
90 return dx*dx + dy*dy;
91}
92
93inline double Hep2Vector::mag() const {
94 return std::sqrt(mag2());
95}
96
97inline double Hep2Vector::r() const {
98 return std::sqrt(mag2());
99}
100
101inline 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
107inline Hep2Vector Hep2Vector::orthogonal() const {
108 double x = std::fabs(dx), y = std::fabs(dy);
109 if (x < y) {
110 return Hep2Vector(dy,-dx);
111 }else{
112 return Hep2Vector(-dy,dx);
113 }
114}
115
116inline double Hep2Vector::phi() const {
117 return dx == 0.0 && dy == 0.0 ? 0.0 : std::atan2(dy,dx);
118}
119
120inline 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
125inline void Hep2Vector::setMag(double r){
126 double ph = phi();
127 setX( r * std::cos(ph) );
128 setY( r * std::sin(ph) );
129}
130
131inline void Hep2Vector::setR(double r){
132 setMag(r);
133}
134
135inline void Hep2Vector::setPhi(double phi){
136 double ma = mag();
137 setX( ma * std::cos(phi) );
138 setY( ma * std::sin(phi) );
139}
140
141inline void Hep2Vector::setPolar(double r, double phi){
142 setX( r * std::cos(phi) );
143 setY( r * std::sin(phi) );
144}
145
146inline Hep2Vector operator + (const Hep2Vector & a, const Hep2Vector & b) {
147 return Hep2Vector(a.x() + b.x(), a.y() + b.y());
148}
149
150inline Hep2Vector operator - (const Hep2Vector & a, const Hep2Vector & b) {
151 return Hep2Vector(a.x() - b.x(), a.y() - b.y());
152}
153
154inline Hep2Vector operator * (const Hep2Vector & p, double a) {
155 return Hep2Vector(a*p.x(), a*p.y());
156}
157
158inline Hep2Vector operator * (double a, const Hep2Vector & p) {
159 return Hep2Vector(a*p.x(), a*p.y());
160}
161
162inline double operator * (const Hep2Vector & a, const Hep2Vector & b) {
163 return a.dot(b);
164}
165
166inline double Hep2Vector::getTolerance () {
167 return tolerance;
168}
169
170} // namespace CLHEP
171
Note: See TracBrowser for help on using the repository browser.