/* Danger from the Deep - Open source submarine simulation Copyright (C) 2003-2006 Thorsten Jordan, Luis Barrancos and others. 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 */ // // A 2d vector (C)+(W) 2001 Thorsten Jordan // #ifndef VECTOR2_H #define VECTOR2_H #include #ifdef WIN32 #undef min #undef max #endif #include template class vector3t; ///\brief Template class for a mathematical vector with two coefficients. template class vector2t { public: D x, y; vector2t() : x(0), y(0) {}; vector2t(const D &x_, const D &y_) : x(x_), y(y_) {}; vector2t normal() const { D len = D(1.0)/length(); return vector2t(x * len, y * len); }; void normalize() { D len = D(1.0)/length(); x *= len; y *= len; }; vector2t orthogonal() const { return vector2t(-y, x); }; vector2t operator* (const D &scalar) const { return vector2t(x * scalar, y * scalar); }; vector2t operator+ (const vector2t& other) const { return vector2t(x + other.x, y + other.y); }; vector2t operator- (const vector2t& other) const { return vector2t(x - other.x, y - other.y); }; vector2t operator- () const { return vector2t(-x, -y); }; vector2t& operator+= (const vector2t& other) { x += other.x; y += other.y; return *this; }; vector2t& operator-= (const vector2t& other) { x -= other.x; y -= other.y; return *this; }; vector2t min(const vector2t& other) const { return vector2t(x < other.x ? x : other.x, y < other.y ? y : other.y); }; vector2t max(const vector2t& other) const { return vector2t(x > other.x ? x : other.x, y > other.y ? y : other.y); }; bool operator== (const vector2t& other) const { return x == other.x && y == other.y; }; D square_length() const { return x * x + y * y; }; D length() const { return D(sqrt(square_length())); }; D square_distance(const vector2t& other) const { vector2t n = *this - other; return n.square_length(); }; D distance(const vector2t& other) const { vector2t n = *this - other; return n.length(); }; D operator* (const vector2t& other) const { return x * other.x + y * other.y; }; bool solve(const vector2t& o1, const vector2t& o2, D& s1, D& s2) const; // multiplies 2x2 matrix (given in columns c0-c1) with *this. vector2t matrixmul(const vector2t& c0, const vector2t& c1) const; vector2t coeff_mul(const vector2t& other) const { return vector2t(x * other.x, y * other.y); } vector3t xy0() const { return vector3t(x, y, 0); } vector3t xyz(const D& z) const { return vector3t(x, y, z); } template friend std::ostream& operator<< ( std::ostream& os, const vector2t& v ); template void assign(const vector2t& other) { x = D(other.x); y = D(other.y); } }; template bool vector2t::solve(const vector2t& o1, const vector2t& o2, D& s1, D& s2) const { D det = o1.x*o2.y - o2.x*o1.y; if (!det) return false; s1 = (o2.y*x - o2.x*y) / det; s2 = (o1.x*y - o1.y*x) / det; return true; } template vector2t vector2t::matrixmul(const vector2t& c0, const vector2t& c1) const { return vector2t( c0.x * x + c1.x * y, c0.y * x + c1.y * y ); } template inline vector2t operator* (const D2& scalar, const vector2t& v) { return v * scalar; } template std::ostream& operator<< ( std::ostream& os, const vector2t& v ) { os << "x=" << v.x << "; y=" << v.y; return os; } typedef vector2t vector2; typedef vector2t vector2f; typedef vector2t vector2i; #endif