1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| #ifndef POINTS_H__
#define POINTS_H__
template <typename T> class Point;
template <typename T> class Dist;
namespace DETAILS
{
template <typename T> class PointImpl_
{
public:
typedef T scalar_type;
friend class Point<T>;
friend class Dist<T>;
protected:
/**@name Construction/destruction
*/
//@{
/** Initialisation constructor.
*/
PointImpl_(scalar_type x_ = scalar_type(), scalar_type y_ = scalar_type())
: m_x(x_), m_y(y_) {}
/** Protected destructor.
*/
// ~PointImpl_() {}
//@}
/**@name Operations
*/
//@{
void add(PointImpl_ const& rhs_)
{ m_x += rhs_.m_x; m_y += rhs_.m_y; }
void dec(PointImpl_ const& rhs_)
{ m_x -= rhs_.m_x; m_y -= rhs_.m_y; }
void mult(scalar_type coeff_)
{ m_x *= coeff_; m_y *= coeff_; }
void div(scalar_type coeff_)
{ m_x /= coeff_; m_y /= coeff_; }
static bool isEqual(
PointImpl_ const& rhs_,
PointImpl_ const& lhs_)
{ return rhs_.m_x == lhs_.m_x && rhs_.m_y == lhs_.m_y; }
//@}
private:
scalar_type m_x;
scalar_type m_y;
};
}
/**
* Point class.
*
* <p><b>Semantics</b><br>
* <li> Value,
* <li> mathematical: represents a point, and not distance between
* points
*/
template <typename T> class Point : DETAILS::PointImpl_<T>
{
public:
friend class Dist<T>;
typedef typename DETAILS::PointImpl_<T>::scalar_type scalar_type;
/**@name Construction/destruction
*/
//@{
/** Initialisation constructor.
*/
Point(scalar_type x_ = scalar_type(), scalar_type y_ = scalar_type())
: DETAILS::PointImpl_<T>(x_, y_){}
//@}
/**@name Operators
*/
//@{
Point & operator+=(Dist<T> const& rhs_) { this->add(rhs_); return *this; }
Point & operator-=(Dist<T> const& rhs_) { this->dec(rhs_); return *this; }
static DETAILS::PointImpl_<T> const diff(Point const& lhs_, Point const& rhs_)
{
DETAILS::PointImpl_<T> tmp(lhs_);
tmp.dec(rhs_);
return tmp;
}
friend bool operator==(Point const& rhs_, Point const& lhs_)
{ return Point::isEqual(rhs_, lhs_); }
//@}
};
/**
* Dist class.
*
* <p><b>Semantics</b><br>
* <li> Value, mathematical:
* <li> «relative position»
* <li> mathematical: it's a relative position, represents a distance
* between points, and not a point
*/
template <typename T> class Dist : DETAILS::PointImpl_<T>
{
public:
friend class Point<T>;
typedef typename DETAILS::PointImpl_<T>::scalar_type scalar_type;
/**@name Construction/destruction
*/
//@{
/** Initialisation constructor.
*/
Dist(scalar_type x_ = scalar_type(), scalar_type y_ = scalar_type())
: DETAILS::PointImpl_<T>(x_, y_){}
//@}
/**@name Operators
*/
//@{
Dist & operator+=(Dist const& rhs_) { this->add(rhs_); return *this; }
Dist & operator-=(Dist const& rhs_) { this->dec(rhs_); return *this; }
Dist & operator*=(scalar_type coeff_){ this->mult(coeff_); return *this; }
Dist & operator/=(scalar_type coeff_){ this->div(coeff_); return *this; }
friend bool operator==(Dist const& rhs_, Dist const& lhs_)
{ return Dist::isEqual(rhs_, lhs_); }
template <typename U>
friend Dist<U> const operator-(Point<U> const& lhs_, Point<U> const& rhs_);
//@}
protected:
explicit Dist(DETAILS::PointImpl_<T> const& rhs_) : DETAILS::PointImpl_<T>(rhs_) {}
};
template <typename T> inline
Dist<T> const operator-(
Point<T> const& lhs_,
Point<T> const& rhs_)
{ return Dist<T>(Point<T>::diff(lhs_, rhs_)); }
template <typename T> inline
Point<T> const operator+(
Point<T> const& lhs_,
Dist<T> const& rhs_)
{ return Point<T>(lhs_) += rhs_; }
template <typename T> inline
Point<T> const operator+(
Dist<T> const& lhs_,
Point<T> const& rhs_)
{ return Point<T>(rhs_) += lhs_; }
template <typename T> inline
Point<T> const operator-(
Point<T> const& lhs_,
Dist<T> const& rhs_)
{ return Point<T>(lhs_) -= rhs_; }
#if 0
// Makes no sense
template <typename T> inline
Point<T> const operator-(
Dist<T> const& lhs_,
Point<T> const& rhs_);
#endif
template <typename T> inline
Dist<T> const operator+(
Dist<T> const& lhs_,
Dist<T> const& rhs_)
{ return Dist<T>(lhs_) += rhs_; }
template <typename T> inline
Dist<T> const operator-(
Dist<T> const& lhs_,
Dist<T> const& rhs_)
{ return Dist<T>(lhs_) -= rhs_; }
template <typename T> inline
Dist<T> const operator*(
Dist<T> const& lhs_,
typename Dist<T>::scalar_type rhs_)
{ return Dist<T>(lhs_) *= rhs_; }
template <typename T> inline
Dist<T> const operator*(
typename Dist<T>::scalar_type lhs_,
Dist<T> const& rhs_)
{ return Dist<T>(rhs_) *= lhs_; }
template <typename T> inline
Dist<T> const operator/(
Dist<T> const& lhs_,
typename Dist<T>::scalar_type rhs_)
{ return Dist<T>(lhs_) /= rhs_; }
template <typename T> inline
Dist<T> const operator/(
typename Dist<T>::scalar_type lhs_,
Dist<T> const& rhs_)
{ return Dist<T>(rhs_) /= lhs_; }
#endif // POINTS_H__ |
Partager