COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/xy.h @ 1081:c0ad2673b11f

Last change on this file since 1081:c0ad2673b11f was 1071:7c70fc1b2d8b, checked in by Alpar Juttner, 19 years ago

Missing 'xy<T> operator*(double, xy<T>)' added.

File size: 7.0 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
[987]53      typedef T Value;
[964]54
[240]55      T x,y;     
[207]56     
57      ///Default constructor: both coordinates become 0
[240]58      xy() : x(0), y(0) {}
[201]59
[240]60      ///Constructing the instance from coordinates
[514]61      xy(T a, T b) : x(a), y(b) { }
[201]62
63
[1049]64      ///Conversion constructor
65      template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
66
[207]67      ///Gives back the square of the norm of the vector
68      T normSquare(){
[240]69        return x*x+y*y;
[207]70      };
[201]71 
[207]72      ///Increments the left hand side by u
73      xy<T>& operator +=(const xy<T>& u){
[240]74        x += u.x;
75        y += u.y;
[207]76        return *this;
77      };
[201]78 
[207]79      ///Decrements the left hand side by u
80      xy<T>& operator -=(const xy<T>& u){
[240]81        x -= u.x;
82        y -= u.y;
[207]83        return *this;
84      };
[201]85
[207]86      ///Multiplying the left hand side with a scalar
87      xy<T>& operator *=(const T &u){
[240]88        x *= u;
89        y *= u;
[207]90        return *this;
91      };
92
93      ///Dividing the left hand side by a scalar
94      xy<T>& operator /=(const T &u){
[240]95        x /= u;
96        y /= u;
[207]97        return *this;
98      };
[201]99 
[207]100      ///Returns the scalar product of two vectors
101      T operator *(const xy<T>& u){
[240]102        return x*u.x+y*u.y;
[207]103      };
[201]104 
[207]105      ///Returns the sum of two vectors
106      xy<T> operator+(const xy<T> &u) const {
107        xy<T> b=*this;
108        return b+=u;
109      };
[201]110
[1049]111      ///Returns the neg of the vectors
112      xy<T> operator-() const {
113        xy<T> b=*this;
114        b.x=-b.x; b.y=-b.y;
115        return b;
116      };
117
[207]118      ///Returns the difference of two vectors
119      xy<T> operator-(const xy<T> &u) const {
120        xy<T> b=*this;
121        return b-=u;
122      };
[201]123
[207]124      ///Returns a vector multiplied by a scalar
125      xy<T> operator*(const T &u) const {
126        xy<T> b=*this;
127        return b*=u;
128      };
[201]129
[207]130      ///Returns a vector divided by a scalar
131      xy<T> operator/(const T &u) const {
132        xy<T> b=*this;
133        return b/=u;
134      };
[201]135
[207]136      ///Testing equality
137      bool operator==(const xy<T> &u){
[240]138        return (x==u.x) && (y==u.y);
[207]139      };
[201]140
[207]141      ///Testing inequality
142      bool operator!=(xy u){
[240]143        return  (x!=u.x) || (y!=u.y);
[207]144      };
[201]145
[207]146    };
[201]147
[1071]148  ///Returns a vector multiplied by a scalar
149  template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
150    return x*u;
151  };
152
[814]153  ///Read a plainvector from a stream
154
[967]155  ///Read a plainvector from a stream
[814]156  ///\relates xy
157  ///
[207]158  template<typename T>
159  inline
160  std::istream& operator>>(std::istream &is, xy<T> &z)
161  {
[240]162
163    is >> z.x >> z.y;
[207]164    return is;
165  }
[201]166
[814]167  ///Write a plainvector to a stream
168
[967]169  ///Write a plainvector to a stream
[814]170  ///\relates xy
171  ///
[207]172  template<typename T>
173  inline
174  std::ostream& operator<<(std::ostream &os, xy<T> z)
175  {
[240]176    os << "(" << z.x << ", " << z.y << ")";
[207]177    return os;
178  }
179
[244]180
[458]181  /// A class to calculate or store the bounding box of plainvectors.
182
183  /// A class to calculate or store the bounding box of plainvectors.
184  ///
185  ///\author Attila Bernath
[244]186  template<typename T>
187    class BoundingBox {
188      xy<T> bottom_left, top_right;
189      bool _empty;
190    public:
191     
192      ///Default constructor: an empty bounding box
193      BoundingBox() { _empty = true; }
194
195      ///Constructing the instance from one point
196      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
197
198      ///Is there any point added
199      bool empty() const {
200        return _empty;
201      }
202
203      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
204      xy<T> bottomLeft() const {
205        return bottom_left;
206      };
207
208      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
209      xy<T> topRight() const {
210        return top_right;
211      };
212
[1045]213      ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined)
214      xy<T> bottomRight() const {
215        return xy<T>(top_right.x,bottom_left.y);
216      };
217
218      ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined)
219      xy<T> topLeft() const {
220        return xy<T>(bottom_left.x,top_right.y);
221      };
222
223      ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined)
224      T bottom() const {
225        return bottom_left.y;
226      };
227
228      ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined)
229      T top() const {
230        return top_right.y;
231      };
232
233      ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined)
234      T left() const {
235        return bottom_left.x;
236      };
237
238      ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined)
239      T right() const {
240        return top_right.x;
241      };
242
[244]243      ///Checks whether a point is inside a bounding box
244      bool inside(const xy<T>& u){
245        if (_empty)
246          return false;
247        else{
248          return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
249                  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
250        }
251      }
252 
253      ///Increments a bounding box with a point
254      BoundingBox& operator +=(const xy<T>& u){
255        if (_empty){
256          bottom_left=top_right=u;
257          _empty = false;
258        }
259        else{
260          if (bottom_left.x > u.x) bottom_left.x = u.x;
261          if (bottom_left.y > u.y) bottom_left.y = u.y;
262          if (top_right.x < u.x) top_right.x = u.x;
263          if (top_right.y < u.y) top_right.y = u.y;
264        }
265        return *this;
266      };
267 
268      ///Sums a bounding box and a point
269      BoundingBox operator +(const xy<T>& u){
270        BoundingBox b = *this;
271        return b += u;
272      };
273
274      ///Increments a bounding box with an other bounding box
275      BoundingBox& operator +=(const BoundingBox &u){
276        if ( !u.empty() ){
277          *this += u.bottomLeft();
278          *this += u.topRight();
279        }
280        return *this;
281      };
282 
283      ///Sums two bounding boxes
284      BoundingBox operator +(const BoundingBox& u){
285        BoundingBox b = *this;
286        return b += u;
287      };
288
289    };//class Boundingbox
290
291
[431]292  /// @}
[244]293
294
[921]295} //namespace lemon
[201]296
[921]297#endif //LEMON_XY_H
Note: See TracBrowser for help on using the repository browser.