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
163 std::istream& operator>>(std::istream &is, xy<T> &z)
170 ///Write a plainvector to a stream
172 ///Write a plainvector to a stream
177 std::ostream& operator<<(std::ostream &os, xy<T> z)
179 os << "(" << z.x << ", " << z.y << ")";
183 ///Rotate by 90 degrees
185 ///Returns its parameter rotated by 90 degrees in positive direction.
189 inline xy<T> rot90(const xy<T> &z)
191 return xy<T>(-z.y,z.x);
194 ///Rotate by 270 degrees
196 ///Returns its parameter rotated by 90 degrees in negative direction.
200 inline xy<T> rot270(const xy<T> &z)
202 return xy<T>(z.y,-z.x);
207 /// A class to calculate or store the bounding box of plainvectors.
209 /// A class to calculate or store the bounding box of plainvectors.
211 ///\author Attila Bernath
214 xy<T> bottom_left, top_right;
218 ///Default constructor: an empty bounding box
219 BoundingBox() { _empty = true; }
221 ///Constructing the instance from one point
222 BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
224 ///Is there any point added
229 ///Makes the BoundingBox empty
234 ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
235 xy<T> bottomLeft() const {
239 ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
240 xy<T> topRight() const {
244 ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined)
245 xy<T> bottomRight() const {
246 return xy<T>(top_right.x,bottom_left.y);
249 ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined)
250 xy<T> topLeft() const {
251 return xy<T>(bottom_left.x,top_right.y);
254 ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined)
256 return bottom_left.y;
259 ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined)
264 ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined)
266 return bottom_left.x;
269 ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined)
274 ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined)
276 return top_right.y-bottom_left.y;
279 ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined)
281 return top_right.x-bottom_left.x;
284 ///Checks whether a point is inside a bounding box
285 bool inside(const xy<T>& u){
289 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
290 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
294 ///Increments a bounding box with a point
295 BoundingBox& operator +=(const xy<T>& u){
297 bottom_left=top_right=u;
301 if (bottom_left.x > u.x) bottom_left.x = u.x;
302 if (bottom_left.y > u.y) bottom_left.y = u.y;
303 if (top_right.x < u.x) top_right.x = u.x;
304 if (top_right.y < u.y) top_right.y = u.y;
309 ///Sums a bounding box and a point
310 BoundingBox operator +(const xy<T>& u){
311 BoundingBox b = *this;
315 ///Increments a bounding box with an other bounding box
316 BoundingBox& operator +=(const BoundingBox &u){
318 *this += u.bottomLeft();
319 *this += u.topRight();
324 ///Sums two bounding boxes
325 BoundingBox operator +(const BoundingBox& u){
326 BoundingBox b = *this;
330 };//class Boundingbox
333 ///Map of x-coordinates of an xy<>-map
342 typedef typename M::Value::Value Value;
343 typedef typename M::Key Key;
345 XMap(M &map) : _map(map) {}
346 Value operator[](Key k) const {return _map[k].x;}
347 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
350 ///Returns an \ref XMap class
352 ///This function just returns an \ref XMap class.
357 inline XMap<M> xMap(M &m)
362 ///Constant (read only) version of \ref XMap
371 typedef typename M::Value::Value Value;
372 typedef typename M::Key Key;
374 ConstXMap(const M &map) : _map(map) {}
375 Value operator[](Key k) const {return _map[k].x;}
378 ///Returns a \ref ConstXMap class
380 ///This function just returns an \ref ConstXMap class.
383 ///\relates ConstXMap
385 inline ConstXMap<M> xMap(const M &m)
387 return ConstXMap<M>(m);
390 ///Map of y-coordinates of an xy<>-map
399 typedef typename M::Value::Value Value;
400 typedef typename M::Key Key;
402 YMap(M &map) : _map(map) {}
403 Value operator[](Key k) const {return _map[k].y;}
404 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
407 ///Returns an \ref YMap class
409 ///This function just returns an \ref YMap class.
414 inline YMap<M> yMap(M &m)
419 ///Constant (read only) version of \ref YMap
428 typedef typename M::Value::Value Value;
429 typedef typename M::Key Key;
431 ConstYMap(const M &map) : _map(map) {}
432 Value operator[](Key k) const {return _map[k].y;}
435 ///Returns a \ref ConstYMap class
437 ///This function just returns an \ref ConstYMap class.
440 ///\relates ConstYMap
442 inline ConstYMap<M> yMap(const M &m)
444 return ConstYMap<M>(m);
448 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
450 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
458 typedef typename M::Value::Value Value;
459 typedef typename M::Key Key;
461 NormSquareMap(const M &map) : _map(map) {}
462 Value operator[](Key k) const {return _map[k].normSquare();}
465 ///Returns a \ref NormSquareMap class
467 ///This function just returns an \ref NormSquareMap class.
470 ///\relates NormSquareMap
472 inline NormSquareMap<M> normSquareMap(const M &m)
474 return NormSquareMap<M>(m);