src/lemon/xy.h
author alpar
Tue, 04 Jan 2005 17:06:20 +0000
changeset 1045 1bf336c63f25
parent 987 87f7c54892df
child 1049 e27446e1deda
permissions -rw-r--r--
Several new member functions added to BoundingBox
     1 /* -*- C++ -*-
     2  * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Combinatorial Optimization Research Group, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_XY_H
    18 #define LEMON_XY_H
    19 
    20 #include <iostream>
    21 
    22 ///\ingroup misc
    23 ///\file
    24 ///\brief A simple two dimensional vector and a bounding box implementation 
    25 ///
    26 /// The class \ref lemon::xy "xy" implements
    27 ///a two dimensional vector with the usual
    28 /// operations.
    29 ///
    30 /// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
    31 /// the rectangular bounding box a set of \ref lemon::xy "xy"'s.
    32 ///
    33 ///\author Attila Bernath
    34 
    35 
    36 namespace lemon {
    37 
    38   /// \addtogroup misc
    39   /// @{
    40 
    41   /// A two dimensional vector (plainvector) implementation
    42 
    43   /// A two dimensional vector (plainvector) implementation
    44   ///with the usual vector
    45   /// operators.
    46   ///
    47   ///\author Attila Bernath
    48   template<typename T>
    49     class xy {
    50 
    51     public:
    52 
    53       typedef T Value;
    54 
    55       T x,y;     
    56       
    57       ///Default constructor: both coordinates become 0
    58       xy() : x(0), y(0) {}
    59 
    60       ///Constructing the instance from coordinates
    61       xy(T a, T b) : x(a), y(b) { }
    62 
    63 
    64       ///Gives back the square of the norm of the vector
    65       T normSquare(){
    66 	return x*x+y*y;
    67       };
    68   
    69       ///Increments the left hand side by u
    70       xy<T>& operator +=(const xy<T>& u){
    71 	x += u.x;
    72 	y += u.y;
    73 	return *this;
    74       };
    75   
    76       ///Decrements the left hand side by u
    77       xy<T>& operator -=(const xy<T>& u){
    78 	x -= u.x;
    79 	y -= u.y;
    80 	return *this;
    81       };
    82 
    83       ///Multiplying the left hand side with a scalar
    84       xy<T>& operator *=(const T &u){
    85 	x *= u;
    86 	y *= u;
    87 	return *this;
    88       };
    89 
    90       ///Dividing the left hand side by a scalar
    91       xy<T>& operator /=(const T &u){
    92 	x /= u;
    93 	y /= u;
    94 	return *this;
    95       };
    96   
    97       ///Returns the scalar product of two vectors
    98       T operator *(const xy<T>& u){
    99 	return x*u.x+y*u.y;
   100       };
   101   
   102       ///Returns the sum of two vectors
   103       xy<T> operator+(const xy<T> &u) const {
   104 	xy<T> b=*this;
   105 	return b+=u;
   106       };
   107 
   108       ///Returns the difference of two vectors
   109       xy<T> operator-(const xy<T> &u) const {
   110 	xy<T> b=*this;
   111 	return b-=u;
   112       };
   113 
   114       ///Returns a vector multiplied by a scalar
   115       xy<T> operator*(const T &u) const {
   116 	xy<T> b=*this;
   117 	return b*=u;
   118       };
   119 
   120       ///Returns a vector divided by a scalar
   121       xy<T> operator/(const T &u) const {
   122 	xy<T> b=*this;
   123 	return b/=u;
   124       };
   125 
   126       ///Testing equality
   127       bool operator==(const xy<T> &u){
   128 	return (x==u.x) && (y==u.y);
   129       };
   130 
   131       ///Testing inequality
   132       bool operator!=(xy u){
   133 	return  (x!=u.x) || (y!=u.y);
   134       };
   135 
   136     };
   137 
   138   ///Read a plainvector from a stream
   139 
   140   ///Read a plainvector from a stream
   141   ///\relates xy
   142   ///
   143   template<typename T>
   144   inline
   145   std::istream& operator>>(std::istream &is, xy<T> &z)
   146   {
   147 
   148     is >> z.x >> z.y;
   149     return is;
   150   }
   151 
   152   ///Write a plainvector to a stream
   153 
   154   ///Write a plainvector to a stream
   155   ///\relates xy
   156   ///
   157   template<typename T>
   158   inline
   159   std::ostream& operator<<(std::ostream &os, xy<T> z)
   160   {
   161     os << "(" << z.x << ", " << z.y << ")";
   162     return os;
   163   }
   164 
   165 
   166   /// A class to calculate or store the bounding box of plainvectors.
   167 
   168   /// A class to calculate or store the bounding box of plainvectors.
   169   ///
   170   ///\author Attila Bernath
   171   template<typename T>
   172     class BoundingBox {
   173       xy<T> bottom_left, top_right;
   174       bool _empty;
   175     public:
   176       
   177       ///Default constructor: an empty bounding box
   178       BoundingBox() { _empty = true; }
   179 
   180       ///Constructing the instance from one point
   181       BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
   182 
   183       ///Is there any point added
   184       bool empty() const {
   185 	return _empty;
   186       }
   187 
   188       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
   189       xy<T> bottomLeft() const {
   190 	return bottom_left;
   191       };
   192 
   193       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
   194       xy<T> topRight() const {
   195 	return top_right;
   196       };
   197 
   198       ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
   199       xy<T> bottomRight() const {
   200 	return xy<T>(top_right.x,bottom_left.y);
   201       };
   202 
   203       ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
   204       xy<T> topLeft() const {
   205 	return xy<T>(bottom_left.x,top_right.y);
   206       };
   207 
   208       ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
   209       T bottom() const {
   210 	return bottom_left.y;
   211       };
   212 
   213       ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
   214       T top() const {
   215 	return top_right.y;
   216       };
   217 
   218       ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
   219       T left() const {
   220 	return bottom_left.x;
   221       };
   222 
   223       ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
   224       T right() const {
   225 	return top_right.x;
   226       };
   227 
   228       ///Checks whether a point is inside a bounding box
   229       bool inside(const xy<T>& u){
   230 	if (_empty)
   231 	  return false;
   232 	else{
   233 	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
   234 		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
   235 	}
   236       }
   237   
   238       ///Increments a bounding box with a point
   239       BoundingBox& operator +=(const xy<T>& u){
   240 	if (_empty){
   241 	  bottom_left=top_right=u;
   242 	  _empty = false;
   243 	}
   244 	else{
   245 	  if (bottom_left.x > u.x) bottom_left.x = u.x;
   246 	  if (bottom_left.y > u.y) bottom_left.y = u.y;
   247 	  if (top_right.x < u.x) top_right.x = u.x;
   248 	  if (top_right.y < u.y) top_right.y = u.y;
   249 	}
   250 	return *this;
   251       };
   252   
   253       ///Sums a bounding box and a point
   254       BoundingBox operator +(const xy<T>& u){
   255 	BoundingBox b = *this;
   256 	return b += u;
   257       };
   258 
   259       ///Increments a bounding box with an other bounding box
   260       BoundingBox& operator +=(const BoundingBox &u){
   261 	if ( !u.empty() ){
   262 	  *this += u.bottomLeft();
   263 	  *this += u.topRight();
   264 	}
   265 	return *this;
   266       };
   267   
   268       ///Sums two bounding boxes
   269       BoundingBox operator +(const BoundingBox& u){
   270 	BoundingBox b = *this;
   271 	return b += u;
   272       };
   273 
   274     };//class Boundingbox
   275 
   276 
   277   /// @}
   278 
   279 
   280 } //namespace lemon
   281 
   282 #endif //LEMON_XY_H