/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
///\brief A simple two dimensional vector and a bounding box implementation
/// The class \ref lemon::dim2::Point "dim2::Point" implements
/// a two dimensional vector with the usual operations.
/// The class \ref lemon::dim2::Box "dim2::Box" can be used to determine
/// the rectangular bounding box of a set of
/// \ref lemon::dim2::Point "dim2::Point"'s.
///Tools for handling two dimensional coordinates
///This namespace is a storage of several
///tools for handling two dimensional coordinates
/// Two dimensional vector (plain vector)
/// A simple two dimensional vector (plain vector) implementation
/// with the usual vector operations.
///Construct an instance from coordinates
Point(T a, T b) : x(a), y(b) { }
///Returns the dimension of the vector (i.e. returns 2).
///The dimension of the vector.
///This function always returns 2.
int size() const { return 2; }
///\c p[0] is \c p.x and \c p[1] is \c p.y
T& operator[](int idx) { return idx == 0 ? x : y; }
///Const subscripting operator
///\c p[0] is \c p.x and \c p[1] is \c p.y
const T& operator[](int idx) const { return idx == 0 ? x : y; }
///Conversion constructor
template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
///Give back the square of the norm of the vector
///Increment the left hand side by \c u
Point<T>& operator +=(const Point<T>& u) {
///Decrement the left hand side by \c u
Point<T>& operator -=(const Point<T>& u) {
///Multiply the left hand side with a scalar
Point<T>& operator *=(const T &u) {
///Divide the left hand side by a scalar
Point<T>& operator /=(const T &u) {
///Return the scalar product of two vectors
T operator *(const Point<T>& u) const {
///Return the sum of two vectors
Point<T> operator+(const Point<T> &u) const {
///Return the negative of the vector
Point<T> operator-() const {
///Return the difference of two vectors
Point<T> operator-(const Point<T> &u) const {
///Return a vector multiplied by a scalar
Point<T> operator*(const T &u) const {
///Return a vector divided by a scalar
Point<T> operator/(const T &u) const {
bool operator==(const Point<T> &u) const {
return (x==u.x) && (y==u.y);
bool operator!=(Point u) const {
return (x!=u.x) || (y!=u.y);
inline Point<T> makePoint(const T& x, const T& y) {
///Return a vector multiplied by a scalar
///Return a vector multiplied by a scalar.
template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
///Read a plain vector from a stream
///Read a plain vector from a stream.
inline std::istream& operator>>(std::istream &is, Point<T> &z) {
if (c != '(') is.putback(c);
if (!(is >> z.x)) return is;
if (c != ',') is.putback(c);
if (!(is >> z.y)) return is;
if (c != ')') is.putback(c);
///Write a plain vector to a stream
///Write a plain vector to a stream.
inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
os << "(" << z.x << "," << z.y << ")";
///Returns the parameter rotated by 90 degrees in positive direction.
inline Point<T> rot90(const Point<T> &z)
return Point<T>(-z.y,z.x);
///Returns the parameter rotated by 180 degrees.
inline Point<T> rot180(const Point<T> &z)
return Point<T>(-z.x,-z.y);
///Returns the parameter rotated by 90 degrees in negative direction.
inline Point<T> rot270(const Point<T> &z)
return Point<T>(z.y,-z.x);
/// Bounding box of plain vectors (\ref Point points).
/// A class to calculate or store the bounding box of plain vectors
Point<T> _bottom_left, _top_right;
///Default constructor: creates an empty box
///Construct a box from one point
_bottom_left = _top_right = a;
///Construct a box from two points
///Construct a box from two points.
///\param a The bottom left corner.
///\param b The top right corner.
///\warning The coordinates of the bottom left corner must be no more
///than those of the top right one.
Box(Point<T> a,Point<T> b)
///Construct a box from four numbers
///Construct a box from four numbers.
///\param l The left side of the box.
///\param b The bottom of the box.
///\param r The right side of the box.
///\param t The top of the box.
///\warning The left side must be no more than the right side and
///bottom must be no more than the top.
_bottom_left=Point<T>(l,b);
_top_right=Point<T>(r,t);
///Return \c true if the box is empty.
///Return \c true if the box is empty (i.e. return \c false
///if at least one point was added to the box or the coordinates of
///The coordinates of an empty box are not defined.
///Give back the bottom left corner of the box
///Give back the bottom left corner of the box.
///If the box is empty, then the return value is not defined.
Point<T> bottomLeft() const {
///Set the bottom left corner of the box
///Set the bottom left corner of the box.
///\pre The box must not be empty.
void bottomLeft(Point<T> p) {
///Give back the top right corner of the box
///Give back the top right corner of the box.
///If the box is empty, then the return value is not defined.
Point<T> topRight() const {
///Set the top right corner of the box
///Set the top right corner of the box.
///\pre The box must not be empty.
void topRight(Point<T> p) {
///Give back the bottom right corner of the box
///Give back the bottom right corner of the box.
///If the box is empty, then the return value is not defined.
Point<T> bottomRight() const {
return Point<T>(_top_right.x,_bottom_left.y);
///Set the bottom right corner of the box
///Set the bottom right corner of the box.
///\pre The box must not be empty.
void bottomRight(Point<T> p) {
///Give back the top left corner of the box
///Give back the top left corner of the box.
///If the box is empty, then the return value is not defined.
Point<T> topLeft() const {
return Point<T>(_bottom_left.x,_top_right.y);
///Set the top left corner of the box
///Set the top left corner of the box.
///\pre The box must not be empty.
void topLeft(Point<T> p) {
///Give back the bottom of the box
///Give back the bottom of the box.
///If the box is empty, then the return value is not defined.
///Set the bottom of the box
///Set the bottom of the box.
///\pre The box must not be empty.
///Give back the top of the box
///Give back the top of the box.
///If the box is empty, then the return value is not defined.
///Set the top of the box
///Set the top of the box.
///\pre The box must not be empty.
///Give back the left side of the box
///Give back the left side of the box.
///If the box is empty, then the return value is not defined.
///Set the left side of the box
///Set the left side of the box.
///\pre The box must not be empty.
/// Give back the right side of the box
/// Give back the right side of the box.
///If the box is empty, then the return value is not defined.
///Set the right side of the box
///Set the right side of the box.
///\pre The box must not be empty.
///Give back the height of the box
///Give back the height of the box.
///If the box is empty, then the return value is not defined.
return _top_right.y-_bottom_left.y;
///Give back the width of the box
///Give back the width of the box.
///If the box is empty, then the return value is not defined.
return _top_right.x-_bottom_left.x;
///Checks whether a point is inside the box
bool inside(const Point<T>& u) const {
return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
(u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
///Increments the box with a point
///Increments the box with a point.
Box& add(const Point<T>& u){
_bottom_left = _top_right = u;
if (_bottom_left.x > u.x) _bottom_left.x = u.x;
if (_bottom_left.y > u.y) _bottom_left.y = u.y;
if (_top_right.x < u.x) _top_right.x = u.x;
if (_top_right.y < u.y) _top_right.y = u.y;
///Increments the box to contain another box
///Increments the box to contain another box.
///Intersection of two boxes
///Intersection of two boxes.
Box operator&(const Box& u) const {
if (_empty || u._empty) {
b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
b._top_right.x = std::min(_top_right.x, u._top_right.x);
b._top_right.y = std::min(_top_right.y, u._top_right.y);
b._empty = b._bottom_left.x > b._top_right.x ||
b._bottom_left.y > b._top_right.y;
///Read a box from a stream
///Read a box from a stream.
inline std::istream& operator>>(std::istream &is, Box<T>& b) {
if (c != '(') is.putback(c);
if (!(is >> p)) return is;
if (c != ',') is.putback(c);
if (!(is >> p)) return is;
if (c != ')') is.putback(c);
///Write a box to a stream
///Write a box to a stream.
inline std::ostream& operator<<(std::ostream &os, const Box<T>& b)
os << "(" << b.bottomLeft() << "," << b.topRight() << ")";
///Map of x-coordinates of a \ref Point "Point"-map
///Map of x-coordinates of a \ref Point "Point"-map.
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
XMap(M& map) : _map(map) {}
Value operator[](Key k) const {return _map[k].x;}
void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
///Returns an \ref XMap class
///This function just returns an \ref XMap class.
inline XMap<M> xMap(M &m)
inline XMap<M> xMap(const M &m)
///Constant (read only) version of \ref XMap
///Constant (read only) version of \ref XMap
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
ConstXMap(const M &map) : _map(map) {}
Value operator[](Key k) const {return _map[k].x;}
///Returns a \ref ConstXMap class
///This function just returns a \ref ConstXMap class.
inline ConstXMap<M> xMap(const M &m)
///Map of y-coordinates of a \ref Point "Point"-map
///Map of y-coordinates of a \ref Point "Point"-map.
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
YMap(M& map) : _map(map) {}
Value operator[](Key k) const {return _map[k].y;}
void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
///Returns a \ref YMap class
///This function just returns a \ref YMap class.
inline YMap<M> yMap(M &m)
inline YMap<M> yMap(const M &m)
///Constant (read only) version of \ref YMap
///Constant (read only) version of \ref YMap
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
ConstYMap(const M &map) : _map(map) {}
Value operator[](Key k) const {return _map[k].y;}
///Returns a \ref ConstYMap class
///This function just returns a \ref ConstYMap class.
inline ConstYMap<M> yMap(const M &m)
///\brief Map of the \ref Point::normSquare() "normSquare()"
///of a \ref Point "Point"-map
///Map of the \ref Point::normSquare() "normSquare()"
///of a \ref Point "Point"-map.
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
NormSquareMap(const M &map) : _map(map) {}
Value operator[](Key k) const {return _map[k].normSquare();}
///Returns a \ref NormSquareMap class
///This function just returns a \ref NormSquareMap class.
///\relates NormSquareMap
inline NormSquareMap<M> normSquareMap(const M &m)
return NormSquareMap<M>(m);