Modifying the interface.
add -> read, write
2 * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
24 ///\brief A simple two dimensional vector and a bounding box implementation
26 /// The class \ref lemon::xy "xy" implements
27 ///a two dimensional vector with the usual
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.
33 ///\author Attila Bernath
41 /// A simple two dimensional vector (plainvector) implementation
43 /// A simple two dimensional vector (plainvector) implementation
44 ///with the usual vector
47 ///\author Attila Bernath
57 ///Default constructor
60 ///Constructing the instance from coordinates
61 xy(T a, T b) : x(a), y(b) { }
64 ///Conversion constructor
65 template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
67 ///Gives back the square of the norm of the vector
68 T normSquare() const {
72 ///Increments the left hand side by u
73 xy<T>& operator +=(const xy<T>& u) {
79 ///Decrements the left hand side by u
80 xy<T>& operator -=(const xy<T>& u) {
86 ///Multiplying the left hand side with a scalar
87 xy<T>& operator *=(const T &u) {
93 ///Dividing the left hand side by a scalar
94 xy<T>& operator /=(const T &u) {
100 ///Returns the scalar product of two vectors
101 T operator *(const xy<T>& u) const {
105 ///Returns the sum of two vectors
106 xy<T> operator+(const xy<T> &u) const {
111 ///Returns the neg of the vectors
112 xy<T> operator-() const {
118 ///Returns the difference of two vectors
119 xy<T> operator-(const xy<T> &u) const {
124 ///Returns a vector multiplied by a scalar
125 xy<T> operator*(const T &u) const {
130 ///Returns a vector divided by a scalar
131 xy<T> operator/(const T &u) const {
137 bool operator==(const xy<T> &u) const {
138 return (x==u.x) && (y==u.y);
141 ///Testing inequality
142 bool operator!=(xy u) const {
143 return (x!=u.x) || (y!=u.y);
148 ///Returns a vector multiplied by a scalar
150 ///Returns a vector multiplied by a scalar
152 template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
156 ///Read a plainvector from a stream
158 ///Read a plainvector from a stream
162 inline std::istream& operator>>(std::istream &is, xy<T> &z) {
165 if (c != '(') is.putback(c);
169 if (!(is >> z.x)) return is;
171 if (c != ',') is.putback(c);
175 if (!(is >> z.y)) return is;
177 if (c != ')') is.putback(c);
184 ///Write a plainvector to a stream
186 ///Write a plainvector to a stream
190 inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
192 os << "(" << z.x << ", " << z.y << ")";
196 ///Rotate by 90 degrees
198 ///Returns its parameter rotated by 90 degrees in positive direction.
202 inline xy<T> rot90(const xy<T> &z)
204 return xy<T>(-z.y,z.x);
207 ///Rotate by 270 degrees
209 ///Returns its parameter rotated by 90 degrees in negative direction.
213 inline xy<T> rot270(const xy<T> &z)
215 return xy<T>(z.y,-z.x);
220 /// A class to calculate or store the bounding box of plainvectors.
222 /// A class to calculate or store the bounding box of plainvectors.
224 ///\author Attila Bernath
227 xy<T> bottom_left, top_right;
231 ///Default constructor: an empty bounding box
232 BoundingBox() { _empty = true; }
234 ///Constructing the instance from one point
235 BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
237 ///Is there any point added
242 ///Makes the BoundingBox empty
247 ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
248 xy<T> bottomLeft() const {
252 ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
253 xy<T> topRight() const {
257 ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined)
258 xy<T> bottomRight() const {
259 return xy<T>(top_right.x,bottom_left.y);
262 ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined)
263 xy<T> topLeft() const {
264 return xy<T>(bottom_left.x,top_right.y);
267 ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined)
269 return bottom_left.y;
272 ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined)
277 ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined)
279 return bottom_left.x;
282 ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined)
287 ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined)
289 return top_right.y-bottom_left.y;
292 ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined)
294 return top_right.x-bottom_left.x;
297 ///Checks whether a point is inside a bounding box
298 bool inside(const xy<T>& u){
302 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
303 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
307 ///Increments a bounding box with a point
308 BoundingBox& operator +=(const xy<T>& u){
310 bottom_left=top_right=u;
314 if (bottom_left.x > u.x) bottom_left.x = u.x;
315 if (bottom_left.y > u.y) bottom_left.y = u.y;
316 if (top_right.x < u.x) top_right.x = u.x;
317 if (top_right.y < u.y) top_right.y = u.y;
322 ///Sums a bounding box and a point
323 BoundingBox operator +(const xy<T>& u){
324 BoundingBox b = *this;
328 ///Increments a bounding box with an other bounding box
329 BoundingBox& operator +=(const BoundingBox &u){
331 *this += u.bottomLeft();
332 *this += u.topRight();
337 ///Sums two bounding boxes
338 BoundingBox operator +(const BoundingBox& u){
339 BoundingBox b = *this;
343 };//class Boundingbox
346 ///Map of x-coordinates of an xy<>-map
355 typedef typename M::Value::Value Value;
356 typedef typename M::Key Key;
358 XMap(M &map) : _map(map) {}
359 Value operator[](Key k) const {return _map[k].x;}
360 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
363 ///Returns an \ref XMap class
365 ///This function just returns an \ref XMap class.
370 inline XMap<M> xMap(M &m)
375 ///Constant (read only) version of \ref XMap
384 typedef typename M::Value::Value Value;
385 typedef typename M::Key Key;
387 ConstXMap(const M &map) : _map(map) {}
388 Value operator[](Key k) const {return _map[k].x;}
391 ///Returns a \ref ConstXMap class
393 ///This function just returns an \ref ConstXMap class.
396 ///\relates ConstXMap
398 inline ConstXMap<M> xMap(const M &m)
400 return ConstXMap<M>(m);
403 ///Map of y-coordinates of an xy<>-map
412 typedef typename M::Value::Value Value;
413 typedef typename M::Key Key;
415 YMap(M &map) : _map(map) {}
416 Value operator[](Key k) const {return _map[k].y;}
417 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
420 ///Returns an \ref YMap class
422 ///This function just returns an \ref YMap class.
427 inline YMap<M> yMap(M &m)
432 ///Constant (read only) version of \ref YMap
441 typedef typename M::Value::Value Value;
442 typedef typename M::Key Key;
444 ConstYMap(const M &map) : _map(map) {}
445 Value operator[](Key k) const {return _map[k].y;}
448 ///Returns a \ref ConstYMap class
450 ///This function just returns an \ref ConstYMap class.
453 ///\relates ConstYMap
455 inline ConstYMap<M> yMap(const M &m)
457 return ConstYMap<M>(m);
461 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
463 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
471 typedef typename M::Value::Value Value;
472 typedef typename M::Key Key;
474 NormSquareMap(const M &map) : _map(map) {}
475 Value operator[](Key k) const {return _map[k].normSquare();}
478 ///Returns a \ref NormSquareMap class
480 ///This function just returns an \ref NormSquareMap class.
483 ///\relates NormSquareMap
485 inline NormSquareMap<M> normSquareMap(const M &m)
487 return NormSquareMap<M>(m);