00001
00013 #ifndef __vector_h__
00014 #define __vector_h__
00015
00016 #include <math.h>
00017 #include "globals.h"
00018 #ifndef PI
00019 #define PI 3.1415926535897;
00020 #endif
00021
00028 class Vector
00029 {
00030
00031 public:
00032
00033 inline Vector(void) { this->x=0;this->y=0;this->z=0;}
00035 inline Vector(float X, float Y, float Z) { this->x = X; this->y = Y; this->z = Z; }
00037 inline Vector(const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; }
00039 inline Vector(float rgfl[3]) { this->x = rgfl[0]; this->y = rgfl[1]; this->z = rgfl[2]; }
00041 inline Vector(Vector spitze, Vector schaft)
00042 {
00043 Vector v = (spitze - schaft);
00044 x = v.x;
00045 y = v.y;
00046 z = v.z;
00047 }
00049
00050 inline Vector operator-(void) const { return Vector(-x,-y,-z); }
00052 inline int operator==(const Vector& v) const { return x==v.x && y==v.y && z==v.z; }
00054 inline int operator!=(const Vector& v) const { return !(*this==v); }
00056 inline Vector operator+(const Vector& v) const { return Vector(x+v.x, y+v.y, z+v.z); }
00058 inline Vector operator-(const Vector& v) const { return Vector(x-v.x, y-v.y, z-v.z); }
00060 inline Vector operator*(float fl) const { return Vector(x*fl, y*fl, z*fl); }
00062 inline Vector operator/(float fl) const { return Vector(x/fl, y/fl, z/fl); }
00066
00067 inline void copyToArray(float* rgfl) const { rgfl[0] = x, rgfl[1] = y, rgfl[2] = z; }
00069 inline float length(void) const { return sqrt(x*x + y*y + z*z); }
00072 Vector normalize(void) const
00073 {
00074 float flLen = this->length();
00075 if (flLen == 0) return Vector(0,0,0);
00076 return Vector(x / flLen, y / flLen, z / flLen);
00077 };
00079 inline float dotProduct(const Vector& b)
00080 {
00081 return(x*b.x + y*b.y + z*b.z);
00082 };
00084 inline Vector crossProduct(const Vector& b)
00085 {
00086 return Vector( y*b.z - z*b.y, z*b.x - x*b.z, x*b.y - y*b.x );
00087 };
00089 void rotateX(float angle)
00090 {
00091 float bg = (angle/180)*PI;
00092 float x= this->x;
00093 float y= this->y;
00094 float z= this->z;
00095
00096 float cosTheta = (float)cos(bg);
00097 float sinTheta = (float)sin(bg);
00098
00099 this->x= x;
00100 this->y= clampTo(-1,1,y*cosTheta - z *sinTheta );
00101 this->z= clampTo(-1,1,y*sinTheta + z *cosTheta) ;
00102 this->normalize();
00103
00104
00105 };
00107 void rotateY(float angle)
00108 {
00109 float bg = (angle/180)*PI;
00110 float x= this->x;
00111 float y= this->y;
00112 float z= this->z;
00113 float cosTheta = (float)cos(bg);
00114 float sinTheta = (float)sin(bg);
00115
00116 this->x= clampTo(-1,1,x*cosTheta + z*sinTheta);
00117 this->y= y;
00118 this->z= clampTo(-1,1,-1*x*sinTheta + z *cosTheta) ;
00119 };
00121 void rotateZ(float angle)
00122 {
00123
00124 float bg = (angle/180)*PI;
00125 float x= this->x;
00126 float y= this->y;
00127 float z= this->z;
00128
00129 float cosTheta = (float)cos(bg);
00130 float sinTheta = (float)sin(bg);
00131
00132 this->x= clampTo(-1,1,x*cosTheta - y*sinTheta);
00133 this->y= clampTo(-1,1,x*sinTheta + y *cosTheta );
00134 this->z= z;
00135 this->normalize();
00136
00137 };
00139 static float clampTo(float min, float max, float Val)
00140 {
00141 if (Val > max) {Val = max; }
00142 else {
00143 if(Val < min){ Val = min;}
00144 }
00145
00146 return Val;
00147 };
00150
00151 float x
00152 , y
00153 , z ;
00154 };
00155
00156
00157
00164 class CVector2
00165 {
00166
00167 public:
00168 CVector2() { x=0; y=0; }
00171 inline CVector2(float X, float Y) { x=X; y=Y; }
00175 CVector2(CVector2 spitze, CVector2 schaft)
00176 {
00177 CVector2 v = (spitze - schaft);
00178 x = v.x;
00179 y = v.y;
00180 }
00182 CVector2(float x1,float y1,float x2,float y2)
00183 {
00184 CVector2 v = (CVector2(x1,y1) - CVector2(x2,y2));
00185 x = v.x;
00186 y = v.y;
00187 }
00190 inline CVector2 operator-(void) const { return CVector2(-x,-y); }
00192 inline int operator==(const CVector2& v) const { return x==v.x && y==v.y; }
00194 inline int operator!=(const CVector2& v) const { return !(*this==v); }
00196 inline CVector2 operator+(const CVector2& v) const { return CVector2(x+v.x, y+v.y); }
00198 inline CVector2 operator-(const CVector2& v) const { return CVector2(x-v.x, y-v.y); }
00200 inline CVector2 operator*(float fl) const { return CVector2(x*fl, y*fl); }
00202 inline CVector2 operator/(float fl) const { return CVector2(x/fl, y/fl); }
00204 inline float length(void) const { return sqrt(x*x + y*y); }
00206 inline CVector2 normalize(void) const { float len = length(); return CVector2(x/len,y/len); }
00209 float angle(CVector2 v1)
00210 {
00211 float a = (x*v1.x + y*v1.y)/(this->length() * v1.length());
00212 return acos(a);
00213 }
00216
00217 float x, y;
00218
00219
00220 };
00221
00222
00223 #endif