Back to index

extremetuxracer  0.5beta
vec3d.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPGLTK
00003  *
00004  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  */
00020 
00021 #include "vec3d.h"
00022 
00023 #include <math.h>
00024 
00025 namespace pp{
00026 
00027 Vec3d::Vec3d()
00028  : x(0.0), y(0.0), z(0.0)
00029 {
00030 }
00031        
00032 Vec3d::Vec3d(const double x, const double y, const double z)
00033  : x(x), y(y), z(z)
00034 {
00035 }
00036 
00037 Vec3d::Vec3d(const double *v)
00038  : x(v[0]), y(v[1]), z(v[2])
00039 {
00040 }
00041 
00042 
00043 double 
00044 Vec3d::normalize()
00045 {
00046     double len = length();
00047        if (len>0.0)
00048        {
00049               x /= len;
00050               y /= len;
00051               z /= len;
00052        }                
00053        return(len);
00054 };
00055 
00056 double
00057 Vec3d::length() const
00058 {
00059        return sqrt(x*x+y*y+z*z);
00060 }
00061 
00062 double
00063 Vec3d::length2() const
00064 {
00065        return x*x+y*y+z*z;
00066 }
00067 
00068 Vec3d
00069 operator+(const Vec3d& vec1,const Vec3d& vec2)
00070 {
00071        return Vec3d(vec1.x+vec2.x,vec1.y+vec2.y,vec1.z+vec2.z);
00072 }
00073 
00074 Vec3d
00075 operator-(const Vec3d& vec1,const Vec3d& vec2)
00076 {
00077        return Vec3d(vec1.x-vec2.x,vec1.y-vec2.y,vec1.z-vec2.z);
00078 }
00079 
00080 Vec3d
00081 operator*(const Vec3d& vec, const double scalar)
00082 {
00083        return Vec3d(vec.x*scalar,vec.y*scalar,vec.z*scalar);
00084 }
00085 
00086 Vec3d
00087 operator*(const double scalar, const Vec3d& vec)
00088 {
00089        return Vec3d(vec.x*scalar,vec.y*scalar,vec.z*scalar);
00090 }
00091 
00092 double
00093 operator*(const Vec3d& vec1,const Vec3d& vec2)
00094 {
00095        return vec1.x*vec2.x+vec1.y*vec2.y+vec1.z*vec2.z;
00096 }
00097 
00098 Vec3d 
00099 operator^(const Vec3d& vec1,const Vec3d& vec2)
00100 {
00101        return Vec3d(
00102               vec1.y * vec2.z - vec1.z * vec2.y,
00103               vec1.z * vec2.x - vec1.x * vec2.z,
00104               vec1.x * vec2.y - vec1.y * vec2.x
00105        );
00106 }
00107 
00108 } //namespace pp