COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/xy.h @ 964:2c0c20e90116

Last change on this file since 964:2c0c20e90116 was 964:2c0c20e90116, checked in by Alpar Juttner, 19 years ago

Doc improvements

File size: 5.5 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
[906]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
[921]17#ifndef LEMON_XY_H
18#define LEMON_XY_H
[201]19
20#include <iostream>
21
[491]22///\ingroup misc
[249]23///\file
24///\brief A simple two dimensional vector and a bounding box implementation
25///
[921]26/// The class \ref lemon::xy "xy" implements
[249]27///a two dimensional vector with the usual
28/// operations.
29///
[921]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.
[458]32///
33///\author Attila Bernath
[249]34
35
[921]36namespace lemon {
[431]37
38  /// \addtogroup misc
39  /// @{
40
[458]41  /// A two dimensional vector (plainvector) implementation
[242]42
[458]43  /// A two dimensional vector (plainvector) implementation
44  ///with the usual vector
45  /// operators.
46  ///
47  ///\author Attila Bernath
[207]48  template<typename T>
49    class xy {
[201]50
[207]51    public:
[240]52
[964]53      typedef T ValueType;
54
[240]55      T x,y;     
[207]56     
57      ///Default constructor: both coordinates become 0
[240]58      xy() : x(0), y(0) {}
[201]59
[240]60      ///Constructing the instance from coordinates
[514]61      xy(T a, T b) : x(a), y(b) { }
[201]62
63
[207]64      ///Gives back the square of the norm of the vector
65      T normSquare(){
[240]66        return x*x+y*y;
[207]67      };
[201]68 
[207]69      ///Increments the left hand side by u
70      xy<T>& operator +=(const xy<T>& u){
[240]71        x += u.x;
72        y += u.y;
[207]73        return *this;
74      };
[201]75 
[207]76      ///Decrements the left hand side by u
77      xy<T>& operator -=(const xy<T>& u){
[240]78        x -= u.x;
79        y -= u.y;
[207]80        return *this;
81      };
[201]82
[207]83      ///Multiplying the left hand side with a scalar
84      xy<T>& operator *=(const T &u){
[240]85        x *= u;
86        y *= u;
[207]87        return *this;
88      };
89
90      ///Dividing the left hand side by a scalar
91      xy<T>& operator /=(const T &u){
[240]92        x /= u;
93        y /= u;
[207]94        return *this;
95      };
[201]96 
[207]97      ///Returns the scalar product of two vectors
98      T operator *(const xy<T>& u){
[240]99        return x*u.x+y*u.y;
[207]100      };
[201]101 
[207]102      ///Returns the sum of two vectors
103      xy<T> operator+(const xy<T> &u) const {
104        xy<T> b=*this;
105        return b+=u;
106      };
[201]107
[207]108      ///Returns the difference of two vectors
109      xy<T> operator-(const xy<T> &u) const {
110        xy<T> b=*this;
111        return b-=u;
112      };
[201]113
[207]114      ///Returns a vector multiplied by a scalar
115      xy<T> operator*(const T &u) const {
116        xy<T> b=*this;
117        return b*=u;
118      };
[201]119
[207]120      ///Returns a vector divided by a scalar
121      xy<T> operator/(const T &u) const {
122        xy<T> b=*this;
123        return b/=u;
124      };
[201]125
[207]126      ///Testing equality
127      bool operator==(const xy<T> &u){
[240]128        return (x==u.x) && (y==u.y);
[207]129      };
[201]130
[207]131      ///Testing inequality
132      bool operator!=(xy u){
[240]133        return  (x!=u.x) || (y!=u.y);
[207]134      };
[201]135
[207]136    };
[201]137
[814]138  ///Read a plainvector from a stream
139
140  ///\relates xy
141  ///
[207]142  template<typename T>
143  inline
144  std::istream& operator>>(std::istream &is, xy<T> &z)
145  {
[240]146
147    is >> z.x >> z.y;
[207]148    return is;
149  }
[201]150
[814]151  ///Write a plainvector to a stream
152
153  ///\relates xy
154  ///
[207]155  template<typename T>
156  inline
157  std::ostream& operator<<(std::ostream &os, xy<T> z)
158  {
[240]159    os << "(" << z.x << ", " << z.y << ")";
[207]160    return os;
161  }
162
[244]163
[458]164  /// A class to calculate or store the bounding box of plainvectors.
165
166  /// A class to calculate or store the bounding box of plainvectors.
167  ///
168  ///\author Attila Bernath
[244]169  template<typename T>
170    class BoundingBox {
171      xy<T> bottom_left, top_right;
172      bool _empty;
173    public:
174     
175      ///Default constructor: an empty bounding box
176      BoundingBox() { _empty = true; }
177
178      ///Constructing the instance from one point
179      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
180
181      ///Is there any point added
182      bool empty() const {
183        return _empty;
184      }
185
186      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
187      xy<T> bottomLeft() const {
188        return bottom_left;
189      };
190
191      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
192      xy<T> topRight() const {
193        return top_right;
194      };
195
196      ///Checks whether a point is inside a bounding box
197      bool inside(const xy<T>& u){
198        if (_empty)
199          return false;
200        else{
201          return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
202                  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
203        }
204      }
205 
206      ///Increments a bounding box with a point
207      BoundingBox& operator +=(const xy<T>& u){
208        if (_empty){
209          bottom_left=top_right=u;
210          _empty = false;
211        }
212        else{
213          if (bottom_left.x > u.x) bottom_left.x = u.x;
214          if (bottom_left.y > u.y) bottom_left.y = u.y;
215          if (top_right.x < u.x) top_right.x = u.x;
216          if (top_right.y < u.y) top_right.y = u.y;
217        }
218        return *this;
219      };
220 
221      ///Sums a bounding box and a point
222      BoundingBox operator +(const xy<T>& u){
223        BoundingBox b = *this;
224        return b += u;
225      };
226
227      ///Increments a bounding box with an other bounding box
228      BoundingBox& operator +=(const BoundingBox &u){
229        if ( !u.empty() ){
230          *this += u.bottomLeft();
231          *this += u.topRight();
232        }
233        return *this;
234      };
235 
236      ///Sums two bounding boxes
237      BoundingBox operator +(const BoundingBox& u){
238        BoundingBox b = *this;
239        return b += u;
240      };
241
242    };//class Boundingbox
243
244
[431]245  /// @}
[244]246
247
[921]248} //namespace lemon
[201]249
[921]250#endif //LEMON_XY_H
Note: See TracBrowser for help on using the repository browser.