src/lemon/xy.h
changeset 921 818510fa3d99
parent 906 17f31d280385
child 964 2c0c20e90116
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lemon/xy.h	Wed Sep 29 15:30:04 2004 +0000
     1.3 @@ -0,0 +1,248 @@
     1.4 +/* -*- C++ -*-
     1.5 + * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#ifndef LEMON_XY_H
    1.21 +#define LEMON_XY_H
    1.22 +
    1.23 +#include <iostream>
    1.24 +
    1.25 +///\ingroup misc
    1.26 +///\file
    1.27 +///\brief A simple two dimensional vector and a bounding box implementation 
    1.28 +///
    1.29 +/// The class \ref lemon::xy "xy" implements
    1.30 +///a two dimensional vector with the usual
    1.31 +/// operations.
    1.32 +///
    1.33 +/// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
    1.34 +/// the rectangular bounding box a set of \ref lemon::xy "xy"'s.
    1.35 +///
    1.36 +///\author Attila Bernath
    1.37 +
    1.38 +
    1.39 +namespace lemon {
    1.40 +
    1.41 +  /// \addtogroup misc
    1.42 +  /// @{
    1.43 +
    1.44 +  /// A two dimensional vector (plainvector) implementation
    1.45 +
    1.46 +  /// A two dimensional vector (plainvector) implementation
    1.47 +  ///with the usual vector
    1.48 +  /// operators.
    1.49 +  ///
    1.50 +  ///\author Attila Bernath
    1.51 +  template<typename T>
    1.52 +    class xy {
    1.53 +
    1.54 +    public:
    1.55 +
    1.56 +      T x,y;     
    1.57 +      
    1.58 +      ///Default constructor: both coordinates become 0
    1.59 +      xy() : x(0), y(0) {}
    1.60 +
    1.61 +      ///Constructing the instance from coordinates
    1.62 +      xy(T a, T b) : x(a), y(b) { }
    1.63 +
    1.64 +
    1.65 +      ///Gives back the square of the norm of the vector
    1.66 +      T normSquare(){
    1.67 +	return x*x+y*y;
    1.68 +      };
    1.69 +  
    1.70 +      ///Increments the left hand side by u
    1.71 +      xy<T>& operator +=(const xy<T>& u){
    1.72 +	x += u.x;
    1.73 +	y += u.y;
    1.74 +	return *this;
    1.75 +      };
    1.76 +  
    1.77 +      ///Decrements the left hand side by u
    1.78 +      xy<T>& operator -=(const xy<T>& u){
    1.79 +	x -= u.x;
    1.80 +	y -= u.y;
    1.81 +	return *this;
    1.82 +      };
    1.83 +
    1.84 +      ///Multiplying the left hand side with a scalar
    1.85 +      xy<T>& operator *=(const T &u){
    1.86 +	x *= u;
    1.87 +	y *= u;
    1.88 +	return *this;
    1.89 +      };
    1.90 +
    1.91 +      ///Dividing the left hand side by a scalar
    1.92 +      xy<T>& operator /=(const T &u){
    1.93 +	x /= u;
    1.94 +	y /= u;
    1.95 +	return *this;
    1.96 +      };
    1.97 +  
    1.98 +      ///Returns the scalar product of two vectors
    1.99 +      T operator *(const xy<T>& u){
   1.100 +	return x*u.x+y*u.y;
   1.101 +      };
   1.102 +  
   1.103 +      ///Returns the sum of two vectors
   1.104 +      xy<T> operator+(const xy<T> &u) const {
   1.105 +	xy<T> b=*this;
   1.106 +	return b+=u;
   1.107 +      };
   1.108 +
   1.109 +      ///Returns the difference of two vectors
   1.110 +      xy<T> operator-(const xy<T> &u) const {
   1.111 +	xy<T> b=*this;
   1.112 +	return b-=u;
   1.113 +      };
   1.114 +
   1.115 +      ///Returns a vector multiplied by a scalar
   1.116 +      xy<T> operator*(const T &u) const {
   1.117 +	xy<T> b=*this;
   1.118 +	return b*=u;
   1.119 +      };
   1.120 +
   1.121 +      ///Returns a vector divided by a scalar
   1.122 +      xy<T> operator/(const T &u) const {
   1.123 +	xy<T> b=*this;
   1.124 +	return b/=u;
   1.125 +      };
   1.126 +
   1.127 +      ///Testing equality
   1.128 +      bool operator==(const xy<T> &u){
   1.129 +	return (x==u.x) && (y==u.y);
   1.130 +      };
   1.131 +
   1.132 +      ///Testing inequality
   1.133 +      bool operator!=(xy u){
   1.134 +	return  (x!=u.x) || (y!=u.y);
   1.135 +      };
   1.136 +
   1.137 +    };
   1.138 +
   1.139 +  ///Read a plainvector from a stream
   1.140 +
   1.141 +  ///\relates xy
   1.142 +  ///
   1.143 +  template<typename T>
   1.144 +  inline
   1.145 +  std::istream& operator>>(std::istream &is, xy<T> &z)
   1.146 +  {
   1.147 +
   1.148 +    is >> z.x >> z.y;
   1.149 +    return is;
   1.150 +  }
   1.151 +
   1.152 +  ///Write a plainvector to a stream
   1.153 +
   1.154 +  ///\relates xy
   1.155 +  ///
   1.156 +  template<typename T>
   1.157 +  inline
   1.158 +  std::ostream& operator<<(std::ostream &os, xy<T> z)
   1.159 +  {
   1.160 +    os << "(" << z.x << ", " << z.y << ")";
   1.161 +    return os;
   1.162 +  }
   1.163 +
   1.164 +
   1.165 +  /// A class to calculate or store the bounding box of plainvectors.
   1.166 +
   1.167 +  /// A class to calculate or store the bounding box of plainvectors.
   1.168 +  ///
   1.169 +  ///\author Attila Bernath
   1.170 +  template<typename T>
   1.171 +    class BoundingBox {
   1.172 +      xy<T> bottom_left, top_right;
   1.173 +      bool _empty;
   1.174 +    public:
   1.175 +      
   1.176 +      ///Default constructor: an empty bounding box
   1.177 +      BoundingBox() { _empty = true; }
   1.178 +
   1.179 +      ///Constructing the instance from one point
   1.180 +      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
   1.181 +
   1.182 +      ///Is there any point added
   1.183 +      bool empty() const {
   1.184 +	return _empty;
   1.185 +      }
   1.186 +
   1.187 +      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
   1.188 +      xy<T> bottomLeft() const {
   1.189 +	return bottom_left;
   1.190 +      };
   1.191 +
   1.192 +      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
   1.193 +      xy<T> topRight() const {
   1.194 +	return top_right;
   1.195 +      };
   1.196 +
   1.197 +      ///Checks whether a point is inside a bounding box
   1.198 +      bool inside(const xy<T>& u){
   1.199 +	if (_empty)
   1.200 +	  return false;
   1.201 +	else{
   1.202 +	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
   1.203 +		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
   1.204 +	}
   1.205 +      }
   1.206 +  
   1.207 +      ///Increments a bounding box with a point
   1.208 +      BoundingBox& operator +=(const xy<T>& u){
   1.209 +	if (_empty){
   1.210 +	  bottom_left=top_right=u;
   1.211 +	  _empty = false;
   1.212 +	}
   1.213 +	else{
   1.214 +	  if (bottom_left.x > u.x) bottom_left.x = u.x;
   1.215 +	  if (bottom_left.y > u.y) bottom_left.y = u.y;
   1.216 +	  if (top_right.x < u.x) top_right.x = u.x;
   1.217 +	  if (top_right.y < u.y) top_right.y = u.y;
   1.218 +	}
   1.219 +	return *this;
   1.220 +      };
   1.221 +  
   1.222 +      ///Sums a bounding box and a point
   1.223 +      BoundingBox operator +(const xy<T>& u){
   1.224 +	BoundingBox b = *this;
   1.225 +	return b += u;
   1.226 +      };
   1.227 +
   1.228 +      ///Increments a bounding box with an other bounding box
   1.229 +      BoundingBox& operator +=(const BoundingBox &u){
   1.230 +	if ( !u.empty() ){
   1.231 +	  *this += u.bottomLeft();
   1.232 +	  *this += u.topRight();
   1.233 +	}
   1.234 +	return *this;
   1.235 +      };
   1.236 +  
   1.237 +      ///Sums two bounding boxes
   1.238 +      BoundingBox operator +(const BoundingBox& u){
   1.239 +	BoundingBox b = *this;
   1.240 +	return b += u;
   1.241 +      };
   1.242 +
   1.243 +    };//class Boundingbox
   1.244 +
   1.245 +
   1.246 +  /// @}
   1.247 +
   1.248 +
   1.249 +} //namespace lemon
   1.250 +
   1.251 +#endif //LEMON_XY_H