Rebuild autotool system
[construo.git] / vector.cxx
blob9a7967c3bc94d76ec106bf66ade413bd504ae19c
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 return (float)acos(dot(v)/(norm()*v.norm()));
69 CL_Vector CL_Vector::cross(const CL_Vector& v) const
71 CL_Vector tmp = CL_Vector(y * v.z - z * v.y,
72 z * v.x - x * v.z,
73 x * v.y - y * v.x);
74 return tmp;
77 // quick hack, same as glRotatef(angle, a);
78 CL_Vector CL_Vector::rotate(float angle, const CL_Vector& a) const
80 CL_Vector tmp = CL_Vector();
82 float s = (float)sin(angle);
83 float c = (float)cos(angle);
85 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);
86 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);
87 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);
88 return tmp;
91 void CL_Vector::round()
93 x = int(x+0.5f);
94 y = int(y+0.5f);
95 z = int(z+0.5f);
96 w = int(w+0.5f);
99 CL_Vector CL_Vector::operator * (float s) const
101 return CL_Vector(s * x,
102 s * y,
103 s * z,
104 s * w);
107 CL_Vector operator * (float s, const CL_Vector& v)
109 return CL_Vector(s * v.x,
110 s * v.y,
111 s * v.z,
112 s * v.w);
115 void CL_Vector::operator += (const CL_Vector& v)
117 x += v.x;
118 y += v.y;
119 z += v.z;
120 w += v.z;
123 void CL_Vector::operator -= (const CL_Vector& v)
125 x -= v.x;
126 y -= v.y;
127 z -= v.z;
128 w -= v.w;
131 void CL_Vector::operator *= (float s)
133 x *= s;
134 y *= s;
135 z *= s;
136 w *= s;
139 CL_Vector CL_Vector::operator + (const CL_Vector& v) const
141 return CL_Vector(x + v.x,
142 y + v.y,
143 z + v.z,
144 w + v.w);
147 CL_Vector CL_Vector::operator - (const CL_Vector& v) const
149 return CL_Vector(x - v.x,
150 y - v.y,
151 z - v.z,
152 w - v.z);
155 CL_Vector CL_Vector::operator - () const
157 return CL_Vector(-x,
160 -w);
163 CL_Vector& CL_Vector::operator = (const CL_Vector& v)
165 x = v.x;
166 y = v.y;
167 z = v.z;
168 w = v.w;
169 return *this;
172 int CL_Vector::operator == (const CL_Vector& v) const
174 return ((x == v.x) && (y == v.y) && (z == v.z) && (w == v.w));
177 int CL_Vector::operator != (const CL_Vector& v) const
179 return !(operator == (v));
182 float & CL_Vector::operator [] (int n)
184 switch (n)
186 case 0: return x;
187 case 1: return y;
188 case 2: return z;
189 case 3: return w;
191 assert(false);
192 return x; // dummy
195 std::ostream& operator << (std::ostream& os, const CL_Vector& v)
197 os << v.x << " " << v.y << " " << v.z;
198 return os;