COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/xy.h @ 924:ccb657556d84

Last change on this file since 924:ccb657556d84 was 921:818510fa3d99, checked in by Alpar Juttner, 20 years ago

hugo -> lemon

File size: 5.5 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
[906]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
[921]17#ifndef LEMON_XY_H
18#define LEMON_XY_H
[201]19
20#include <iostream>
21
[491]22///\ingroup misc
[249]23///\file
24///\brief A simple two dimensional vector and a bounding box implementation
25///
[921]26/// The class \ref lemon::xy "xy" implements
[249]27///a two dimensional vector with the usual
28/// operations.
29///
[921]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.
[458]32///
33///\author Attila Bernath
[249]34
35
[921]36namespace lemon {
[431]37
38  /// \addtogroup misc
39  /// @{
40
[458]41  /// A two dimensional vector (plainvector) implementation
[242]42
[458]43  /// A two dimensional vector (plainvector) implementation
44  ///with the usual vector
45  /// operators.
46  ///
47  ///\author Attila Bernath
[207]48  template<typename T>
49    class xy {
[201]50
[207]51    public:
[240]52
53      T x,y;     
[207]54     
55      ///Default constructor: both coordinates become 0
[240]56      xy() : x(0), y(0) {}
[201]57
[240]58      ///Constructing the instance from coordinates
[514]59      xy(T a, T b) : x(a), y(b) { }
[201]60
61
[207]62      ///Gives back the square of the norm of the vector
63      T normSquare(){
[240]64        return x*x+y*y;
[207]65      };
[201]66 
[207]67      ///Increments the left hand side by u
68      xy<T>& operator +=(const xy<T>& u){
[240]69        x += u.x;
70        y += u.y;
[207]71        return *this;
72      };
[201]73 
[207]74      ///Decrements the left hand side by u
75      xy<T>& operator -=(const xy<T>& u){
[240]76        x -= u.x;
77        y -= u.y;
[207]78        return *this;
79      };
[201]80
[207]81      ///Multiplying the left hand side with a scalar
82      xy<T>& operator *=(const T &u){
[240]83        x *= u;
84        y *= u;
[207]85        return *this;
86      };
87
88      ///Dividing the left hand side by a scalar
89      xy<T>& operator /=(const T &u){
[240]90        x /= u;
91        y /= u;
[207]92        return *this;
93      };
[201]94 
[207]95      ///Returns the scalar product of two vectors
96      T operator *(const xy<T>& u){
[240]97        return x*u.x+y*u.y;
[207]98      };
[201]99 
[207]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      };
[201]105
[207]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      };
[201]111
[207]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      };
[201]117
[207]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      };
[201]123
[207]124      ///Testing equality
125      bool operator==(const xy<T> &u){
[240]126        return (x==u.x) && (y==u.y);
[207]127      };
[201]128
[207]129      ///Testing inequality
130      bool operator!=(xy u){
[240]131        return  (x!=u.x) || (y!=u.y);
[207]132      };
[201]133
[207]134    };
[201]135
[814]136  ///Read a plainvector from a stream
137
138  ///\relates xy
139  ///
[207]140  template<typename T>
141  inline
142  std::istream& operator>>(std::istream &is, xy<T> &z)
143  {
[240]144
145    is >> z.x >> z.y;
[207]146    return is;
147  }
[201]148
[814]149  ///Write a plainvector to a stream
150
151  ///\relates xy
152  ///
[207]153  template<typename T>
154  inline
155  std::ostream& operator<<(std::ostream &os, xy<T> z)
156  {
[240]157    os << "(" << z.x << ", " << z.y << ")";
[207]158    return os;
159  }
160
[244]161
[458]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
[244]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
[431]243  /// @}
[244]244
245
[921]246} //namespace lemon
[201]247
[921]248#endif //LEMON_XY_H
Note: See TracBrowser for help on using the repository browser.