COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/xy/xy.h @ 248:dcb70198e2a2

Last change on this file since 248:dcb70198e2a2 was 244:0e02be2ca43c, checked in by athos, 21 years ago

Betettem 1 file-ba a boundingbox-ot és az xy-t + egyéb apró módosítások.

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