Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

point2i.H

Go to the documentation of this file.
00001 #ifndef POINT2I_H_IS_INCLUDED
00002 #define POINT2I_H_IS_INCLUDED
00003 
00004 /*!
00005  *  \file Point2i.H
00006  *  \brief Contains the definitions of the Point2i and Vec2i classes.
00007  *  \ingroup group_MLIB
00008  *
00009  */
00010 
00011 #include <cmath>
00012 #include "std/support.H"
00013 
00014 namespace mlib {
00015 
00016 class Vec2i;
00017 
00018 //! \brief Shortcut for const Vec2i
00019 //! \relates Vec2i
00020 typedef const Vec2i Cvec2i;
00021 
00022 /*!
00023  *  \brief A 2D vector class with integer components.
00024  *  \ingroup group_MLIB
00025  *
00026  */
00027 class Vec2i
00028 {
00029    
00030    protected:
00031    
00032       int _x, _y;
00033 
00034    public:
00035    
00036       //! \name Constructors
00037       //@{
00038 
00039       //! \brief Default Constructor.  Creates a zero vector.
00040       Vec2i()                   : _x(0)   , _y(0)    {}
00041       
00042       //! \brief Constructor that creates a vector with the components specified
00043       //! in the arguments.
00044       Vec2i(int x, int y)       : _x(x)   , _y(y)    {}
00045       
00046       //    Vec2i(Cvec3d &v)         : _x(v[0]), _y(v[1]) {}
00047       
00048       //@}
00049       
00050       //! \name Descriptive interface
00051       //@{
00052          
00053       typedef int value_type;
00054       static int dim() { return 2; }
00055       
00056       //@} 
00057       
00058       //! \name Overloaded Arithmetic Operators
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       //! \name Element Access Functions
00072       //@{
00073       
00074       int   operator [](int index)     const { return (&_x)[index];     }
00075       int&  operator [](int index)           { return (&_x)[index];     }
00076       
00077       //@}
00078       
00079       //! \name Vector Property Queries
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       //! \brief Are the vector's components both equal to zero?
00086       int      is_null()                const { return _x == 0 && _y == 0; }
00087       
00088       //@}
00089       
00090       //! \name Vector Operations
00091       //@{
00092       
00093       Vec2i    perpend    ()           const { return Vec2i(-_y, _x); }
00094       
00095       //@}
00096       
00097       //! \name Two Vector Operations
00098       //@{
00099       
00100       //! \brief Computes the distance between two vectors treated as position
00101       //! vectors.
00102       double   dist       (Cvec2i &v)  const { return (*this-v).length();     }
00103       //! \brief Computes the distance squared between two vectors treated as
00104       //! position vectors.
00105       int      dist_sqrd   (Cvec2i &v)  const { return (*this-v).length_sqrd(); }
00106       
00107       //@}
00108       
00109       //! \name Overloaded Comparison Operators
00110       //@{
00111       
00112       //! \brief Are the two vectors exactly equal?
00113       int      operator ==(Cvec2i& v)  const { return v._x == _x && v._y == _y;}
00114       //! \brief Are the two vectors not equal?
00115       int      operator !=(Cvec2i& v)  const { return v._x != _x || v._y != _y;}
00116       
00117       //@}
00118       
00119       //! \name Overloaded Stream Operators
00120       //@{
00121       
00122       //! \brief Stream insertion operator for Vec2i class.
00123       friend  ostream &operator<<(ostream &os, Cvec2i &v) 
00124          { os << "{"<<v._x<<","<<v._y<<"}"; return os; }
00125          
00126       //@}
00127 
00128 }; // class Vec2i
00129 
00130 } // namespace mlib
00131 
00132 namespace mlib {
00133 
00134 class Point2i;
00135 
00136 //! \brief Shortcut for const Point2i
00137 //! \relates Point2i
00138 typedef const Point2i Cpoint2i;
00139 
00140 /*!
00141  *  \brief A 2D point class with integer components.
00142  *  \ingroup group_MLIB
00143  *
00144  */
00145 class Point2i
00146 {
00147    
00148    protected:
00149    
00150       int _x, _y;
00151 
00152    public:
00153    
00154       //! \name Constructors
00155       //@{
00156 
00157       //! \brief Default Constructor.  Creates a point at the origin.
00158       Point2i()                     : _x(0),    _y(0)    {}
00159       
00160       //! \brief Constructor that creates a point with the components specified
00161       //! in the arguments.
00162       Point2i(int xx, int yy)       : _x(xx),   _y(yy)   {}
00163       
00164       //    Point2i(Cpoint3 &p)    : _x(round(p[0])), _y(round(p[1])) {}
00165       //    Point2i(Cpoint2 &p)    : _x(round(p[0])), _y(round(p[1])) {}
00166       
00167       //@}
00168       
00169       //! \name Descriptive interface
00170       //@{
00171          
00172       typedef int value_type;
00173       static int dim() { return 2; }
00174       
00175       //@} 
00176       
00177       //! \name Element Access Functions
00178       //@{
00179       
00180       //! \brief Returns the components as a 2-element array of integers.
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       //! \name Overloaded Arithmetic Operators
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       //! \name Point Property Queries
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       //! \name Two Point Operations
00213       //@{
00214       
00215       //! \brief Computes the distance squared between two points.
00216       int      dist_sqrd   (Cpoint2i &p)  const
00217          { return (_x-p._x) * (_x-p._x) + (_y-p._y) * (_y-p._y); }
00218       //! \brief Computes the distance between two points.
00219       double   dist       (Cpoint2i &p)  const
00220          { return sqrt((double) dist_sqrd(p)); }
00221       
00222       //@}
00223       
00224       //! \name Overloaded Comparison Operators
00225       //@{
00226       
00227       //! \brief Are the two points exactly equal?
00228       int      operator ==(Cpoint2i &p)  const { return _x == p._x && _y == p._y; }
00229       //! \brief Are the two points not equal?
00230       int      operator !=(Cpoint2i &p)  const { return _x != p._x || _y != p._y; }
00231       
00232       //@}
00233       
00234       //! \name Overloaded Stream Operators
00235       //@{
00236       
00237       //! \brief Stream insertion operator for Point2i class.
00238       friend  ostream &operator <<(ostream &os, const Point2i &p) 
00239          { os << "<"<<p._x<<","<<p._y<<">"; return os; }
00240                        
00241       //@}
00242                        
00243 }; // class Point2i
00244 
00245 } // namespace mlib
00246 
00247 #endif // POINT2I_H_IS_INCLUDED
00248 
00249 /* end of file Point2i.H */
00250 

Generated on Mon Sep 18 11:39:33 2006 for jot by  doxygen 1.4.4