src/lemon/xy.h
changeset 1391 5b46af577b23
parent 1359 1581f961cfaa
child 1392 b87aa8f0feb8
equal deleted inserted replaced
14:c3fa1e4bc688 15:c03ebc13c474
    65       template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
    65       template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
    66 
    66 
    67       ///Gives back the square of the norm of the vector
    67       ///Gives back the square of the norm of the vector
    68       T normSquare() const {
    68       T normSquare() const {
    69 	return x*x+y*y;
    69 	return x*x+y*y;
    70       };
    70       }
    71   
    71   
    72       ///Increments the left hand side by u
    72       ///Increments the left hand side by u
    73       xy<T>& operator +=(const xy<T>& u) {
    73       xy<T>& operator +=(const xy<T>& u) {
    74 	x += u.x;
    74 	x += u.x;
    75 	y += u.y;
    75 	y += u.y;
    76 	return *this;
    76 	return *this;
    77       };
    77       }
    78   
    78   
    79       ///Decrements the left hand side by u
    79       ///Decrements the left hand side by u
    80       xy<T>& operator -=(const xy<T>& u) {
    80       xy<T>& operator -=(const xy<T>& u) {
    81 	x -= u.x;
    81 	x -= u.x;
    82 	y -= u.y;
    82 	y -= u.y;
    83 	return *this;
    83 	return *this;
    84       };
    84       }
    85 
    85 
    86       ///Multiplying the left hand side with a scalar
    86       ///Multiplying the left hand side with a scalar
    87       xy<T>& operator *=(const T &u) {
    87       xy<T>& operator *=(const T &u) {
    88 	x *= u;
    88 	x *= u;
    89 	y *= u;
    89 	y *= u;
    90 	return *this;
    90 	return *this;
    91       };
    91       }
    92 
    92 
    93       ///Dividing the left hand side by a scalar
    93       ///Dividing the left hand side by a scalar
    94       xy<T>& operator /=(const T &u) {
    94       xy<T>& operator /=(const T &u) {
    95 	x /= u;
    95 	x /= u;
    96 	y /= u;
    96 	y /= u;
    97 	return *this;
    97 	return *this;
    98       };
    98       }
    99   
    99   
   100       ///Returns the scalar product of two vectors
   100       ///Returns the scalar product of two vectors
   101       T operator *(const xy<T>& u) const {
   101       T operator *(const xy<T>& u) const {
   102 	return x*u.x+y*u.y;
   102 	return x*u.x+y*u.y;
   103       };
   103       }
   104   
   104   
   105       ///Returns the sum of two vectors
   105       ///Returns the sum of two vectors
   106       xy<T> operator+(const xy<T> &u) const {
   106       xy<T> operator+(const xy<T> &u) const {
   107 	xy<T> b=*this;
   107 	xy<T> b=*this;
   108 	return b+=u;
   108 	return b+=u;
   109       };
   109       }
   110 
   110 
   111       ///Returns the neg of the vectors
   111       ///Returns the neg of the vectors
   112       xy<T> operator-() const {
   112       xy<T> operator-() const {
   113 	xy<T> b=*this;
   113 	xy<T> b=*this;
   114 	b.x=-b.x; b.y=-b.y;
   114 	b.x=-b.x; b.y=-b.y;
   115 	return b;
   115 	return b;
   116       };
   116       }
   117 
   117 
   118       ///Returns the difference of two vectors
   118       ///Returns the difference of two vectors
   119       xy<T> operator-(const xy<T> &u) const {
   119       xy<T> operator-(const xy<T> &u) const {
   120 	xy<T> b=*this;
   120 	xy<T> b=*this;
   121 	return b-=u;
   121 	return b-=u;
   122       };
   122       }
   123 
   123 
   124       ///Returns a vector multiplied by a scalar
   124       ///Returns a vector multiplied by a scalar
   125       xy<T> operator*(const T &u) const {
   125       xy<T> operator*(const T &u) const {
   126 	xy<T> b=*this;
   126 	xy<T> b=*this;
   127 	return b*=u;
   127 	return b*=u;
   128       };
   128       }
   129 
   129 
   130       ///Returns a vector divided by a scalar
   130       ///Returns a vector divided by a scalar
   131       xy<T> operator/(const T &u) const {
   131       xy<T> operator/(const T &u) const {
   132 	xy<T> b=*this;
   132 	xy<T> b=*this;
   133 	return b/=u;
   133 	return b/=u;
   134       };
   134       }
   135 
   135 
   136       ///Testing equality
   136       ///Testing equality
   137       bool operator==(const xy<T> &u) const {
   137       bool operator==(const xy<T> &u) const {
   138 	return (x==u.x) && (y==u.y);
   138 	return (x==u.x) && (y==u.y);
   139       };
   139       }
   140 
   140 
   141       ///Testing inequality
   141       ///Testing inequality
   142       bool operator!=(xy u) const {
   142       bool operator!=(xy u) const {
   143 	return  (x!=u.x) || (y!=u.y);
   143 	return  (x!=u.x) || (y!=u.y);
   144       };
   144       }
   145 
   145 
   146     };
   146     };
   147 
   147 
   148   ///Returns a vector multiplied by a scalar
   148   ///Returns a vector multiplied by a scalar
   149 
   149 
   150   ///Returns a vector multiplied by a scalar
   150   ///Returns a vector multiplied by a scalar
   151   ///\relates xy
   151   ///\relates xy
   152   template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
   152   template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
   153     return x*u;
   153     return x*u;
   154   };
   154   }
   155 
   155 
   156   ///Read a plainvector from a stream
   156   ///Read a plainvector from a stream
   157 
   157 
   158   ///Read a plainvector from a stream
   158   ///Read a plainvector from a stream
   159   ///\relates xy
   159   ///\relates xy
   224       ///Is there any point added
   224       ///Is there any point added
   225       bool empty() const {
   225       bool empty() const {
   226 	return _empty;
   226 	return _empty;
   227       }
   227       }
   228 
   228 
       
   229       ///Makes the BoundingBox empty
       
   230       void clear() {
       
   231 	_empty=1;
       
   232       }
       
   233 
   229       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
   234       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
   230       xy<T> bottomLeft() const {
   235       xy<T> bottomLeft() const {
   231 	return bottom_left;
   236 	return bottom_left;
   232       };
   237       }
   233 
   238 
   234       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
   239       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
   235       xy<T> topRight() const {
   240       xy<T> topRight() const {
   236 	return top_right;
   241 	return top_right;
   237       };
   242       }
   238 
   243 
   239       ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
   244       ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
   240       xy<T> bottomRight() const {
   245       xy<T> bottomRight() const {
   241 	return xy<T>(top_right.x,bottom_left.y);
   246 	return xy<T>(top_right.x,bottom_left.y);
   242       };
   247       }
   243 
   248 
   244       ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
   249       ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
   245       xy<T> topLeft() const {
   250       xy<T> topLeft() const {
   246 	return xy<T>(bottom_left.x,top_right.y);
   251 	return xy<T>(bottom_left.x,top_right.y);
   247       };
   252       }
   248 
   253 
   249       ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
   254       ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
   250       T bottom() const {
   255       T bottom() const {
   251 	return bottom_left.y;
   256 	return bottom_left.y;
   252       };
   257       }
   253 
   258 
   254       ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
   259       ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
   255       T top() const {
   260       T top() const {
   256 	return top_right.y;
   261 	return top_right.y;
   257       };
   262       }
   258 
   263 
   259       ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
   264       ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
   260       T left() const {
   265       T left() const {
   261 	return bottom_left.x;
   266 	return bottom_left.x;
   262       };
   267       }
   263 
   268 
   264       ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
   269       ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
   265       T right() const {
   270       T right() const {
   266 	return top_right.x;
   271 	return top_right.x;
   267       };
   272       }
   268 
   273 
   269       ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined) 
   274       ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined) 
   270       T height() const {
   275       T height() const {
   271 	return top_right.y-bottom_left.y;
   276 	return top_right.y-bottom_left.y;
   272       };
   277       }
   273 
   278 
   274       ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined) 
   279       ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined) 
   275       T width() const {
   280       T width() const {
   276 	return top_right.x-bottom_left.x;
   281 	return top_right.x-bottom_left.x;
   277       };
   282       }
   278 
   283 
   279       ///Checks whether a point is inside a bounding box
   284       ///Checks whether a point is inside a bounding box
   280       bool inside(const xy<T>& u){
   285       bool inside(const xy<T>& u){
   281 	if (_empty)
   286 	if (_empty)
   282 	  return false;
   287 	  return false;
   297 	  if (bottom_left.y > u.y) bottom_left.y = u.y;
   302 	  if (bottom_left.y > u.y) bottom_left.y = u.y;
   298 	  if (top_right.x < u.x) top_right.x = u.x;
   303 	  if (top_right.x < u.x) top_right.x = u.x;
   299 	  if (top_right.y < u.y) top_right.y = u.y;
   304 	  if (top_right.y < u.y) top_right.y = u.y;
   300 	}
   305 	}
   301 	return *this;
   306 	return *this;
   302       };
   307       }
   303   
   308   
   304       ///Sums a bounding box and a point
   309       ///Sums a bounding box and a point
   305       BoundingBox operator +(const xy<T>& u){
   310       BoundingBox operator +(const xy<T>& u){
   306 	BoundingBox b = *this;
   311 	BoundingBox b = *this;
   307 	return b += u;
   312 	return b += u;
   308       };
   313       }
   309 
   314 
   310       ///Increments a bounding box with an other bounding box
   315       ///Increments a bounding box with an other bounding box
   311       BoundingBox& operator +=(const BoundingBox &u){
   316       BoundingBox& operator +=(const BoundingBox &u){
   312 	if ( !u.empty() ){
   317 	if ( !u.empty() ){
   313 	  *this += u.bottomLeft();
   318 	  *this += u.bottomLeft();
   314 	  *this += u.topRight();
   319 	  *this += u.topRight();
   315 	}
   320 	}
   316 	return *this;
   321 	return *this;
   317       };
   322       }
   318   
   323   
   319       ///Sums two bounding boxes
   324       ///Sums two bounding boxes
   320       BoundingBox operator +(const BoundingBox& u){
   325       BoundingBox operator +(const BoundingBox& u){
   321 	BoundingBox b = *this;
   326 	BoundingBox b = *this;
   322 	return b += u;
   327 	return b += u;
   323       };
   328       }
   324 
   329 
   325     };//class Boundingbox
   330     };//class Boundingbox
   326 
   331 
   327 
   332 
   328   ///Map of x-coordinates of an xy<>-map
   333   ///Map of x-coordinates of an xy<>-map