From af34bad8b39e95276b0d4e08ccdc26f89df2d2ec Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 19 Aug 2010 13:24:37 +0000 Subject: Updates to maths and added the use of a 3D vector. --- vec3d.cpp | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 vec3d.cpp (limited to 'vec3d.cpp') diff --git a/vec3d.cpp b/vec3d.cpp new file mode 100644 index 0000000..5e073d8 --- /dev/null +++ b/vec3d.cpp @@ -0,0 +1,166 @@ +// +// glgrav - OpenGL N-Body gravity simulator +// +// Copyright (C) 2003 Ian Cowburn (ianc@noddybox.demon.co.uk) +// +// 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 +// +// ------------------------------------------------------------------------- +// +// 3D Vector +// +// +static const char id[]="$Id: sparks.cpp 8 2010-08-18 14:38:34Z ianc $"; + +#include "vec3d.h" + +#include + +Vec3D::Vec3D() +{ + m_dx = 0.0; + m_dy = 0.0; + m_dz = 0.0; +} + +Vec3D::Vec3D(double dx, double dy, double dz) +{ + m_dx = dx; + m_dy = dy; + m_dz = dz; +} + +Vec3D::~Vec3D() +{ +} + +void Vec3D::Set(double dx, double dy ,double dz) +{ + m_dx = dx; + m_dy = dy; + m_dz = dz; +} + +double Vec3D::dx() const +{ + return m_dx; +} + +double Vec3D::dy() const +{ + return m_dy; +} + +double Vec3D::dz() const +{ + return m_dz; +} + + +bool Vec3D::IsNull() const +{ + return m_dx == 0.0 && m_dy == 0.0 && m_dz == 0.0; +} + + +double Vec3D::Length() const +{ + if (IsNull()) + { + return 0.0; + } + else + { + return sqrt(m_dx * m_dx + m_dy * m_dy + m_dz * m_dz); + } +} + + +void Vec3D::Normalise() +{ + double l; + + l = Length(); + + m_dx /= l; + m_dy /= l; + m_dz /= l; +} + +void Vec3D::SetLength(double d) +{ + Normalise(); + Scale(d); +} + + +void Vec3D::AddScalar(double d) +{ + m_dx += d; + m_dy += d; + m_dz += d; +} + +void Vec3D::Minus() +{ + m_dx = -m_dx; + m_dy = -m_dy; + m_dz = -m_dz; +} + +void Vec3D::Scale(double d) +{ + m_dx *= d; + m_dy *= d; + m_dz *= d; +} + + +void Vec3D::Add(const Vec3D& v) +{ + m_dx += v.m_dx; + m_dy += v.m_dy; + m_dz += v.m_dz; +} + +void Vec3D::Subtract(const Vec3D& v) +{ + m_dx -= v.m_dx; + m_dy -= v.m_dy; + m_dz -= v.m_dz; +} + +void Vec3D::Scale(const Vec3D& v) +{ + m_dx *= v.m_dx; + m_dy *= v.m_dy; + m_dz *= v.m_dz; +} + +double Vec3D::DotProduct(const Vec3D& v) +{ + return m_dx*v.m_dx + m_dy*v.m_dy + m_dz*v.m_dz; +} + +Vec3D Vec3D::CrossProduct(const Vec3D& v) +{ + return Vec3D(m_dy * v.m_dz - v.m_dy * m_dz, + m_dz * v.m_dx - v.m_dz * m_dx, + m_dx * v.m_dy - v.m_dx * m_dy); +} + + + +// END OF FILE // -- cgit v1.2.3