2 * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, 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 two dimensional vector (plainvector) implementation
43 /// A two dimensional vector (plainvector) implementation
44 ///with the usual vector
47 ///\author Attila Bernath
57 ///Default constructor: both coordinates become 0
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
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){
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){
138 return (x==u.x) && (y==u.y);
141 ///Testing inequality
142 bool operator!=(xy u){
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 << ")";
184 /// A class to calculate or store the bounding box of plainvectors.
186 /// A class to calculate or store the bounding box of plainvectors.
188 ///\author Attila Bernath
191 xy<T> bottom_left, top_right;
195 ///Default constructor: an empty bounding box
196 BoundingBox() { _empty = true; }
198 ///Constructing the instance from one point
199 BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
201 ///Is there any point added
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 {
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 {
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);
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);
226 ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined)
228 return bottom_left.y;
231 ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined)
236 ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined)
238 return bottom_left.x;
241 ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined)
246 ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined)
248 return top_right.y-bottom_left.y;
251 ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined)
253 return top_right.x-bottom_left.x;
256 ///Checks whether a point is inside a bounding box
257 bool inside(const xy<T>& u){
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 );
266 ///Increments a bounding box with a point
267 BoundingBox& operator +=(const xy<T>& u){
269 bottom_left=top_right=u;
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;
281 ///Sums a bounding box and a point
282 BoundingBox operator +(const xy<T>& u){
283 BoundingBox b = *this;
287 ///Increments a bounding box with an other bounding box
288 BoundingBox& operator +=(const BoundingBox &u){
290 *this += u.bottomLeft();
291 *this += u.topRight();
296 ///Sums two bounding boxes
297 BoundingBox operator +(const BoundingBox& u){
298 BoundingBox b = *this;
302 };//class Boundingbox