00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #ifndef __TWOVECTOR_H
00013 #define __TWOVECTOR_H
00014
00015 #include <iostream>
00016 #include <cmath>
00017
00018 class TwoVector {
00019 public:
00020 inline TwoVector();
00021 inline TwoVector(double x_, double y_);
00022
00023
00024 inline double getX() const;
00025 inline double getY() const;
00026
00027 inline double getMag2() const;
00028 inline double getMag() const;
00029
00030 inline double getPhi() const;
00031
00032 inline double getComponent (int i) const;
00033
00034 inline TwoVector& setValues(double x_, double y_);
00035
00036 inline TwoVector& TwoVector::operator+= (const TwoVector& rhs);
00037 inline TwoVector& TwoVector::operator-= (const TwoVector& rhs);
00038 inline TwoVector& TwoVector::operator*= (double rhs);
00039
00040 private:
00041 double x, y;
00042 };
00043
00044 TwoVector::TwoVector()
00045 : x(0), y(0)
00046 {}
00047
00048 TwoVector::TwoVector(double x_, double y_)
00049 : x(x_), y(y_)
00050 {}
00051
00052 double TwoVector::getX() const { return x; }
00053 double TwoVector::getY() const { return y; }
00054
00055 double TwoVector::getMag2() const { return x*x + y*y; }
00056 double TwoVector::getMag()const { return std::sqrt(getMag2()); }
00057
00058 double TwoVector::getPhi() const { return std::atan2(y, x); }
00059
00060 double TwoVector::getComponent(int i) const {
00061 switch (i) {
00062 case 0: return getX();
00063 case 1: return getY();
00064 }
00065 return NAN;
00066 }
00067
00068 TwoVector& TwoVector::setValues(double x_, double y_) {
00069 x = x_;
00070 y = y_;
00071 return *this;
00072 }
00073
00074
00075 TwoVector& TwoVector::operator+= (const TwoVector& rhs) {
00076 x += rhs.x;
00077 y += rhs.y;
00078 return *this;
00079 }
00080
00081 TwoVector& TwoVector::operator-= (const TwoVector& rhs) {
00082 x -= rhs.x;
00083 y -= rhs.y;
00084 return *this;
00085 }
00086
00087 TwoVector& TwoVector::operator*= (double rhs) {
00088 x *= rhs;
00089 y *= rhs;
00090 return *this;
00091 }
00092
00093 inline TwoVector operator+ (const TwoVector& lhs, const TwoVector& rhs) {
00094 return TwoVector (lhs.getX()+rhs.getX(), lhs.getY()+rhs.getY());
00095 }
00096
00097 inline TwoVector operator- (const TwoVector& lhs, const TwoVector& rhs) {
00098 return TwoVector (lhs.getX()-rhs.getX(), lhs.getY()-rhs.getY());
00099 }
00100
00101 inline TwoVector operator- (const TwoVector& rhs) {
00102 return TwoVector (-rhs.getX(), -rhs.getY());
00103 }
00104
00105 inline double operator* (const TwoVector& lhs, const TwoVector& rhs) {
00106 return lhs.getX()*rhs.getX() + lhs.getY()*rhs.getY();
00107 }
00108
00109 inline TwoVector operator* (double lhs, const TwoVector& rhs) {
00110 return TwoVector (lhs*rhs.getX(), lhs*rhs.getY());
00111 }
00112
00113 inline std::ostream& operator<< (std::ostream& out, const TwoVector& v) {
00114 out << "(" << v.getX() << ", " << v.getY() << ")";
00115 return out;
00116 }
00117
00118
00119
00120 #endif
00121
00122