# source:lemon-0.x/src/work/athos/xy/xy.h@202:0bd4fe53b1d0

Last change on this file since 202:0bd4fe53b1d0 was 201:b9158a014fe8, checked in by athos, 18 years ago

Alpár javaslatára készítettem egy struktúrát 2 dimenziós vektorok (síkvektorok) tárolására. Olyasmiket tud, amiket síkvektoroktól szeretnénk. A neve: xy, a hasonló nevu .h file-ban található.

File size: 2.1 KB
RevLine
[201]1/**
22 dimensional vector (plainvector) implementation
3
4*/
5#ifndef HUGO_XY_H
6#define HUGO_XY_H
7
8#include <iostream>
9
10using namespace std;
11template<typename T>
12class xy {
13  T _x,_y;
14
15public:
16
17  ///Default constructor: both coordinates become 0
18  xy() { _x=_y=0; }
19
20  ///Constructing from coordinates
21  xy(T a, T b) { _x=a; _y=b; }
22
23  ///Gives back the x coordinate
24  T x(){
25    return _x;
26  };
27
28  ///Gives back the y coordinate
29  T y(){
30    return _y;
31  };
32
33  ///Gives back the square of the norm of the vector
34  T normSquare(){
35    return _x*_x+_y*_y;
36  };
37
38  ///Increments the left hand side by u
39  xy<T>& operator +=(const xy<T>& u){
40    _x += u._x;
41    _y += u._y;
42    return *this;
43  };
44
45  ///Decrements the left hand side by u
46  xy<T>& operator -=(const xy<T>& u){
47    _x -= u._x;
48    _y -= u._y;
49    return *this;
50  };
51
52  ///Multiplying the left hand side with a scalar
53  xy<T>& operator *=(const T &u){
54    _x *= u;
55    _y *= u;
56    return *this;
57  };
58
59  ///Returns the scalar product of two vectors
60  T operator *(const xy<T>& u){
61    return _x*u._x+_y*u._y;
62  };
63
64  ///Returns the sum of two vectors
65  xy<T> operator+(const xy<T> &u) const {
66    xy<T> b=*this;
67    return b+=u;
68  };
69
70  ///Returns the difference of two vectors
71  xy<T> operator-(const xy<T> &u) const {
72    xy<T> b=*this;
73    return b-=u;
74  };
75
76  ///Returns a vector multiplied by a scalar
77  xy<T> operator*(const T &u) const {
78    xy<T> b=*this;
79    return b*=u;
80  };
81
82  ///Testing equality
83  bool operator==(const xy<T> &u){
84    return (_x==u._x) && (_y==u._y);
85  };
86
87  ///Testing inequality
88  bool operator!=(xy u){
89    return  (_x!=u._x) || (_y!=u._y);
90  };
91
92};
93///Reading a plainvector from a stream
94template<typename T>
95inline
96istream& operator>>(istream &is, xy<T> &z)
97{
98  ///This is not the best solution here: I didn't know how to solve this with friend functions
99  T a,b;
100  is >> a >> b;
101  xy<T> buf(a,b);
102  z=buf;
103  return is;
104}
105
106///Outputting a plainvector to a stream
107template<typename T>
108inline
109ostream& operator<<(ostream &os, xy<T> z)
110{
111        os << "(" << z.x() << ", " << z.y() << ")";
112        return os;
113}
114
115
116
117#endif //HUGO_XY_H
Note: See TracBrowser for help on using the repository browser.