src/work/athos/xy/xy.h
changeset 246 dc95ca4ebc7b
parent 242 b255f25ad394
child 249 0b0bdf24d00c
equal deleted inserted replaced
4:4fb59affa366 5:d4155bc41fb0
   115   {
   115   {
   116     os << "(" << z.x << ", " << z.y << ")";
   116     os << "(" << z.x << ", " << z.y << ")";
   117     return os;
   117     return os;
   118   }
   118   }
   119 
   119 
       
   120 
       
   121   /** \brief
       
   122      Implementation of a bounding box of plainvectors.
       
   123      
       
   124   */
       
   125   template<typename T>
       
   126     class BoundingBox {
       
   127       xy<T> bottom_left, top_right;
       
   128       bool _empty;
       
   129     public:
       
   130       
       
   131       ///Default constructor: an empty bounding box
       
   132       BoundingBox() { _empty = true; }
       
   133 
       
   134       ///Constructing the instance from one point
       
   135       BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
       
   136 
       
   137       ///Is there any point added
       
   138       bool empty() const {
       
   139 	return _empty;
       
   140       }
       
   141 
       
   142       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
       
   143       xy<T> bottomLeft() const {
       
   144 	return bottom_left;
       
   145       };
       
   146 
       
   147       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
       
   148       xy<T> topRight() const {
       
   149 	return top_right;
       
   150       };
       
   151 
       
   152       ///Checks whether a point is inside a bounding box
       
   153       bool inside(const xy<T>& u){
       
   154 	if (_empty)
       
   155 	  return false;
       
   156 	else{
       
   157 	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
       
   158 		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
       
   159 	}
       
   160       }
       
   161   
       
   162       ///Increments a bounding box with a point
       
   163       BoundingBox& operator +=(const xy<T>& u){
       
   164 	if (_empty){
       
   165 	  bottom_left=top_right=u;
       
   166 	  _empty = false;
       
   167 	}
       
   168 	else{
       
   169 	  if (bottom_left.x > u.x) bottom_left.x = u.x;
       
   170 	  if (bottom_left.y > u.y) bottom_left.y = u.y;
       
   171 	  if (top_right.x < u.x) top_right.x = u.x;
       
   172 	  if (top_right.y < u.y) top_right.y = u.y;
       
   173 	}
       
   174 	return *this;
       
   175       };
       
   176   
       
   177       ///Sums a bounding box and a point
       
   178       BoundingBox operator +(const xy<T>& u){
       
   179 	BoundingBox b = *this;
       
   180 	return b += u;
       
   181       };
       
   182 
       
   183       ///Increments a bounding box with an other bounding box
       
   184       BoundingBox& operator +=(const BoundingBox &u){
       
   185 	if ( !u.empty() ){
       
   186 	  *this += u.bottomLeft();
       
   187 	  *this += u.topRight();
       
   188 	}
       
   189 	return *this;
       
   190       };
       
   191   
       
   192       ///Sums two bounding boxes
       
   193       BoundingBox operator +(const BoundingBox& u){
       
   194 	BoundingBox b = *this;
       
   195 	return b += u;
       
   196       };
       
   197 
       
   198     };//class Boundingbox
       
   199 
       
   200 
       
   201 
       
   202 
   120 } //namespace hugo
   203 } //namespace hugo
   121 
   204 
   122 #endif //HUGO_XY_H
   205 #endif //HUGO_XY_H