00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
#ifndef LEMON_XY_H
00018
#define LEMON_XY_H
00019
00020
#include <iostream>
00021
00034
00035
00036
namespace lemon {
00037
00040
00042
00048
template<
typename T>
00049 class xy {
00050
00051
public:
00052
00053 T x,y;
00054
00056 xy() : x(0), y(0) {}
00057
00059 xy(T a, T b) : x(a), y(b) { }
00060
00061
00063 T
normSquare(){
00064
return x*x+y*y;
00065 };
00066
00068 xy<T>&
operator +=(
const xy<T>& u){
00069 x += u.
x;
00070 y += u.
y;
00071
return *
this;
00072 };
00073
00075 xy<T>&
operator -=(
const xy<T>& u){
00076 x -= u.
x;
00077 y -= u.
y;
00078
return *
this;
00079 };
00080
00082 xy<T>&
operator *=(
const T &u){
00083 x *= u;
00084 y *= u;
00085
return *
this;
00086 };
00087
00089 xy<T>&
operator /=(
const T &u){
00090 x /= u;
00091 y /= u;
00092
return *
this;
00093 };
00094
00096 T
operator *(
const xy<T>& u){
00097
return x*u.
x+y*u.
y;
00098 };
00099
00101 xy<T> operator+(
const xy<T> &u)
const {
00102
xy<T> b=*
this;
00103
return b+=u;
00104 };
00105
00107 xy<T> operator-(
const xy<T> &u)
const {
00108
xy<T> b=*
this;
00109
return b-=u;
00110 };
00111
00113 xy<T> operator*(
const T &u)
const {
00114
xy<T> b=*
this;
00115
return b*=u;
00116 };
00117
00119 xy<T> operator/(
const T &u)
const {
00120
xy<T> b=*
this;
00121
return b/=u;
00122 };
00123
00125 bool operator==(
const xy<T> &u){
00126
return (x==u.
x) && (y==u.
y);
00127 };
00128
00130 bool operator!=(
xy u){
00131
return (x!=u.
x) || (y!=u.
y);
00132 };
00133
00134 };
00135
00137
00140
template<
typename T>
00141
inline
00142 std::istream& operator>>(std::istream &is,
xy<T> &z)
00143 {
00144
00145 is >> z.
x >> z.
y;
00146
return is;
00147 }
00148
00150
00153
template<
typename T>
00154
inline
00155 std::ostream& operator<<(std::ostream &os, xy<T> z)
00156 {
00157 os <<
"(" << z.x <<
", " << z.y <<
")";
00158
return os;
00159 }
00160
00161
00163
00167
template<
typename T>
00168 class BoundingBox {
00169
xy<T> bottom_left, top_right;
00170
bool _empty;
00171
public:
00172
00174 BoundingBox() { _empty =
true; }
00175
00177 BoundingBox(
xy<T> a) { bottom_left=top_right=a; _empty =
false; }
00178
00180 bool empty()
const {
00181
return _empty;
00182 }
00183
00185 xy<T> bottomLeft()
const {
00186
return bottom_left;
00187 };
00188
00190 xy<T> topRight()
const {
00191
return top_right;
00192 };
00193
00195 bool inside(
const xy<T>& u){
00196
if (_empty)
00197
return false;
00198
else{
00199
return ((u.
x-bottom_left.x)*(top_right.x-u.
x) >= 0 &&
00200 (u.
y-bottom_left.y)*(top_right.y-u.
y) >= 0 );
00201 }
00202 }
00203
00205 BoundingBox&
operator +=(
const xy<T>& u){
00206
if (_empty){
00207 bottom_left=top_right=u;
00208 _empty =
false;
00209 }
00210
else{
00211
if (bottom_left.x > u.
x) bottom_left.x = u.
x;
00212
if (bottom_left.y > u.
y) bottom_left.y = u.
y;
00213
if (top_right.x < u.
x) top_right.x = u.
x;
00214
if (top_right.y < u.
y) top_right.y = u.
y;
00215 }
00216
return *
this;
00217 };
00218
00220 BoundingBox operator +(
const xy<T>& u){
00221
BoundingBox b = *
this;
00222
return b += u;
00223 };
00224
00226 BoundingBox&
operator +=(
const BoundingBox &u){
00227
if ( !u.
empty() ){
00228 *
this += u.
bottomLeft();
00229 *
this += u.
topRight();
00230 }
00231
return *
this;
00232 };
00233
00235 BoundingBox operator +(
const BoundingBox& u){
00236
BoundingBox b = *
this;
00237
return b += u;
00238 };
00239
00240 };
00241
00242
00244
00245
00246 }
00247
00248
#endif //LEMON_XY_H