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

vector2.inl

Go to the documentation of this file.
00001 /*
00002     Sheep - A Rigid Body Dynamics Engine
00003     Copyright (C) 2001-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of Sheep.
00007 
00008     Sheep is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     Sheep is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with Sheep; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 inline
00024 Vector2::Vector2() {}
00025 
00026 inline
00027 Vector2::Vector2(Real r) :
00028     m_x(r), m_y(r) {}
00029 
00030 inline
00031 Vector2::Vector2(const Vector2 &v) :
00032     m_x(v.m_x), m_y(v.m_y) {}
00033 
00034 inline
00035 Vector2::Vector2(Real x, Real y) :
00036     m_x(x), m_y(y) {}
00037 
00038 inline
00039 Vector2 &Vector2::operator=(Real rhs) {
00040     m_x = m_y = rhs;
00041 
00042     return *this;
00043 }
00044 
00045 inline
00046 Vector2 &Vector2::operator=(const Vector2 &rhs) {
00047     m_x = rhs.m_x;
00048     m_y = rhs.m_y;
00049 
00050     return *this;
00051 }
00052 
00053 inline
00054 Real &Vector2::operator[](int i) {
00055     assert(i == 0 || i == 1);
00056 
00057     return *(&m_x + i);
00058 }
00059 
00060 inline
00061 Real Vector2::SquareNorm() const {
00062     return m_x * m_x + m_y * m_y;
00063 }
00064 
00065 inline
00066 Real Vector2::Norm() const {
00067     return sqrt(SquareNorm());
00068 }
00069 
00070 inline
00071 Vector2 Vector2::Normalized() const {
00072     const Real n = Norm();
00073     return n == 0.0 ? Vector2(1.0, 0.0) : *this / n;
00074 }
00075 
00076 inline
00077 void Vector2::Normalize() {
00078     const Real n = Norm();
00079 
00080     if(n == 0.0) {
00081         m_x = 1.0;
00082         m_y = 0.0;
00083     } else {
00084         m_x /= n;
00085         m_y /= n;
00086     }
00087 }
00088 
00089 inline
00090 bool Vector2::IsUnitLength(Real e /*= EPS*/) const {
00091     return feq(SquareNorm(), 1.0, e);
00092 }
00093 
00094 inline
00095 Vector2 operator+(const Vector2 &lhs, const Vector2 &rhs) {
00096     return Vector2(
00097         lhs.m_x + rhs.m_x,
00098         lhs.m_y + rhs.m_y);
00099 }
00100 
00101 inline
00102 Vector2 operator-(const Vector2 &lhs, const Vector2 &rhs) {
00103     return Vector2(
00104         lhs.m_x - rhs.m_x,
00105         lhs.m_y - rhs.m_y);
00106 }
00107 
00108 inline
00109 Vector2 operator-(const Vector2 &v) {
00110     return Vector2(-v.m_x, -v.m_y);
00111 }
00112 
00113 inline
00114 Vector2 operator*(const Vector2 &lhs, Real rhs) {
00115     return Vector2(
00116         lhs.m_x * rhs,
00117         lhs.m_y * rhs);
00118 }
00119 
00120 inline
00121 Vector2 operator*(Real lhs, const Vector2 &rhs) {
00122     return Vector2(
00123         lhs * rhs.m_x,
00124         lhs * rhs.m_y);
00125 }
00126 
00127 inline
00128 Vector2 operator/(const Vector2 &lhs, Real rhs) {
00129     return Vector2(
00130         lhs.m_x / rhs,
00131         lhs.m_y / rhs);
00132 }
00133 
00134 inline
00135 Vector2 &operator+=(Vector2 &lhs, const Vector2 &rhs) {
00136     lhs.m_x += rhs.m_x;
00137     lhs.m_y += rhs.m_y;
00138 
00139     return lhs;
00140 }
00141 
00142 inline
00143 Vector2 &operator-=(Vector2 &lhs, const Vector2 &rhs) {
00144     lhs.m_x -= rhs.m_x;
00145     lhs.m_y -= rhs.m_y;
00146 
00147     return lhs;
00148 }
00149 
00150 inline
00151 Vector2 &operator*=(Vector2 &lhs, Real rhs) {
00152     lhs.m_x *= rhs;
00153     lhs.m_y *= rhs;
00154 
00155     return lhs;
00156 }
00157 
00158 inline
00159 Vector2 &operator/=(Vector2 &lhs, Real rhs) {
00160     lhs.m_x /= rhs;
00161     lhs.m_y /= rhs;
00162 
00163     return lhs;
00164 }
00165 
00166 inline
00167 Real operator*(const Vector2 &lhs, const Vector2 &rhs) {
00168     return
00169         lhs.m_x * rhs.m_x +
00170         lhs.m_y * rhs.m_y;
00171 }

Generated on Tue May 11 01:31:52 2004 for toxic by doxygen 1.3.6