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