// // 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 //