src/lemon/xy.h
changeset 1429 4283998fb2be
parent 1420 e37cca875667
equal deleted inserted replaced
17:38884f02f37c 18:3bbf6b88ea58
    27 /// The class \ref lemon::xy "xy" implements
    27 /// The class \ref lemon::xy "xy" implements
    28 ///a two dimensional vector with the usual
    28 ///a two dimensional vector with the usual
    29 /// operations.
    29 /// operations.
    30 ///
    30 ///
    31 /// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
    31 /// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
    32 /// the rectangular bounding box a set of \ref lemon::xy "xy"'s.
    32 /// the rectangular bounding box of a set of \ref lemon::xy "xy"'s.
    33 ///
    33 ///
    34 ///\author Attila Bernath
    34 ///\author Attila Bernath
    35 
    35 
    36 
    36 
    37 namespace lemon {
    37 namespace lemon {
    65       ///Conversion constructor
    65       ///Conversion constructor
    66       template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
    66       template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
    67 
    67 
    68       ///Gives back the square of the norm of the vector
    68       ///Gives back the square of the norm of the vector
    69       T normSquare() const {
    69       T normSquare() const {
    70 	return x*x+y*y;
    70         return x*x+y*y;
    71       }
    71       }
    72   
    72   
    73       ///Increments the left hand side by u
    73       ///Increments the left hand side by u
    74       xy<T>& operator +=(const xy<T>& u) {
    74       xy<T>& operator +=(const xy<T>& u) {
    75 	x += u.x;
    75         x += u.x;
    76 	y += u.y;
    76         y += u.y;
    77 	return *this;
    77         return *this;
    78       }
    78       }
    79   
    79   
    80       ///Decrements the left hand side by u
    80       ///Decrements the left hand side by u
    81       xy<T>& operator -=(const xy<T>& u) {
    81       xy<T>& operator -=(const xy<T>& u) {
    82 	x -= u.x;
    82         x -= u.x;
    83 	y -= u.y;
    83         y -= u.y;
    84 	return *this;
    84         return *this;
    85       }
    85       }
    86 
    86 
    87       ///Multiplying the left hand side with a scalar
    87       ///Multiplying the left hand side with a scalar
    88       xy<T>& operator *=(const T &u) {
    88       xy<T>& operator *=(const T &u) {
    89 	x *= u;
    89         x *= u;
    90 	y *= u;
    90         y *= u;
    91 	return *this;
    91         return *this;
    92       }
    92       }
    93 
    93 
    94       ///Dividing the left hand side by a scalar
    94       ///Dividing the left hand side by a scalar
    95       xy<T>& operator /=(const T &u) {
    95       xy<T>& operator /=(const T &u) {
    96 	x /= u;
    96         x /= u;
    97 	y /= u;
    97         y /= u;
    98 	return *this;
    98         return *this;
    99       }
    99       }
   100   
   100   
   101       ///Returns the scalar product of two vectors
   101       ///Returns the scalar product of two vectors
   102       T operator *(const xy<T>& u) const {
   102       T operator *(const xy<T>& u) const {
   103 	return x*u.x+y*u.y;
   103         return x*u.x+y*u.y;
   104       }
   104       }
   105   
   105   
   106       ///Returns the sum of two vectors
   106       ///Returns the sum of two vectors
   107       xy<T> operator+(const xy<T> &u) const {
   107       xy<T> operator+(const xy<T> &u) const {
   108 	xy<T> b=*this;
   108         xy<T> b=*this;
   109 	return b+=u;
   109         return b+=u;
   110       }
   110       }
   111 
   111 
   112       ///Returns the neg of the vectors
   112       ///Returns the neg of the vectors
   113       xy<T> operator-() const {
   113       xy<T> operator-() const {
   114 	xy<T> b=*this;
   114         xy<T> b=*this;
   115 	b.x=-b.x; b.y=-b.y;
   115         b.x=-b.x; b.y=-b.y;
   116 	return b;
   116         return b;
   117       }
   117       }
   118 
   118 
   119       ///Returns the difference of two vectors
   119       ///Returns the difference of two vectors
   120       xy<T> operator-(const xy<T> &u) const {
   120       xy<T> operator-(const xy<T> &u) const {
   121 	xy<T> b=*this;
   121         xy<T> b=*this;
   122 	return b-=u;
   122         return b-=u;
   123       }
   123       }
   124 
   124 
   125       ///Returns a vector multiplied by a scalar
   125       ///Returns a vector multiplied by a scalar
   126       xy<T> operator*(const T &u) const {
   126       xy<T> operator*(const T &u) const {
   127 	xy<T> b=*this;
   127         xy<T> b=*this;
   128 	return b*=u;
   128         return b*=u;
   129       }
   129       }
   130 
   130 
   131       ///Returns a vector divided by a scalar
   131       ///Returns a vector divided by a scalar
   132       xy<T> operator/(const T &u) const {
   132       xy<T> operator/(const T &u) const {
   133 	xy<T> b=*this;
   133         xy<T> b=*this;
   134 	return b/=u;
   134         return b/=u;
   135       }
   135       }
   136 
   136 
   137       ///Testing equality
   137       ///Testing equality
   138       bool operator==(const xy<T> &u) const {
   138       bool operator==(const xy<T> &u) const {
   139 	return (x==u.x) && (y==u.y);
   139         return (x==u.x) && (y==u.y);
   140       }
   140       }
   141 
   141 
   142       ///Testing inequality
   142       ///Testing inequality
   143       bool operator!=(xy u) const {
   143       bool operator!=(xy u) const {
   144 	return  (x!=u.x) || (y!=u.y);
   144         return  (x!=u.x) || (y!=u.y);
   145       }
   145       }
   146 
   146 
   147     };
   147     };
   148 
   148 
   149   ///Returns a vector multiplied by a scalar
   149   ///Returns a vector multiplied by a scalar
   227     class BoundingBox {
   227     class BoundingBox {
   228       xy<T> bottom_left, top_right;
   228       xy<T> bottom_left, top_right;
   229       bool _empty;
   229       bool _empty;
   230     public:
   230     public:
   231       
   231       
   232       ///Default constructor: an empty bounding box
   232       ///Default constructor: creates an empty bounding box
   233       BoundingBox() { _empty = true; }
   233       BoundingBox() { _empty = true; }
   234 
   234 
   235       ///Constructing the instance from one point
   235       ///Constructing the instance from one point
   236       BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
   236       BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
   237 
   237 
   238       ///Is there any point added
   238       ///Were any points added?
   239       bool empty() const {
   239       bool empty() const {
   240 	return _empty;
   240         return _empty;
   241       }
   241       }
   242 
   242 
   243       ///Makes the BoundingBox empty
   243       ///Makes the BoundingBox empty
   244       void clear() {
   244       void clear() {
   245 	_empty=1;
   245         _empty=1;
   246       }
   246       }
   247 
   247 
   248       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
   248       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
   249       xy<T> bottomLeft() const {
   249       xy<T> bottomLeft() const {
   250 	return bottom_left;
   250         return bottom_left;
   251       }
   251       }
   252 
   252 
   253       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
   253       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
   254       xy<T> topRight() const {
   254       xy<T> topRight() const {
   255 	return top_right;
   255         return top_right;
   256       }
   256       }
   257 
   257 
   258       ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
   258       ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
   259       xy<T> bottomRight() const {
   259       xy<T> bottomRight() const {
   260 	return xy<T>(top_right.x,bottom_left.y);
   260         return xy<T>(top_right.x,bottom_left.y);
   261       }
   261       }
   262 
   262 
   263       ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
   263       ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
   264       xy<T> topLeft() const {
   264       xy<T> topLeft() const {
   265 	return xy<T>(bottom_left.x,top_right.y);
   265         return xy<T>(bottom_left.x,top_right.y);
   266       }
   266       }
   267 
   267 
   268       ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
   268       ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
   269       T bottom() const {
   269       T bottom() const {
   270 	return bottom_left.y;
   270         return bottom_left.y;
   271       }
   271       }
   272 
   272 
   273       ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
   273       ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
   274       T top() const {
   274       T top() const {
   275 	return top_right.y;
   275         return top_right.y;
   276       }
   276       }
   277 
   277 
   278       ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
   278       ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
   279       T left() const {
   279       T left() const {
   280 	return bottom_left.x;
   280         return bottom_left.x;
   281       }
   281       }
   282 
   282 
   283       ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
   283       ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
   284       T right() const {
   284       T right() const {
   285 	return top_right.x;
   285         return top_right.x;
   286       }
   286       }
   287 
   287 
   288       ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined) 
   288       ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined) 
   289       T height() const {
   289       T height() const {
   290 	return top_right.y-bottom_left.y;
   290         return top_right.y-bottom_left.y;
   291       }
   291       }
   292 
   292 
   293       ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined) 
   293       ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined) 
   294       T width() const {
   294       T width() const {
   295 	return top_right.x-bottom_left.x;
   295         return top_right.x-bottom_left.x;
   296       }
   296       }
   297 
   297 
   298       ///Checks whether a point is inside a bounding box
   298       ///Checks whether a point is inside a bounding box
   299       bool inside(const xy<T>& u){
   299       bool inside(const xy<T>& u){
   300 	if (_empty)
   300         if (_empty)
   301 	  return false;
   301           return false;
   302 	else{
   302         else{
   303 	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
   303           return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
   304 		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
   304               (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
   305 	}
   305         }
   306       }
   306       }
   307   
   307   
   308       ///Increments a bounding box with a point
   308       ///Increments a bounding box with a point
   309       BoundingBox& operator +=(const xy<T>& u){
   309       BoundingBox& operator +=(const xy<T>& u){
   310 	if (_empty){
   310         if (_empty){
   311 	  bottom_left=top_right=u;
   311           bottom_left=top_right=u;
   312 	  _empty = false;
   312           _empty = false;
   313 	}
   313         }
   314 	else{
   314         else{
   315 	  if (bottom_left.x > u.x) bottom_left.x = u.x;
   315           if (bottom_left.x > u.x) bottom_left.x = u.x;
   316 	  if (bottom_left.y > u.y) bottom_left.y = u.y;
   316           if (bottom_left.y > u.y) bottom_left.y = u.y;
   317 	  if (top_right.x < u.x) top_right.x = u.x;
   317           if (top_right.x < u.x) top_right.x = u.x;
   318 	  if (top_right.y < u.y) top_right.y = u.y;
   318           if (top_right.y < u.y) top_right.y = u.y;
   319 	}
   319         }
   320 	return *this;
   320         return *this;
   321       }
   321       }
   322   
   322   
   323       ///Sums a bounding box and a point
   323       ///Sums a bounding box and a point
   324       BoundingBox operator +(const xy<T>& u){
   324       BoundingBox operator +(const xy<T>& u){
   325 	BoundingBox b = *this;
   325         BoundingBox b = *this;
   326 	return b += u;
   326         return b += u;
   327       }
   327       }
   328 
   328 
   329       ///Increments a bounding box with an other bounding box
   329       ///Increments a bounding box with an other bounding box
   330       BoundingBox& operator +=(const BoundingBox &u){
   330       BoundingBox& operator +=(const BoundingBox &u){
   331 	if ( !u.empty() ){
   331         if ( !u.empty() ){
   332 	  *this += u.bottomLeft();
   332           *this += u.bottomLeft();
   333 	  *this += u.topRight();
   333           *this += u.topRight();
   334 	}
   334         }
   335 	return *this;
   335         return *this;
   336       }
   336       }
   337   
   337   
   338       ///Sums two bounding boxes
   338       ///Sums two bounding boxes
   339       BoundingBox operator +(const BoundingBox& u){
   339       BoundingBox operator +(const BoundingBox& u){
   340 	BoundingBox b = *this;
   340         BoundingBox b = *this;
   341 	return b += u;
   341         return b += u;
   342       }
   342       }
   343 
   343 
   344     };//class Boundingbox
   344     };//class Boundingbox
   345 
   345 
   346 
   346