00001 #ifndef POINT2I_H_IS_INCLUDED
00002 #define POINT2I_H_IS_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <cmath>
00012 #include "std/support.H"
00013
00014 namespace mlib {
00015
00016 class Vec2i;
00017
00018
00019
00020 typedef const Vec2i Cvec2i;
00021
00022
00023
00024
00025
00026
00027 class Vec2i
00028 {
00029
00030 protected:
00031
00032 int _x, _y;
00033
00034 public:
00035
00036
00037
00038
00039
00040 Vec2i() : _x(0) , _y(0) {}
00041
00042
00043
00044 Vec2i(int x, int y) : _x(x) , _y(y) {}
00045
00046
00047
00048
00049
00050
00051
00052
00053 typedef int value_type;
00054 static int dim() { return 2; }
00055
00056
00057
00058
00059
00060
00061 Vec2i operator + (Cvec2i &v) const { return Vec2i(_x+v._x, _y+v._y); }
00062 Vec2i operator - (Cvec2i &v) const { return Vec2i(_x-v._x, _y-v._y); }
00063 int operator * (Cvec2i &v) const { return _x*v._x+_y*v._y; }
00064 Vec2i operator - () const { return Vec2i(-_x, -_y); }
00065
00066 void operator +=(Cvec2i &v) { _x += v._x; _y += v._y; }
00067 void operator -=(Cvec2i &v) { _x -= v._x; _y -= v._y; }
00068
00069
00070
00071
00072
00073
00074 int operator [](int index) const { return (&_x)[index]; }
00075 int& operator [](int index) { return (&_x)[index]; }
00076
00077
00078
00079
00080
00081
00082 double length () const { return sqrt((double)_x*_x+_y*_y);}
00083 int length_sqrd () const { return _x*_x+_y*_y; }
00084
00085
00086 int is_null() const { return _x == 0 && _y == 0; }
00087
00088
00089
00090
00091
00092
00093 Vec2i perpend () const { return Vec2i(-_y, _x); }
00094
00095
00096
00097
00098
00099
00100
00101
00102 double dist (Cvec2i &v) const { return (*this-v).length(); }
00103
00104
00105 int dist_sqrd (Cvec2i &v) const { return (*this-v).length_sqrd(); }
00106
00107
00108
00109
00110
00111
00112
00113 int operator ==(Cvec2i& v) const { return v._x == _x && v._y == _y;}
00114
00115 int operator !=(Cvec2i& v) const { return v._x != _x || v._y != _y;}
00116
00117
00118
00119
00120
00121
00122
00123 friend ostream &operator<<(ostream &os, Cvec2i &v)
00124 { os << "{"<<v._x<<","<<v._y<<"}"; return os; }
00125
00126
00127
00128 };
00129
00130 }
00131
00132 namespace mlib {
00133
00134 class Point2i;
00135
00136
00137
00138 typedef const Point2i Cpoint2i;
00139
00140
00141
00142
00143
00144
00145 class Point2i
00146 {
00147
00148 protected:
00149
00150 int _x, _y;
00151
00152 public:
00153
00154
00155
00156
00157
00158 Point2i() : _x(0), _y(0) {}
00159
00160
00161
00162 Point2i(int xx, int yy) : _x(xx), _y(yy) {}
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 typedef int value_type;
00173 static int dim() { return 2; }
00174
00175
00176
00177
00178
00179
00180
00181 const int *data() const { return &_x; }
00182
00183 int operator [](int index) const { return (&_x)[index]; }
00184 int& operator [](int index) { return (&_x)[index]; }
00185
00186
00187
00188
00189
00190
00191 Point2i operator +(Cpoint2i &p) const { return Point2i(_x+p[0],_y+p[1]);}
00192 Point2i operator +(Cvec2i &v) const { return Point2i(_x+v[0],_y+v[1]);}
00193 Vec2i operator -(Cpoint2i &p) const { return Vec2i (_x-p[0],_y-p[1]);}
00194 Point2i operator -(Cvec2i &v) const { return Point2i(_x-v[0],_y-v[1]);}
00195 Point2i operator -() const { return Point2i(-_x, -_y); }
00196
00197 void operator +=(Cpoint2i &p) { _x += p[0]; _y += p[1]; }
00198 void operator +=(Cvec2i &v) { _x += v[0]; _y += v[1]; }
00199 void operator -=(Cpoint2i &p) { _x -= p[0]; _y -= p[1]; }
00200 void operator -=(Cvec2i &v) { _x -= v[0]; _y -= v[1]; }
00201
00202
00203
00204
00205
00206
00207 double length () const { return sqrt((double) (_x*_x+_y*_y)); }
00208 int length_sqrd () const { return _x*_x+_y*_y; }
00209
00210
00211
00212
00213
00214
00215
00216 int dist_sqrd (Cpoint2i &p) const
00217 { return (_x-p._x) * (_x-p._x) + (_y-p._y) * (_y-p._y); }
00218
00219 double dist (Cpoint2i &p) const
00220 { return sqrt((double) dist_sqrd(p)); }
00221
00222
00223
00224
00225
00226
00227
00228 int operator ==(Cpoint2i &p) const { return _x == p._x && _y == p._y; }
00229
00230 int operator !=(Cpoint2i &p) const { return _x != p._x || _y != p._y; }
00231
00232
00233
00234
00235
00236
00237
00238 friend ostream &operator <<(ostream &os, const Point2i &p)
00239 { os << "<"<<p._x<<","<<p._y<<">"; return os; }
00240
00241
00242
00243 };
00244
00245 }
00246
00247 #endif // POINT2I_H_IS_INCLUDED
00248
00249
00250