src/lemon/xy.h
author ladanyi
Mon, 11 Oct 2004 18:02:48 +0000
changeset 942 75fdd0c6866d
parent 906 17f31d280385
child 964 2c0c20e90116
permissions -rw-r--r--
Naming and coding style fixes and various other changes.
     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       T x,y;     
    54       
    55       ///Default constructor: both coordinates become 0
    56       xy() : x(0), y(0) {}
    57 
    58       ///Constructing the instance from coordinates
    59       xy(T a, T b) : x(a), y(b) { }
    60 
    61 
    62       ///Gives back the square of the norm of the vector
    63       T normSquare(){
    64 	return x*x+y*y;
    65       };
    66   
    67       ///Increments the left hand side by u
    68       xy<T>& operator +=(const xy<T>& u){
    69 	x += u.x;
    70 	y += u.y;
    71 	return *this;
    72       };
    73   
    74       ///Decrements the left hand side by u
    75       xy<T>& operator -=(const xy<T>& u){
    76 	x -= u.x;
    77 	y -= u.y;
    78 	return *this;
    79       };
    80 
    81       ///Multiplying the left hand side with a scalar
    82       xy<T>& operator *=(const T &u){
    83 	x *= u;
    84 	y *= u;
    85 	return *this;
    86       };
    87 
    88       ///Dividing the left hand side by a scalar
    89       xy<T>& operator /=(const T &u){
    90 	x /= u;
    91 	y /= u;
    92 	return *this;
    93       };
    94   
    95       ///Returns the scalar product of two vectors
    96       T operator *(const xy<T>& u){
    97 	return x*u.x+y*u.y;
    98       };
    99   
   100       ///Returns the sum of two vectors
   101       xy<T> operator+(const xy<T> &u) const {
   102 	xy<T> b=*this;
   103 	return b+=u;
   104       };
   105 
   106       ///Returns the difference of two vectors
   107       xy<T> operator-(const xy<T> &u) const {
   108 	xy<T> b=*this;
   109 	return b-=u;
   110       };
   111 
   112       ///Returns a vector multiplied by a scalar
   113       xy<T> operator*(const T &u) const {
   114 	xy<T> b=*this;
   115 	return b*=u;
   116       };
   117 
   118       ///Returns a vector divided by a scalar
   119       xy<T> operator/(const T &u) const {
   120 	xy<T> b=*this;
   121 	return b/=u;
   122       };
   123 
   124       ///Testing equality
   125       bool operator==(const xy<T> &u){
   126 	return (x==u.x) && (y==u.y);
   127       };
   128 
   129       ///Testing inequality
   130       bool operator!=(xy u){
   131 	return  (x!=u.x) || (y!=u.y);
   132       };
   133 
   134     };
   135 
   136   ///Read a plainvector from a stream
   137 
   138   ///\relates xy
   139   ///
   140   template<typename T>
   141   inline
   142   std::istream& operator>>(std::istream &is, xy<T> &z)
   143   {
   144 
   145     is >> z.x >> z.y;
   146     return is;
   147   }
   148 
   149   ///Write a plainvector to a stream
   150 
   151   ///\relates xy
   152   ///
   153   template<typename T>
   154   inline
   155   std::ostream& operator<<(std::ostream &os, xy<T> z)
   156   {
   157     os << "(" << z.x << ", " << z.y << ")";
   158     return os;
   159   }
   160 
   161 
   162   /// A class to calculate or store the bounding box of plainvectors.
   163 
   164   /// A class to calculate or store the bounding box of plainvectors.
   165   ///
   166   ///\author Attila Bernath
   167   template<typename T>
   168     class BoundingBox {
   169       xy<T> bottom_left, top_right;
   170       bool _empty;
   171     public:
   172       
   173       ///Default constructor: an empty bounding box
   174       BoundingBox() { _empty = true; }
   175 
   176       ///Constructing the instance from one point
   177       BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
   178 
   179       ///Is there any point added
   180       bool empty() const {
   181 	return _empty;
   182       }
   183 
   184       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
   185       xy<T> bottomLeft() const {
   186 	return bottom_left;
   187       };
   188 
   189       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
   190       xy<T> topRight() const {
   191 	return top_right;
   192       };
   193 
   194       ///Checks whether a point is inside a bounding box
   195       bool inside(const xy<T>& u){
   196 	if (_empty)
   197 	  return false;
   198 	else{
   199 	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
   200 		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
   201 	}
   202       }
   203   
   204       ///Increments a bounding box with a point
   205       BoundingBox& operator +=(const xy<T>& u){
   206 	if (_empty){
   207 	  bottom_left=top_right=u;
   208 	  _empty = false;
   209 	}
   210 	else{
   211 	  if (bottom_left.x > u.x) bottom_left.x = u.x;
   212 	  if (bottom_left.y > u.y) bottom_left.y = u.y;
   213 	  if (top_right.x < u.x) top_right.x = u.x;
   214 	  if (top_right.y < u.y) top_right.y = u.y;
   215 	}
   216 	return *this;
   217       };
   218   
   219       ///Sums a bounding box and a point
   220       BoundingBox operator +(const xy<T>& u){
   221 	BoundingBox b = *this;
   222 	return b += u;
   223       };
   224 
   225       ///Increments a bounding box with an other bounding box
   226       BoundingBox& operator +=(const BoundingBox &u){
   227 	if ( !u.empty() ){
   228 	  *this += u.bottomLeft();
   229 	  *this += u.topRight();
   230 	}
   231 	return *this;
   232       };
   233   
   234       ///Sums two bounding boxes
   235       BoundingBox operator +(const BoundingBox& u){
   236 	BoundingBox b = *this;
   237 	return b += u;
   238       };
   239 
   240     };//class Boundingbox
   241 
   242 
   243   /// @}
   244 
   245 
   246 } //namespace lemon
   247 
   248 #endif //LEMON_XY_H