10 |
10 |
11 namespace hugo { |
11 namespace hugo { |
12 |
12 |
13 template<typename T> |
13 template<typename T> |
14 class xy { |
14 class xy { |
15 T _x,_y; |
|
16 |
15 |
17 public: |
16 public: |
|
17 |
|
18 T x,y; |
18 |
19 |
19 ///Default constructor: both coordinates become 0 |
20 ///Default constructor: both coordinates become 0 |
20 xy() : _x(0), _y(0 ){ /*_x=_y=0;*/ } |
21 xy() : x(0), y(0) {} |
21 |
22 |
22 ///Constructing from coordinates |
23 ///Constructing the instance from coordinates |
23 xy(T a, T b) : _x(a), _x(b) { /*_x=a; _y=b;*/ } |
24 xy(T a, T b) : x(a), y(a) { } |
24 |
25 |
25 ///Gives back the x coordinate |
|
26 T x(){ |
|
27 return _x; |
|
28 }; |
|
29 |
|
30 ///Gives back the y coordinate |
|
31 T y(){ |
|
32 return _y; |
|
33 }; |
|
34 |
26 |
35 ///Gives back the square of the norm of the vector |
27 ///Gives back the square of the norm of the vector |
36 T normSquare(){ |
28 T normSquare(){ |
37 return _x*_x+_y*_y; |
29 return x*x+y*y; |
38 }; |
30 }; |
39 |
31 |
40 ///Increments the left hand side by u |
32 ///Increments the left hand side by u |
41 xy<T>& operator +=(const xy<T>& u){ |
33 xy<T>& operator +=(const xy<T>& u){ |
42 _x += u._x; |
34 x += u.x; |
43 _y += u._y; |
35 y += u.y; |
44 return *this; |
36 return *this; |
45 }; |
37 }; |
46 |
38 |
47 ///Decrements the left hand side by u |
39 ///Decrements the left hand side by u |
48 xy<T>& operator -=(const xy<T>& u){ |
40 xy<T>& operator -=(const xy<T>& u){ |
49 _x -= u._x; |
41 x -= u.x; |
50 _y -= u._y; |
42 y -= u.y; |
51 return *this; |
43 return *this; |
52 }; |
44 }; |
53 |
45 |
54 ///Multiplying the left hand side with a scalar |
46 ///Multiplying the left hand side with a scalar |
55 xy<T>& operator *=(const T &u){ |
47 xy<T>& operator *=(const T &u){ |
56 _x *= u; |
48 x *= u; |
57 _y *= u; |
49 y *= u; |
58 return *this; |
50 return *this; |
59 }; |
51 }; |
60 |
52 |
61 ///Dividing the left hand side by a scalar |
53 ///Dividing the left hand side by a scalar |
62 xy<T>& operator /=(const T &u){ |
54 xy<T>& operator /=(const T &u){ |
63 _x /= u; |
55 x /= u; |
64 _y /= u; |
56 y /= u; |
65 return *this; |
57 return *this; |
66 }; |
58 }; |
67 |
59 |
68 ///Returns the scalar product of two vectors |
60 ///Returns the scalar product of two vectors |
69 T operator *(const xy<T>& u){ |
61 T operator *(const xy<T>& u){ |
70 return _x*u._x+_y*u._y; |
62 return x*u.x+y*u.y; |
71 }; |
63 }; |
72 |
64 |
73 ///Returns the sum of two vectors |
65 ///Returns the sum of two vectors |
74 xy<T> operator+(const xy<T> &u) const { |
66 xy<T> operator+(const xy<T> &u) const { |
75 xy<T> b=*this; |
67 xy<T> b=*this; |
94 return b/=u; |
86 return b/=u; |
95 }; |
87 }; |
96 |
88 |
97 ///Testing equality |
89 ///Testing equality |
98 bool operator==(const xy<T> &u){ |
90 bool operator==(const xy<T> &u){ |
99 return (_x==u._x) && (_y==u._y); |
91 return (x==u.x) && (y==u.y); |
100 }; |
92 }; |
101 |
93 |
102 ///Testing inequality |
94 ///Testing inequality |
103 bool operator!=(xy u){ |
95 bool operator!=(xy u){ |
104 return (_x!=u._x) || (_y!=u._y); |
96 return (x!=u.x) || (y!=u.y); |
105 }; |
97 }; |
106 |
98 |
107 }; |
99 }; |
108 |
100 |
109 ///Reading a plainvector from a stream |
101 ///Reading a plainvector from a stream |
110 template<typename T> |
102 template<typename T> |
111 inline |
103 inline |
112 std::istream& operator>>(std::istream &is, xy<T> &z) |
104 std::istream& operator>>(std::istream &is, xy<T> &z) |
113 { |
105 { |
114 ///This is not the best solution here: I didn't know how to solve this with friend functions |
106 |
115 T a,b; |
107 is >> z.x >> z.y; |
116 is >> a >> b; |
|
117 xy<T> buf(a,b); |
|
118 z=buf; |
|
119 return is; |
108 return is; |
120 } |
109 } |
121 |
110 |
122 ///Outputting a plainvector to a stream |
111 ///Outputting a plainvector to a stream |
123 template<typename T> |
112 template<typename T> |
124 inline |
113 inline |
125 std::ostream& operator<<(std::ostream &os, xy<T> z) |
114 std::ostream& operator<<(std::ostream &os, xy<T> z) |
126 { |
115 { |
127 os << "(" << z.x() << ", " << z.y() << ")"; |
116 os << "(" << z.x << ", " << z.y << ")"; |
128 return os; |
117 return os; |
129 } |
118 } |
130 |
119 |
131 } //namespace hugo |
120 } //namespace hugo |
132 |
121 |