52 |
52 |
53 typedef T Value; |
53 typedef T Value; |
54 |
54 |
55 T x,y; |
55 T x,y; |
56 |
56 |
57 ///Default constructor: both coordinates become 0 |
57 ///Default constructor |
58 xy() : x(0), y(0) {} |
58 xy() {} |
59 |
59 |
60 ///Constructing the instance from coordinates |
60 ///Constructing the instance from coordinates |
61 xy(T a, T b) : x(a), y(b) { } |
61 xy(T a, T b) : x(a), y(b) { } |
62 |
62 |
63 |
63 |
64 ///Conversion constructor |
64 ///Conversion constructor |
65 template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {} |
65 template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {} |
66 |
66 |
67 ///Gives back the square of the norm of the vector |
67 ///Gives back the square of the norm of the vector |
68 T normSquare(){ |
68 T normSquare() const { |
69 return x*x+y*y; |
69 return x*x+y*y; |
70 }; |
70 }; |
71 |
71 |
72 ///Increments the left hand side by u |
72 ///Increments the left hand side by u |
73 xy<T>& operator +=(const xy<T>& u){ |
73 xy<T>& operator +=(const xy<T>& u) { |
74 x += u.x; |
74 x += u.x; |
75 y += u.y; |
75 y += u.y; |
76 return *this; |
76 return *this; |
77 }; |
77 }; |
78 |
78 |
79 ///Decrements the left hand side by u |
79 ///Decrements the left hand side by u |
80 xy<T>& operator -=(const xy<T>& u){ |
80 xy<T>& operator -=(const xy<T>& u) { |
81 x -= u.x; |
81 x -= u.x; |
82 y -= u.y; |
82 y -= u.y; |
83 return *this; |
83 return *this; |
84 }; |
84 }; |
85 |
85 |
86 ///Multiplying the left hand side with a scalar |
86 ///Multiplying the left hand side with a scalar |
87 xy<T>& operator *=(const T &u){ |
87 xy<T>& operator *=(const T &u) { |
88 x *= u; |
88 x *= u; |
89 y *= u; |
89 y *= u; |
90 return *this; |
90 return *this; |
91 }; |
91 }; |
92 |
92 |
93 ///Dividing the left hand side by a scalar |
93 ///Dividing the left hand side by a scalar |
94 xy<T>& operator /=(const T &u){ |
94 xy<T>& operator /=(const T &u) { |
95 x /= u; |
95 x /= u; |
96 y /= u; |
96 y /= u; |
97 return *this; |
97 return *this; |
98 }; |
98 }; |
99 |
99 |
100 ///Returns the scalar product of two vectors |
100 ///Returns the scalar product of two vectors |
101 T operator *(const xy<T>& u){ |
101 T operator *(const xy<T>& u) const { |
102 return x*u.x+y*u.y; |
102 return x*u.x+y*u.y; |
103 }; |
103 }; |
104 |
104 |
105 ///Returns the sum of two vectors |
105 ///Returns the sum of two vectors |
106 xy<T> operator+(const xy<T> &u) const { |
106 xy<T> operator+(const xy<T> &u) const { |