Imported vanilla contents from upstream
[construo.git] / vector.cxx
blobeee35f5fc751d6a143f9d1accab19610fca78afe
1 /*
2 $Id: vector.cxx,v 1.1 2002/11/20 00:33:56 grumbel Exp $
4 ------------------------------------------------------------------------
5 ClanLib, the platform independent game SDK.
7 This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
8 version 2. See COPYING for details.
10 For a total list of contributers see CREDITS.
12 ------------------------------------------------------------------------
14 1999/06/19 Daniel Vogel
16 totally replaced old CL_Vector with this code
19 #include <assert.h>
20 #include <math.h>
21 #include "vector.hxx"
23 CL_Vector::CL_Vector(float x, float y, float z, float w)
25 this->x = x;
26 this->y = y;
27 this->z = z;
28 this->w = w;
31 CL_Vector::CL_Vector(const CL_Vector &other)
33 x = other.x;
34 y = other.y;
35 z = other.z;
36 w = other.w;
39 float CL_Vector::norm() const
41 #ifdef WIN32
42 return (float)sqrt(x*x+y*y+z*z);
43 #else
44 return sqrt(x*x+y*y+z*z);
45 #endif
48 void CL_Vector::normalize()
50 float f = norm();
51 if (f!=0)
53 x /= f;
54 y /= f;
55 z /= f;
59 float CL_Vector::dot(const CL_Vector& v) const
61 return x*v.x + y*v.y + z*v.z;
64 float CL_Vector::angle(const CL_Vector& v) const
66 #ifdef WIN32
67 return (float)acos(dot(v)/(norm()*v.norm()));
68 #else
69 return std::acos(dot(v)/(norm()*v.norm()));
70 #endif
73 CL_Vector CL_Vector::cross(const CL_Vector& v) const
75 CL_Vector tmp = CL_Vector(y * v.z - z * v.y,
76 z * v.x - x * v.z,
77 x * v.y - y * v.x);
78 return tmp;
81 // quick hack, same as glRotatef(angle, a);
82 CL_Vector CL_Vector::rotate(float angle, const CL_Vector& a) const
84 CL_Vector tmp = CL_Vector();
86 #ifdef WIN32
87 float s = (float)sin(angle);
88 float c = (float)cos(angle);
89 #else
90 float s = std::sin(angle);
91 float c = std::cos(angle);
92 #endif
94 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);
95 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);
96 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);
97 return tmp;
100 void CL_Vector::round()
102 x = int(x+0.5f);
103 y = int(y+0.5f);
104 z = int(z+0.5f);
105 w = int(w+0.5f);
108 CL_Vector CL_Vector::operator * (float s) const
110 return CL_Vector(s * x,
111 s * y,
112 s * z,
113 s * w);
116 CL_Vector operator * (float s, const CL_Vector& v)
118 return CL_Vector(s * v.x,
119 s * v.y,
120 s * v.z,
121 s * v.w);
124 void CL_Vector::operator += (const CL_Vector& v)
126 x += v.x;
127 y += v.y;
128 z += v.z;
129 w += v.z;
132 void CL_Vector::operator -= (const CL_Vector& v)
134 x -= v.x;
135 y -= v.y;
136 z -= v.z;
137 w -= v.w;
140 void CL_Vector::operator *= (float s)
142 x *= s;
143 y *= s;
144 z *= s;
145 w *= s;
148 CL_Vector CL_Vector::operator + (const CL_Vector& v) const
150 return CL_Vector(x + v.x,
151 y + v.y,
152 z + v.z,
153 w + v.w);
156 CL_Vector CL_Vector::operator - (const CL_Vector& v) const
158 return CL_Vector(x - v.x,
159 y - v.y,
160 z - v.z,
161 w - v.z);
164 CL_Vector CL_Vector::operator - () const
166 return CL_Vector(-x,
169 -w);
172 CL_Vector& CL_Vector::operator = (const CL_Vector& v)
174 x = v.x;
175 y = v.y;
176 z = v.z;
177 w = v.w;
178 return *this;
181 int CL_Vector::operator == (const CL_Vector& v) const
183 return ((x == v.x) && (y == v.y) && (z == v.z) && (w == v.w));
186 int CL_Vector::operator != (const CL_Vector& v) const
188 return !(operator == (v));
191 float & CL_Vector::operator [] (int n)
193 switch (n)
195 case 0: return x;
196 case 1: return y;
197 case 2: return z;
198 case 3: return w;
200 assert(false);
201 return x; // dummy
204 std::ostream& operator << (std::ostream& os, const CL_Vector& v)
206 os << v.x << " " << v.y << " " << v.z;
207 return os;