maths.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Matthew Williams   *
00003  *   matt@milliams.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifndef MATHS_H
00022 #define MATHS_H
00023 
00024 #include <cmath>
00025 #include <vector>
00026 
00034 namespace sparkle
00035 {
00037 
00042     struct VecXY
00043     {
00047         double x;
00051         double y;
00052         void operator()(double newX, double newY)
00053         {
00054             x = newX;
00055             y = newY;
00056         }
00057     };
00058     
00060 
00063     template<class T>
00064     class Vector
00065     {
00066         public:
00069 
00075             Vector(T startX, T startY, T startZ = 0) : x(startX), y(startY), z(startZ), real(true) {};
00076             Vector(const Vector& copyVector) : x(copyVector.getX()), y(copyVector.getY()), z(copyVector.getZ()), real(copyVector.isReal()) {}
00077             Vector(VecXY copyVecXY) : x(copyVecXY.x), y(copyVecXY.y), z(0.0), real(true) {}
00078             Vector(bool status = true) {real = status;}
00079             ~Vector() {};
00081             
00085 
00086             void setX(T newX) {x = newX;}
00087             void setY(T newY) {y = newY;}
00088             void setZ(T newZ) {z = newZ;}
00090             
00094 
00095             T getX() const {return x;}
00096             T getY() const {return y;}
00097             T getZ() const {return z;}
00098             T getLength() const {return sqrt((x*x)+(y*y)+(z*z));}
00099             bool isReal() const {return real;}
00101             
00104 
00107             Vector& operator=(const Vector& vec) {if(this == &vec) return *this; x = vec.getX(); y = vec.getY(); z = vec.getZ(); real = vec.isReal(); return *this;}
00111             Vector& operator=(const VecXY& vecxy) {x = vecxy.x; y = vecxy.y; z = 0.0; real = true; return *this;}
00115             bool operator==(const Vector& vec) const {if((vec.getX() == x) && (vec.getY() == y) && (vec.getZ() == z)) return true; else return false;}
00119             const Vector& operator+(const Vector& vec) const {return Vector(vec.getX()+x, vec.getY()+y, vec.getZ()+z);}
00123             Vector& operator+=(const Vector& vec) {x += vec.getX(); y += vec.getY(); z += vec.getZ(); return *this;}
00127             const Vector& operator-(const Vector& vec) const {return Vector(x-vec.getX(), y-vec.getY(), z-vec.getZ());}
00131             Vector& operator-=(const Vector& vec) {x -= vec.getX(); y -= vec.getY(); z -= vec.getZ(); return *this;}
00133 
00136             const double operator*(const Vector& vec) const {return ((vec.getX() * x) + (vec.getY() * y) + (vec.getZ() * z));}
00138 
00141             const Vector& operator^(const Vector& vec) const {return Vector(((vec.getZ()*y)-(vec.getY()*z)), ((vec.getX()*z)-(vec.getZ()*x)), ((vec.getY()*x)-(vec.getX()*y)));}
00142             
00147             const Vector<double>& operator^(const double& scalar) const {return Vector(x*scalar, y*scalar, z*scalar);}
00152             Vector<double>& operator^=(const double& scalar) {x *= scalar; y *= scalar; z *= scalar; return *this;}
00157             const Vector<double>& operator*(const double& scalar) const {return Vector(x*scalar, y*scalar, z*scalar);}
00161             Vector<double>& operator*=(const double& scalar) {x *= scalar; y *= scalar; z *= scalar; return *this;}
00165             const Vector<double>& operator/(const double& scalar) const {return Vector(x/scalar, y/scalar, z/scalar);}
00173             Vector<double>& operator/=(const double& scalar) {x /= scalar; y /= scalar; z /= scalar; return *this;}
00177             const Vector<double>& operator%(const double& scalar) const {return Vector(x%scalar, y%scalar, z%scalar);}
00181             Vector<double>& operator%=(const double& scalar) {x %= scalar; y %= scalar; z %= scalar; return *this;}
00183         protected:
00184             T x;
00185             T y;
00186             T z;
00187             bool real;
00188     };
00189     
00191 
00200     Vector<double> intersection(Vector<double> pos1, Vector<double> dir1, Vector<double> pos2, Vector<double> dir2);
00201     
00203 
00211     double momentOfInertia(std::vector<double> x, std::vector<double> y, double mass);
00213 
00219     double dAbs(double value);
00220 };
00221 #endif // MATHS_H

Hosted by SourceForge.net Logo Generated on Sat Sep 16 01:51:07 2006 for Sparkle by Doxygen 1.4.6.