Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

xy.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Combinatorial Optimization Research Group, EGRES).
00006  *
00007  * Permission to use, modify and distribute this software is granted
00008  * provided that this copyright notice appears in all copies. For
00009  * precise terms see the accompanying LICENSE file.
00010  *
00011  * This software is provided "AS IS" with no warranty of any kind,
00012  * express or implied, and with no claim as to its suitability for any
00013  * purpose.
00014  *
00015  */
00016 
00017 #ifndef LEMON_XY_H
00018 #define LEMON_XY_H
00019 
00020 #include <iostream>
00021 
00034 
00035 
00036 namespace lemon {
00037 
00040 
00042 
00048   template<typename T>
00049     class xy {
00050 
00051     public:
00052 
00053       typedef T Value;
00054 
00055       T x,y;     
00056       
00058       xy() : x(0), y(0) {}
00059 
00061       xy(T a, T b) : x(a), y(b) { }
00062 
00063 
00065       template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
00066 
00068       T normSquare(){
00069         return x*x+y*y;
00070       };
00071   
00073       xy<T>& operator +=(const xy<T>& u){
00074         x += u.x;
00075         y += u.y;
00076         return *this;
00077       };
00078   
00080       xy<T>& operator -=(const xy<T>& u){
00081         x -= u.x;
00082         y -= u.y;
00083         return *this;
00084       };
00085 
00087       xy<T>& operator *=(const T &u){
00088         x *= u;
00089         y *= u;
00090         return *this;
00091       };
00092 
00094       xy<T>& operator /=(const T &u){
00095         x /= u;
00096         y /= u;
00097         return *this;
00098       };
00099   
00101       T operator *(const xy<T>& u){
00102         return x*u.x+y*u.y;
00103       };
00104   
00106       xy<T> operator+(const xy<T> &u) const {
00107         xy<T> b=*this;
00108         return b+=u;
00109       };
00110 
00112       xy<T> operator-() const {
00113         xy<T> b=*this;
00114         b.x=-b.x; b.y=-b.y;
00115         return b;
00116       };
00117 
00119       xy<T> operator-(const xy<T> &u) const {
00120         xy<T> b=*this;
00121         return b-=u;
00122       };
00123 
00125       xy<T> operator*(const T &u) const {
00126         xy<T> b=*this;
00127         return b*=u;
00128       };
00129 
00131       xy<T> operator/(const T &u) const {
00132         xy<T> b=*this;
00133         return b/=u;
00134       };
00135 
00137       bool operator==(const xy<T> &u){
00138         return (x==u.x) && (y==u.y);
00139       };
00140 
00142       bool operator!=(xy u){
00143         return  (x!=u.x) || (y!=u.y);
00144       };
00145 
00146     };
00147 
00149 
00152   template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
00153     return x*u;
00154   };
00155 
00157 
00161   template<typename T>
00162   inline
00163   std::istream& operator>>(std::istream &is, xy<T> &z)
00164   {
00165 
00166     is >> z.x >> z.y;
00167     return is;
00168   }
00169 
00171 
00175   template<typename T>
00176   inline
00177   std::ostream& operator<<(std::ostream &os, xy<T> z)
00178   {
00179     os << "(" << z.x << ", " << z.y << ")";
00180     return os;
00181   }
00182 
00183 
00185 
00189   template<typename T>
00190     class BoundingBox {
00191       xy<T> bottom_left, top_right;
00192       bool _empty;
00193     public:
00194       
00196       BoundingBox() { _empty = true; }
00197 
00199       BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
00200 
00202       bool empty() const {
00203         return _empty;
00204       }
00205 
00207       xy<T> bottomLeft() const {
00208         return bottom_left;
00209       };
00210 
00212       xy<T> topRight() const {
00213         return top_right;
00214       };
00215 
00217       xy<T> bottomRight() const {
00218         return xy<T>(top_right.x,bottom_left.y);
00219       };
00220 
00222       xy<T> topLeft() const {
00223         return xy<T>(bottom_left.x,top_right.y);
00224       };
00225 
00227       T bottom() const {
00228         return bottom_left.y;
00229       };
00230 
00232       T top() const {
00233         return top_right.y;
00234       };
00235 
00237       T left() const {
00238         return bottom_left.x;
00239       };
00240 
00242       T right() const {
00243         return top_right.x;
00244       };
00245 
00247       T height() const {
00248         return top_right.y-bottom_left.y;
00249       };
00250 
00252       T width() const {
00253         return top_right.x-bottom_left.x;
00254       };
00255 
00257       bool inside(const xy<T>& u){
00258         if (_empty)
00259           return false;
00260         else{
00261           return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
00262                   (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
00263         }
00264       }
00265   
00267       BoundingBox& operator +=(const xy<T>& u){
00268         if (_empty){
00269           bottom_left=top_right=u;
00270           _empty = false;
00271         }
00272         else{
00273           if (bottom_left.x > u.x) bottom_left.x = u.x;
00274           if (bottom_left.y > u.y) bottom_left.y = u.y;
00275           if (top_right.x < u.x) top_right.x = u.x;
00276           if (top_right.y < u.y) top_right.y = u.y;
00277         }
00278         return *this;
00279       };
00280   
00282       BoundingBox operator +(const xy<T>& u){
00283         BoundingBox b = *this;
00284         return b += u;
00285       };
00286 
00288       BoundingBox& operator +=(const BoundingBox &u){
00289         if ( !u.empty() ){
00290           *this += u.bottomLeft();
00291           *this += u.topRight();
00292         }
00293         return *this;
00294       };
00295   
00297       BoundingBox operator +(const BoundingBox& u){
00298         BoundingBox b = *this;
00299         return b += u;
00300       };
00301 
00302     };//class Boundingbox
00303 
00304 
00306 
00307 
00308 } //namespace lemon
00309 
00310 #endif //LEMON_XY_H

Generated on Mon Feb 21 15:02:22 2005 for LEMON by  doxygen 1.4.1