// vector.tcc // robert - programme du robot 2005. {{{ // // Copyright (C) 2005 Nicolas Schodet // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // Contact : // Web: http://perso.efrei.fr/~schodet/ // Email: // }}} namespace geometry { template VectorT & VectorT::operator+= (const VectorT &v) { x += v.x; y += v.y; return *this; } template VectorT & VectorT::operator-= (const VectorT &v) { x -= v.x; y -= v.y; return *this; } template VectorT & VectorT::operator*= (T scalar) { x *= scalar; y *= scalar; return *this; } template VectorT & VectorT::operator/= (T scalar) { x /= scalar; y /= scalar; return *this; } template VectorT VectorT::operator+ (const VectorT &v) const { VectorT r (*this); r += v; return r; } template VectorT VectorT::operator- (const VectorT &v) const { VectorT r (*this); r -= v; return r; } template VectorT VectorT::operator* (T scalar) const { VectorT r (*this); r *= scalar; return r; } template VectorT VectorT::operator/ (T scalar) const { VectorT r (*this); r /= scalar; return r; } /// Produit scalaire. template T VectorT::operator* (const VectorT &v) const { return x * v.x + y * v.y; } /// Calcule la norme. template T VectorT::norm (void) { return sqrt (x * x + y * y); } /// Normalise. template VectorT & VectorT::normalize (void) { T norm = norm (); x /= norm; y /= norm; return *this; } /// Rotation de pi/2. template VectorT & VectorT::rotate (void) { T tx = x; x = -y; y = tx; return *this; } /// Rotation. template VectorT & VectorT::rotate (T angle) { T tx = x; T c, s; sincos (angle, *s, *c); x = tx * c - y * s; y = tx * s + y * c; } /// Distance à un autre point. template T VectorT::distTo (const VectorT &v) const { return sqrt (sqDistTo (v)); } /// Distance à un autre point au carré. template T VectorT::sqDistTo (const VectorT &v) const { double dx = x - v.x; double dy = y - v.y; return dx * dx + dy * dy; } /// Multiplication. template VectorT operator* (T scalar, const VectorT &v) { VectorT r (v); r *= scalar; return r; } /// Sortie sur un ostream. template std::ostream &operator<< (std::ostream &os, const VectorT &v) { os << v.x << ' ' << v.y; return os; } } // namespace geometry