src/work/athos/xy/xy.h
author athos
Thu, 25 Mar 2004 17:38:45 +0000
changeset 244 0e02be2ca43c
parent 242 b255f25ad394
child 249 0b0bdf24d00c
permissions -rw-r--r--
Betettem 1 file-ba a boundingbox-ot ?s az xy-t + egy?b apr? m?dos?t?sok.
athos@207
     1
// -*- c++ -*-
athos@201
     2
#ifndef HUGO_XY_H
athos@201
     3
#define HUGO_XY_H
athos@201
     4
athos@201
     5
#include <iostream>
athos@201
     6
athos@207
     7
namespace hugo {
athos@201
     8
alpar@242
     9
/** \brief
alpar@242
    10
2 dimensional vector (plainvector) implementation
alpar@242
    11
alpar@242
    12
*/
athos@207
    13
  template<typename T>
athos@207
    14
    class xy {
athos@201
    15
athos@207
    16
    public:
athos@240
    17
athos@240
    18
      T x,y;     
athos@207
    19
      
athos@207
    20
      ///Default constructor: both coordinates become 0
athos@240
    21
      xy() : x(0), y(0) {}
athos@201
    22
athos@240
    23
      ///Constructing the instance from coordinates
athos@240
    24
      xy(T a, T b) : x(a), y(a) { }
athos@201
    25
athos@201
    26
athos@207
    27
      ///Gives back the square of the norm of the vector
athos@207
    28
      T normSquare(){
athos@240
    29
	return x*x+y*y;
athos@207
    30
      };
athos@201
    31
  
athos@207
    32
      ///Increments the left hand side by u
athos@207
    33
      xy<T>& operator +=(const xy<T>& u){
athos@240
    34
	x += u.x;
athos@240
    35
	y += u.y;
athos@207
    36
	return *this;
athos@207
    37
      };
athos@201
    38
  
athos@207
    39
      ///Decrements the left hand side by u
athos@207
    40
      xy<T>& operator -=(const xy<T>& u){
athos@240
    41
	x -= u.x;
athos@240
    42
	y -= u.y;
athos@207
    43
	return *this;
athos@207
    44
      };
athos@201
    45
athos@207
    46
      ///Multiplying the left hand side with a scalar
athos@207
    47
      xy<T>& operator *=(const T &u){
athos@240
    48
	x *= u;
athos@240
    49
	y *= u;
athos@207
    50
	return *this;
athos@207
    51
      };
athos@207
    52
athos@207
    53
      ///Dividing the left hand side by a scalar
athos@207
    54
      xy<T>& operator /=(const T &u){
athos@240
    55
	x /= u;
athos@240
    56
	y /= u;
athos@207
    57
	return *this;
athos@207
    58
      };
athos@201
    59
  
athos@207
    60
      ///Returns the scalar product of two vectors
athos@207
    61
      T operator *(const xy<T>& u){
athos@240
    62
	return x*u.x+y*u.y;
athos@207
    63
      };
athos@201
    64
  
athos@207
    65
      ///Returns the sum of two vectors
athos@207
    66
      xy<T> operator+(const xy<T> &u) const {
athos@207
    67
	xy<T> b=*this;
athos@207
    68
	return b+=u;
athos@207
    69
      };
athos@201
    70
athos@207
    71
      ///Returns the difference of two vectors
athos@207
    72
      xy<T> operator-(const xy<T> &u) const {
athos@207
    73
	xy<T> b=*this;
athos@207
    74
	return b-=u;
athos@207
    75
      };
athos@201
    76
athos@207
    77
      ///Returns a vector multiplied by a scalar
athos@207
    78
      xy<T> operator*(const T &u) const {
athos@207
    79
	xy<T> b=*this;
athos@207
    80
	return b*=u;
athos@207
    81
      };
athos@201
    82
athos@207
    83
      ///Returns a vector divided by a scalar
athos@207
    84
      xy<T> operator/(const T &u) const {
athos@207
    85
	xy<T> b=*this;
athos@207
    86
	return b/=u;
athos@207
    87
      };
athos@201
    88
athos@207
    89
      ///Testing equality
athos@207
    90
      bool operator==(const xy<T> &u){
athos@240
    91
	return (x==u.x) && (y==u.y);
athos@207
    92
      };
athos@201
    93
athos@207
    94
      ///Testing inequality
athos@207
    95
      bool operator!=(xy u){
athos@240
    96
	return  (x!=u.x) || (y!=u.y);
athos@207
    97
      };
athos@201
    98
athos@207
    99
    };
athos@201
   100
athos@207
   101
  ///Reading a plainvector from a stream
athos@207
   102
  template<typename T>
athos@207
   103
  inline
athos@207
   104
  std::istream& operator>>(std::istream &is, xy<T> &z)
athos@207
   105
  {
athos@240
   106
athos@240
   107
    is >> z.x >> z.y;
athos@207
   108
    return is;
athos@207
   109
  }
athos@201
   110
athos@207
   111
  ///Outputting a plainvector to a stream
athos@207
   112
  template<typename T>
athos@207
   113
  inline
athos@207
   114
  std::ostream& operator<<(std::ostream &os, xy<T> z)
athos@207
   115
  {
athos@240
   116
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   117
    return os;
athos@207
   118
  }
athos@207
   119
athos@244
   120
athos@244
   121
  /** \brief
athos@244
   122
     Implementation of a bounding box of plainvectors.
athos@244
   123
     
athos@244
   124
  */
athos@244
   125
  template<typename T>
athos@244
   126
    class BoundingBox {
athos@244
   127
      xy<T> bottom_left, top_right;
athos@244
   128
      bool _empty;
athos@244
   129
    public:
athos@244
   130
      
athos@244
   131
      ///Default constructor: an empty bounding box
athos@244
   132
      BoundingBox() { _empty = true; }
athos@244
   133
athos@244
   134
      ///Constructing the instance from one point
athos@244
   135
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   136
athos@244
   137
      ///Is there any point added
athos@244
   138
      bool empty() const {
athos@244
   139
	return _empty;
athos@244
   140
      }
athos@244
   141
athos@244
   142
      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   143
      xy<T> bottomLeft() const {
athos@244
   144
	return bottom_left;
athos@244
   145
      };
athos@244
   146
athos@244
   147
      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   148
      xy<T> topRight() const {
athos@244
   149
	return top_right;
athos@244
   150
      };
athos@244
   151
athos@244
   152
      ///Checks whether a point is inside a bounding box
athos@244
   153
      bool inside(const xy<T>& u){
athos@244
   154
	if (_empty)
athos@244
   155
	  return false;
athos@244
   156
	else{
athos@244
   157
	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
athos@244
   158
		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
athos@244
   159
	}
athos@244
   160
      }
athos@244
   161
  
athos@244
   162
      ///Increments a bounding box with a point
athos@244
   163
      BoundingBox& operator +=(const xy<T>& u){
athos@244
   164
	if (_empty){
athos@244
   165
	  bottom_left=top_right=u;
athos@244
   166
	  _empty = false;
athos@244
   167
	}
athos@244
   168
	else{
athos@244
   169
	  if (bottom_left.x > u.x) bottom_left.x = u.x;
athos@244
   170
	  if (bottom_left.y > u.y) bottom_left.y = u.y;
athos@244
   171
	  if (top_right.x < u.x) top_right.x = u.x;
athos@244
   172
	  if (top_right.y < u.y) top_right.y = u.y;
athos@244
   173
	}
athos@244
   174
	return *this;
athos@244
   175
      };
athos@244
   176
  
athos@244
   177
      ///Sums a bounding box and a point
athos@244
   178
      BoundingBox operator +(const xy<T>& u){
athos@244
   179
	BoundingBox b = *this;
athos@244
   180
	return b += u;
athos@244
   181
      };
athos@244
   182
athos@244
   183
      ///Increments a bounding box with an other bounding box
athos@244
   184
      BoundingBox& operator +=(const BoundingBox &u){
athos@244
   185
	if ( !u.empty() ){
athos@244
   186
	  *this += u.bottomLeft();
athos@244
   187
	  *this += u.topRight();
athos@244
   188
	}
athos@244
   189
	return *this;
athos@244
   190
      };
athos@244
   191
  
athos@244
   192
      ///Sums two bounding boxes
athos@244
   193
      BoundingBox operator +(const BoundingBox& u){
athos@244
   194
	BoundingBox b = *this;
athos@244
   195
	return b += u;
athos@244
   196
      };
athos@244
   197
athos@244
   198
    };//class Boundingbox
athos@244
   199
athos@244
   200
athos@244
   201
athos@244
   202
athos@207
   203
} //namespace hugo
athos@201
   204
athos@201
   205
#endif //HUGO_XY_H