00001 #ifndef VECTOR_H_
00002 #define VECTOR_H_
00003
00004 #include "Types.h"
00005 #include <cmath>
00006 #include <iostream>
00007
00008 class Vector;
00009
00010 inline Vector operator*(real c, const Vector& v);
00011
00012
00013 class Vector {
00014 protected:
00015 real x,y,z;
00016 real pad;
00017
00018
00019 public:
00020 friend Vector operator*(real c, const Vector& v);
00021 friend std::ostream& operator<<(std::ostream & os, const Vector &v);
00022 Vector(): x(0.0),y(0.0),z(0.0){}
00023 Vector(real _x, real _y, real _z): x(_x), y(_y), z(_z){};
00024 Vector(const Vector & v): x(v.x),y(v.y),z(v.z) {};
00025 Vector(const std::pair<real, real> & p): x(p.first), y(0.0), z(p.second){};
00026
00027 real getX() const { return x;}
00028 real getY() const { return y;}
00029 real getZ() const { return z;}
00030
00031 void setX(real newX) { x=newX;}
00032 void setY(real newY) { y=newY;}
00033 void setZ(real newZ) { z=newZ;}
00034
00035 real magnitude() const {
00036 return sqrt(x*x+y*y+z*z);
00037 }
00038
00039 Vector unit() const {
00040 return 1/magnitude()*(*this);
00041 }
00042
00043 Vector & normalize() {
00044 return (*this *= 1/(this->magnitude()));
00045 }
00046
00047 Vector & operator+=(const Vector&v) {
00048 x+= v.x;
00049 y+=v.y;
00050 z+=v.z;
00051 return *this;
00052 }
00053
00054 Vector operator+(const Vector&v) const {
00055 return Vector(x+v.x,y+v.y,z+v.z);
00056 }
00057
00058 Vector & operator-=(const Vector&v) {
00059 x-=v.x;
00060 y-=v.y;
00061 z-=v.z;
00062 return *this;
00063 }
00064
00065 Vector operator-(const Vector& v) const {
00066 return Vector(x-v.x,y-v.y,z-v.z);
00067 }
00068
00069 Vector operator-() const {
00070 return Vector(-x,-y,-z);
00071 }
00072
00073 Vector & operator*=(const real c) {
00074 x*=c;
00075 y*=c;
00076 z*=c;
00077 return *this;
00078 }
00079
00080 Vector operator*(const real c) const {
00081 return Vector(c*x,c*y,c*z);
00082 }
00083 real operator*(const Vector& v ) const {
00084 return x*v.x+y*v.y+z*v.z;
00085 }
00086
00087 Vector project(const Vector& v) const {
00088 return v*scalarProduct(v);
00089 }
00090
00091 real squareMagnitude() const {
00092 return x*x + y*y + z*z;
00093 }
00094 real scalarProduct(const Vector& v) const {
00095 return x*v.x+y*v.y+z*v.z;
00096 }
00097
00098 Vector perp2D() const {
00099 return Vector(-z,0.0,x);
00100 }
00101
00102 Vector crossProduct(const Vector& v) const {
00103 return Vector(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
00104 }
00105
00106 Vector & operator%=(const Vector& v) {
00107 *this = crossProduct(v);
00108 return *this;
00109 }
00110 Vector operator%(const Vector &v) const {
00111 return crossProduct(v);
00112 }
00113
00114 void reset() {
00115 x = y = z= 0.0;
00116 }
00117 };
00118
00119 inline Vector operator*(real c, const Vector& v) {
00120 return Vector(c*v.x,c*v.y,c*v.z);
00121 }
00122
00123
00124 #endif
00125