00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef VECTOR_H
00023 #define VECTOR_H
00024
00025 #include <math.h>
00026
00027 #include <iostream>
00028
00033 class Vector
00034 {
00035 public:
00039 Vector():
00040 x(0.0f),
00041 y(0.0f),
00042 z(0.0f)
00043 {}
00044
00048 Vector(float x, float y, float z = 0.0f):
00049 x(x),
00050 y(y),
00051 z(z)
00052 {}
00053
00057 Vector(const Vector &v):
00058 x(v.x),
00059 y(v.y),
00060 z(v.z)
00061 {}
00062
00066 Vector operator*(float c) const
00067 {
00068 return Vector(x * c,
00069 y * c,
00070 z * c);
00071 }
00072
00076 Vector &operator*=(float c)
00077 {
00078 x *= c;
00079 y *= c;
00080 z *= c;
00081 return *this;
00082 }
00083
00087 Vector operator/(float c) const
00088 {
00089 return Vector(x / c,
00090 y / c,
00091 z / c);
00092 }
00093
00097 Vector &operator/=(float c)
00098 {
00099 x /= c;
00100 y /= c;
00101 z /= c;
00102 return *this;
00103 }
00104
00108 Vector operator+(const Vector &v) const
00109 {
00110 return Vector(x + v.x,
00111 y + v.y,
00112 z + v.z);
00113 }
00114
00118 Vector &operator+=(const Vector &v)
00119 {
00120 x += v.x;
00121 y += v.y;
00122 z += v.z;
00123 return *this;
00124 }
00125
00129 Vector operator-(const Vector &v) const
00130 {
00131 return Vector(x - v.x,
00132 y - v.y,
00133 z - v.z);
00134 }
00135
00139 Vector &operator-=(const Vector &v)
00140 {
00141 x -= v.x;
00142 y -= v.y;
00143 z -= v.z;
00144 return *this;
00145 }
00146
00151 float length() const
00152 {
00153 return sqrtf(x * x + y * y + z * z);
00154 }
00155
00159 float squaredLength() const
00160 {
00161 return x * x + y * y + z * z;
00162 }
00163
00167 float manhattanLength() const
00168 {
00169 return fabsf(x) + fabsf(y) + fabsf(z);
00170 }
00171
00176 Vector normalized() const
00177 {
00178 const float l = length();
00179 return Vector(x / l, y / l, z / l);
00180 }
00181
00182 float x, y, z;
00183 };
00184
00188 std::ostream& operator <<(std::ostream &os, const Vector &v);
00189
00190 #endif // VECTOR_H