COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/xy.h @ 1157:3996d2098090

Last change on this file since 1157:3996d2098090 was 1102:100c8d5ee36b, checked in by Alpar Juttner, 19 years ago

Functions to query height/width of a BoundingBox?.

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