src/work/athos/xy/xy.h
author athos
Tue, 23 Mar 2004 17:28:47 +0000
changeset 240 4a1d2e642552
parent 237 7fb8b67d2c5e
child 242 b255f25ad394
permissions -rw-r--r--
Elk?sz?lt a boundingbox oszt?ly (boundingbox.h) ?s hozz? a tesztprogi.
     1 // -*- c++ -*-
     2 /**
     3 2 dimensional vector (plainvector) implementation
     4 
     5 */
     6 #ifndef HUGO_XY_H
     7 #define HUGO_XY_H
     8 
     9 #include <iostream>
    10 
    11 namespace hugo {
    12 
    13   template<typename T>
    14     class xy {
    15 
    16     public:
    17 
    18       T x,y;     
    19       
    20       ///Default constructor: both coordinates become 0
    21       xy() : x(0), y(0) {}
    22 
    23       ///Constructing the instance from coordinates
    24       xy(T a, T b) : x(a), y(a) { }
    25 
    26 
    27       ///Gives back the square of the norm of the vector
    28       T normSquare(){
    29 	return x*x+y*y;
    30       };
    31   
    32       ///Increments the left hand side by u
    33       xy<T>& operator +=(const xy<T>& u){
    34 	x += u.x;
    35 	y += u.y;
    36 	return *this;
    37       };
    38   
    39       ///Decrements the left hand side by u
    40       xy<T>& operator -=(const xy<T>& u){
    41 	x -= u.x;
    42 	y -= u.y;
    43 	return *this;
    44       };
    45 
    46       ///Multiplying the left hand side with a scalar
    47       xy<T>& operator *=(const T &u){
    48 	x *= u;
    49 	y *= u;
    50 	return *this;
    51       };
    52 
    53       ///Dividing the left hand side by a scalar
    54       xy<T>& operator /=(const T &u){
    55 	x /= u;
    56 	y /= u;
    57 	return *this;
    58       };
    59   
    60       ///Returns the scalar product of two vectors
    61       T operator *(const xy<T>& u){
    62 	return x*u.x+y*u.y;
    63       };
    64   
    65       ///Returns the sum of two vectors
    66       xy<T> operator+(const xy<T> &u) const {
    67 	xy<T> b=*this;
    68 	return b+=u;
    69       };
    70 
    71       ///Returns the difference of two vectors
    72       xy<T> operator-(const xy<T> &u) const {
    73 	xy<T> b=*this;
    74 	return b-=u;
    75       };
    76 
    77       ///Returns a vector multiplied by a scalar
    78       xy<T> operator*(const T &u) const {
    79 	xy<T> b=*this;
    80 	return b*=u;
    81       };
    82 
    83       ///Returns a vector divided by a scalar
    84       xy<T> operator/(const T &u) const {
    85 	xy<T> b=*this;
    86 	return b/=u;
    87       };
    88 
    89       ///Testing equality
    90       bool operator==(const xy<T> &u){
    91 	return (x==u.x) && (y==u.y);
    92       };
    93 
    94       ///Testing inequality
    95       bool operator!=(xy u){
    96 	return  (x!=u.x) || (y!=u.y);
    97       };
    98 
    99     };
   100 
   101   ///Reading a plainvector from a stream
   102   template<typename T>
   103   inline
   104   std::istream& operator>>(std::istream &is, xy<T> &z)
   105   {
   106 
   107     is >> z.x >> z.y;
   108     return is;
   109   }
   110 
   111   ///Outputting a plainvector to a stream
   112   template<typename T>
   113   inline
   114   std::ostream& operator<<(std::ostream &os, xy<T> z)
   115   {
   116     os << "(" << z.x << ", " << z.y << ")";
   117     return os;
   118   }
   119 
   120 } //namespace hugo
   121 
   122 #endif //HUGO_XY_H