This machine mirrors various open-source projects.
20 Gbit/s uplink.
If there are any issues or you want another project mirrored, please contact
mirror-service -=AT=- netcologne DOT de !
00001 /* 00002 $Id: vector.cxx,v 1.1 2002/11/20 00:33:56 grumbel Exp $ 00003 00004 ------------------------------------------------------------------------ 00005 ClanLib, the platform independent game SDK. 00006 00007 This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE 00008 version 2. See COPYING for details. 00009 00010 For a total list of contributers see CREDITS. 00011 00012 ------------------------------------------------------------------------ 00013 00014 1999/06/19 Daniel Vogel 00015 00016 totally replaced old CL_Vector with this code 00017 */ 00018 00019 #include <assert.h> 00020 #include <math.h> 00021 #include "vector.hxx" 00022 00023 CL_Vector::CL_Vector(float x, float y, float z, float w) 00024 { 00025 this->x = x; 00026 this->y = y; 00027 this->z = z; 00028 this->w = w; 00029 } 00030 00031 CL_Vector::CL_Vector(const CL_Vector &other) 00032 { 00033 x = other.x; 00034 y = other.y; 00035 z = other.z; 00036 w = other.w; 00037 } 00038 00039 float CL_Vector::norm() const 00040 { 00041 #ifdef WIN32 00042 return (float)sqrt(x*x+y*y+z*z); 00043 #else 00044 return sqrt(x*x+y*y+z*z); 00045 #endif 00046 } 00047 00048 void CL_Vector::normalize() 00049 { 00050 float f = norm(); 00051 if (f!=0) 00052 { 00053 x /= f; 00054 y /= f; 00055 z /= f; 00056 } 00057 } 00058 00059 float CL_Vector::dot(const CL_Vector& v) const 00060 { 00061 return x*v.x + y*v.y + z*v.z; 00062 } 00063 00064 float CL_Vector::angle(const CL_Vector& v) const 00065 { 00066 #ifdef WIN32 00067 return (float)acos(dot(v)/(norm()*v.norm())); 00068 #else 00069 return std::acos(dot(v)/(norm()*v.norm())); 00070 #endif 00071 } 00072 00073 CL_Vector CL_Vector::cross(const CL_Vector& v) const 00074 { 00075 CL_Vector tmp = CL_Vector(y * v.z - z * v.y, 00076 z * v.x - x * v.z, 00077 x * v.y - y * v.x); 00078 return tmp; 00079 } 00080 00081 // quick hack, same as glRotatef(angle, a); 00082 CL_Vector CL_Vector::rotate(float angle, const CL_Vector& a) const 00083 { 00084 CL_Vector tmp = CL_Vector(); 00085 00086 #ifdef WIN32 00087 float s = (float)sin(angle); 00088 float c = (float)cos(angle); 00089 #else 00090 float s = std::sin(angle); 00091 float c = std::cos(angle); 00092 #endif 00093 00094 tmp.x = x*(a.x*a.x*(1-c)+c) + y*(a.x*a.y*(1-c)-a.z*s) + z*(a.x*a.z*(1-c)+a.y*s); 00095 tmp.y = x*(a.y*a.x*(1-c)+a.z*s) + y*(a.y*a.y*(1-c)+c) + z*(a.y*a.z*(1-c)-a.x*s); 00096 tmp.z = x*(a.x*a.z*(1-c)-a.y*s) + y*(a.y*a.z*(1-c)+a.x*s) + z*(a.z*a.z*(1-c)+c); 00097 return tmp; 00098 } 00099 00100 void CL_Vector::round() 00101 { 00102 x = int(x+0.5f); 00103 y = int(y+0.5f); 00104 z = int(z+0.5f); 00105 w = int(w+0.5f); 00106 } 00107 00108 CL_Vector CL_Vector::operator * (float s) const 00109 { 00110 return CL_Vector(s * x, 00111 s * y, 00112 s * z, 00113 s * w); 00114 } 00115 00116 CL_Vector operator * (float s, const CL_Vector& v) 00117 { 00118 return CL_Vector(s * v.x, 00119 s * v.y, 00120 s * v.z, 00121 s * v.w); 00122 } 00123 00124 void CL_Vector::operator += (const CL_Vector& v) 00125 { 00126 x += v.x; 00127 y += v.y; 00128 z += v.z; 00129 w += v.z; 00130 } 00131 00132 void CL_Vector::operator -= (const CL_Vector& v) 00133 { 00134 x -= v.x; 00135 y -= v.y; 00136 z -= v.z; 00137 w -= v.w; 00138 } 00139 00140 void CL_Vector::operator *= (float s) 00141 { 00142 x *= s; 00143 y *= s; 00144 z *= s; 00145 w *= s; 00146 } 00147 00148 CL_Vector CL_Vector::operator + (const CL_Vector& v) const 00149 { 00150 return CL_Vector(x + v.x, 00151 y + v.y, 00152 z + v.z, 00153 w + v.w); 00154 } 00155 00156 CL_Vector CL_Vector::operator - (const CL_Vector& v) const 00157 { 00158 return CL_Vector(x - v.x, 00159 y - v.y, 00160 z - v.z, 00161 w - v.z); 00162 } 00163 00164 CL_Vector CL_Vector::operator - () const 00165 { 00166 return CL_Vector(-x, 00167 -y, 00168 -z, 00169 -w); 00170 } 00171 00172 CL_Vector& CL_Vector::operator = (const CL_Vector& v) 00173 { 00174 x = v.x; 00175 y = v.y; 00176 z = v.z; 00177 w = v.w; 00178 return *this; 00179 } 00180 00181 int CL_Vector::operator == (const CL_Vector& v) const 00182 { 00183 return ((x == v.x) && (y == v.y) && (z == v.z) && (w == v.w)); 00184 } 00185 00186 int CL_Vector::operator != (const CL_Vector& v) const 00187 { 00188 return !(operator == (v)); 00189 } 00190 00191 float & CL_Vector::operator [] (int n) 00192 { 00193 switch (n) 00194 { 00195 case 0: return x; 00196 case 1: return y; 00197 case 2: return z; 00198 case 3: return w; 00199 } 00200 assert(false); 00201 return x; // dummy 00202 } 00203 00204 std::ostream& operator << (std::ostream& os, const CL_Vector& v) 00205 { 00206 os << v.x << " " << v.y << " " << v.z; 00207 return os; 00208 }