Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

point2i.H

Go to the documentation of this file.
00001 /*
00002  * Copyright 2000, Brown University, Providence, RI.
00003  * 
00004  *                         All Rights Reserved
00005  * 
00006  * Permission to use, copy, modify, and distribute this software and its
00007  * documentation for any purpose other than its incorporation into a
00008  * commercial product is hereby granted without fee, provided that the
00009  * above copyright notice appear in all copies and that both that
00010  * copyright notice and this permission notice appear in supporting
00011  * documentation, and that the name of Brown University not be used in
00012  * advertising or publicity pertaining to distribution of the software
00013  * without specific, written prior permission.
00014  * 
00015  * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00016  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
00017  * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
00018  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00019  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00020  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00021  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00022  */
00023 #ifndef MATH_LIB_INC_POINT2i_H
00024 #define MATH_LIB_INC_POINT2i_H
00025 
00026 #include <math.h>
00027 #include "std/config.H"
00028 
00029 /*****************************************************************
00030  * point2i
00031  *****************************************************************/
00032 typedef const class vec2i cvec2i;
00033 class vec2i {
00034 protected :
00035     int _x, _y;
00036 
00037 public:
00038 
00039     vec2i()                   : _x(0)   , _y(0)    {} 
00040     vec2i(int x, int y)       : _x(x)   , _y(y)    {}
00041 
00042     vec2i  operator + (cvec2i &v)    const { return vec2i(_x+v._x, _y+v._y); }
00043     vec2i  operator - (cvec2i &v)    const { return vec2i(_x-v._x, _y-v._y); }
00044     int    operator * (cvec2i &v)    const { return _x*v._x+_y*v._y;         }
00045     vec2i  operator - ()             const { return vec2i(-_x, -_y);         }
00046 
00047     int   operator [](int index)  const { return (&_x)[index];     }
00048     int&  operator [](int index)        { return (&_x)[index];     }
00049 
00050     double   length     ()           const { return sqrt((double)_x*_x+_y*_y);}
00051     int      lengthSqrd ()           const { return _x*_x+_y*_y;       }
00052 
00053     vec2i    perpend    ()           const { return vec2i(-_y, _x); }
00054 
00055     double   dist       (cvec2i &v)  const { return (*this-v).length();     }
00056     int      distSqrd   (cvec2i &v)  const { return (*this-v).lengthSqrd(); }
00057 
00058     int      isNull()                const { return _x == 0 && _y == 0; }
00059     
00060     void     operator +=(cvec2i &v)        { _x += v._x; _y += v._y;       }
00061     void     operator -=(cvec2i &v)        { _x -= v._x; _y -= v._y;       }
00062     int      operator ==(cvec2i& v) const { return v._x == _x && v._y == _y;}
00063     int      operator !=(cvec2i& v) const { return v._x != _x || v._y != _y;}
00064 };
00065 
00066 inline ostream &
00067 operator<<(ostream &os, cvec2i &v)
00068 { 
00069    return os << "{ " << v[0] << " " << v[1] << " } "; 
00070 }
00071 
00072 inline istream &
00073 operator>>(istream &is, vec2i &v) 
00074 { 
00075    char dummy;
00076    return is >> dummy >> v[0] >> v[1] >> dummy; 
00077 }
00078 
00079 typedef ARRAY<vec2i> vec2i_list;
00080 
00081 /*****************************************************************
00082  * point2i
00083  *****************************************************************/
00084 typedef const class point2i cpoint2i;
00085 class point2i {
00086 protected:
00087     int _x, _y;
00088 
00089 public:
00090 
00091     point2i()                     : _x(0),    _y(0)    {}
00092     point2i(int xx, int yy)       : _x(xx),   _y(yy)   {}
00093 
00094     const int *data()                  const { return &_x; }
00095     point2i  operator  %(cpoint2i &p) const { return point2i(_x+p[0],_y+p[1]);}
00096     point2i  operator  +(cvec2i   &v) const { return point2i(_x+v[0],_y+v[1]);}
00097     vec2i    operator  -(cpoint2i &p) const { return vec2i  (_x-p[0],_y-p[1]);}
00098     point2i  operator  -(cvec2i   &v) const { return point2i(_x-v[0],_y-v[1]);}
00099     point2i  operator  -()            const { return point2i(-_x,   -_y);     }
00100 
00101     int   operator [](int index)    const { return (&_x)[index];     }
00102     int&  operator [](int index)          { return (&_x)[index];     }
00103 
00104     double   length     () const { return sqrt((double) (_x*_x+_y*_y)); }
00105     int      lengthSqrd () const { return _x*_x+_y*_y;       }
00106 
00107     int      distSqrd(cpoint2i &p) const { return (_x-p._x) * (_x-p._x) + 
00108                                                       (_y-p._y) * (_y-p._y);  }
00109     double   dist    (cpoint2i &p) const { return sqrt((double) distSqrd(p)); }
00110 
00111     void     operator +=(cpoint2i &p)        { _x += p[0]; _y += p[1];     }
00112     void     operator +=(cvec2i   &v)        { _x += v[0]; _y += v[1];     }
00113     void     operator -=(cpoint2i &p)        { _x -= p[0]; _y -= p[1];     }
00114     void     operator -=(cvec2i   &v)        { _x -= v[0]; _y -= v[1];     }
00115 
00116     int      operator ==(cpoint2i &p) const { return _x == p._x && _y == p._y;}
00117     int      operator !=(cpoint2i &p) const { return _x != p._x || _y != p._y;}
00118 };
00119 
00120 inline ostream &
00121 operator<<(ostream &os, cpoint2i &p)
00122 { 
00123    return os << "< " << p[0] << " " << p[1] << " > "; 
00124 }
00125 
00126 inline istream &
00127 operator>>(istream &is, point2i &p) 
00128 { 
00129    char dummy;
00130    return is >> dummy >> p[0] >> p[1] >> dummy; 
00131 }
00132 
00133 typedef ARRAY<point2i> point2i_list;
00134 
00135 #endif // MATH_LIB_INC_Point2i_H
00136 
00137 /* end of file point2i.H */
00138 

Generated on Mon Sep 15 16:25:57 2003 for gluebase by doxygen1.2.18