COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/xy/xy.h @ 237:7fb8b67d2c5e

Last change on this file since 237:7fb8b67d2c5e was 237:7fb8b67d2c5e, checked in by marci, 21 years ago

.

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