COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/include/xy.h @ 435:8f1dece01cc4

Last change on this file since 435:8f1dece01cc4 was 432:a51ba0e51a3a, checked in by Alpar Juttner, 20 years ago

xy.h went to src/include.

File size: 4.6 KB
Line 
1// -*- c++ -*-
2#ifndef HUGO_XY_H
3#define HUGO_XY_H
4
5#include <iostream>
6
7///ingroup misc
8///\file
9///\brief A simple two dimensional vector and a bounding box implementation
10///
11/// The class \ref hugo::xy "xy" implements
12///a two dimensional vector with the usual
13/// operations.
14///
15/// The class \ref hugo::BoundingBox "BoundingBox" can be used to determine
16/// the rectangular bounding box a set of \ref hugo::xy "xy"'s.
17
18
19namespace hugo {
20
21  /// \addtogroup misc
22  /// @{
23
24/** \brief
252 dimensional vector (plainvector) implementation
26
27*/
28  template<typename T>
29    class xy {
30
31    public:
32
33      T x,y;     
34     
35      ///Default constructor: both coordinates become 0
36      xy() : x(0), y(0) {}
37
38      ///Constructing the instance from coordinates
39      xy(T a, T b) : x(a), y(a) { }
40
41
42      ///Gives back the square of the norm of the vector
43      T normSquare(){
44        return x*x+y*y;
45      };
46 
47      ///Increments the left hand side by u
48      xy<T>& operator +=(const xy<T>& u){
49        x += u.x;
50        y += u.y;
51        return *this;
52      };
53 
54      ///Decrements the left hand side by u
55      xy<T>& operator -=(const xy<T>& u){
56        x -= u.x;
57        y -= u.y;
58        return *this;
59      };
60
61      ///Multiplying the left hand side with a scalar
62      xy<T>& operator *=(const T &u){
63        x *= u;
64        y *= u;
65        return *this;
66      };
67
68      ///Dividing the left hand side by a scalar
69      xy<T>& operator /=(const T &u){
70        x /= u;
71        y /= u;
72        return *this;
73      };
74 
75      ///Returns the scalar product of two vectors
76      T operator *(const xy<T>& u){
77        return x*u.x+y*u.y;
78      };
79 
80      ///Returns the sum of two vectors
81      xy<T> operator+(const xy<T> &u) const {
82        xy<T> b=*this;
83        return b+=u;
84      };
85
86      ///Returns the difference of two vectors
87      xy<T> operator-(const xy<T> &u) const {
88        xy<T> b=*this;
89        return b-=u;
90      };
91
92      ///Returns a vector multiplied by a scalar
93      xy<T> operator*(const T &u) const {
94        xy<T> b=*this;
95        return b*=u;
96      };
97
98      ///Returns a vector divided by a scalar
99      xy<T> operator/(const T &u) const {
100        xy<T> b=*this;
101        return b/=u;
102      };
103
104      ///Testing equality
105      bool operator==(const xy<T> &u){
106        return (x==u.x) && (y==u.y);
107      };
108
109      ///Testing inequality
110      bool operator!=(xy u){
111        return  (x!=u.x) || (y!=u.y);
112      };
113
114    };
115
116  ///Reading a plainvector from a stream
117  template<typename T>
118  inline
119  std::istream& operator>>(std::istream &is, xy<T> &z)
120  {
121
122    is >> z.x >> z.y;
123    return is;
124  }
125
126  ///Outputting a plainvector to a stream
127  template<typename T>
128  inline
129  std::ostream& operator<<(std::ostream &os, xy<T> z)
130  {
131    os << "(" << z.x << ", " << z.y << ")";
132    return os;
133  }
134
135
136  /** \brief
137     Implementation of a bounding box of plainvectors.
138     
139  */
140  template<typename T>
141    class BoundingBox {
142      xy<T> bottom_left, top_right;
143      bool _empty;
144    public:
145     
146      ///Default constructor: an empty bounding box
147      BoundingBox() { _empty = true; }
148
149      ///Constructing the instance from one point
150      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
151
152      ///Is there any point added
153      bool empty() const {
154        return _empty;
155      }
156
157      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
158      xy<T> bottomLeft() const {
159        return bottom_left;
160      };
161
162      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
163      xy<T> topRight() const {
164        return top_right;
165      };
166
167      ///Checks whether a point is inside a bounding box
168      bool inside(const xy<T>& u){
169        if (_empty)
170          return false;
171        else{
172          return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
173                  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
174        }
175      }
176 
177      ///Increments a bounding box with a point
178      BoundingBox& operator +=(const xy<T>& u){
179        if (_empty){
180          bottom_left=top_right=u;
181          _empty = false;
182        }
183        else{
184          if (bottom_left.x > u.x) bottom_left.x = u.x;
185          if (bottom_left.y > u.y) bottom_left.y = u.y;
186          if (top_right.x < u.x) top_right.x = u.x;
187          if (top_right.y < u.y) top_right.y = u.y;
188        }
189        return *this;
190      };
191 
192      ///Sums a bounding box and a point
193      BoundingBox operator +(const xy<T>& u){
194        BoundingBox b = *this;
195        return b += u;
196      };
197
198      ///Increments a bounding box with an other bounding box
199      BoundingBox& operator +=(const BoundingBox &u){
200        if ( !u.empty() ){
201          *this += u.bottomLeft();
202          *this += u.topRight();
203        }
204        return *this;
205      };
206 
207      ///Sums two bounding boxes
208      BoundingBox operator +(const BoundingBox& u){
209        BoundingBox b = *this;
210        return b += u;
211      };
212
213    };//class Boundingbox
214
215
216  /// @}
217
218
219} //namespace hugo
220
221#endif //HUGO_XY_H
Note: See TracBrowser for help on using the repository browser.