COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/xy/xy.h @ 284:2d4684f76aac

Last change on this file since 284:2d4684f76aac was 249:0b0bdf24d00c, checked in by Alpar Juttner, 21 years ago

DocFix?

File size: 4.5 KB
RevLine 
[207]1// -*- c++ -*-
[201]2#ifndef HUGO_XY_H
3#define HUGO_XY_H
4
5#include <iostream>
6
[207]7namespace hugo {
[201]8
[249]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
[242]20/** \brief
212 dimensional vector (plainvector) implementation
22
23*/
[207]24  template<typename T>
25    class xy {
[201]26
[207]27    public:
[240]28
29      T x,y;     
[207]30     
31      ///Default constructor: both coordinates become 0
[240]32      xy() : x(0), y(0) {}
[201]33
[240]34      ///Constructing the instance from coordinates
35      xy(T a, T b) : x(a), y(a) { }
[201]36
37
[207]38      ///Gives back the square of the norm of the vector
39      T normSquare(){
[240]40        return x*x+y*y;
[207]41      };
[201]42 
[207]43      ///Increments the left hand side by u
44      xy<T>& operator +=(const xy<T>& u){
[240]45        x += u.x;
46        y += u.y;
[207]47        return *this;
48      };
[201]49 
[207]50      ///Decrements the left hand side by u
51      xy<T>& operator -=(const xy<T>& u){
[240]52        x -= u.x;
53        y -= u.y;
[207]54        return *this;
55      };
[201]56
[207]57      ///Multiplying the left hand side with a scalar
58      xy<T>& operator *=(const T &u){
[240]59        x *= u;
60        y *= u;
[207]61        return *this;
62      };
63
64      ///Dividing the left hand side by a scalar
65      xy<T>& operator /=(const T &u){
[240]66        x /= u;
67        y /= u;
[207]68        return *this;
69      };
[201]70 
[207]71      ///Returns the scalar product of two vectors
72      T operator *(const xy<T>& u){
[240]73        return x*u.x+y*u.y;
[207]74      };
[201]75 
[207]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      };
[201]81
[207]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      };
[201]87
[207]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      };
[201]93
[207]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      };
[201]99
[207]100      ///Testing equality
101      bool operator==(const xy<T> &u){
[240]102        return (x==u.x) && (y==u.y);
[207]103      };
[201]104
[207]105      ///Testing inequality
106      bool operator!=(xy u){
[240]107        return  (x!=u.x) || (y!=u.y);
[207]108      };
[201]109
[207]110    };
[201]111
[207]112  ///Reading a plainvector from a stream
113  template<typename T>
114  inline
115  std::istream& operator>>(std::istream &is, xy<T> &z)
116  {
[240]117
118    is >> z.x >> z.y;
[207]119    return is;
120  }
[201]121
[207]122  ///Outputting a plainvector to a stream
123  template<typename T>
124  inline
125  std::ostream& operator<<(std::ostream &os, xy<T> z)
126  {
[240]127    os << "(" << z.x << ", " << z.y << ")";
[207]128    return os;
129  }
130
[244]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
[207]214} //namespace hugo
[201]215
216#endif //HUGO_XY_H
Note: See TracBrowser for help on using the repository browser.