COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/xy/xy.h @ 393:4535f78639e2

Last change on this file since 393:4535f78639e2 was 249:0b0bdf24d00c, checked in by Alpar Juttner, 21 years ago

DocFix?

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