1 /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2 /* If you are missing that file, acquire a complete release at teeworlds.com. */
8 // ------------------------------------
18 vector2_base(float nx
, float ny
)
24 vector2_base
operator -() const { return vector2_base(-x
, -y
); }
25 vector2_base
operator -(const vector2_base
&v
) const { return vector2_base(x
-v
.x
, y
-v
.y
); }
26 vector2_base
operator +(const vector2_base
&v
) const { return vector2_base(x
+v
.x
, y
+v
.y
); }
27 vector2_base
operator *(const T v
) const { return vector2_base(x
*v
, y
*v
); }
29 const vector2_base
&operator =(const vector2_base
&v
) { x
= v
.x
; y
= v
.y
; return *this; }
31 const vector2_base
&operator +=(const vector2_base
&v
) { x
+= v
.x
; y
+= v
.y
; return *this; }
32 const vector2_base
&operator -=(const vector2_base
&v
) { x
-= v
.x
; y
-= v
.y
; return *this; }
33 const vector2_base
&operator *=(const T v
) { x
*= v
; y
*= v
; return *this; }
35 bool operator ==(const vector2_base
&v
) const { return x
== v
.x
&& y
== v
.y
; } //TODO: do this with an eps instead
37 operator const T
* () { return &x
; }
42 inline T
length(const vector2_base
<T
> &a
)
44 return sqrtf(a
.x
*a
.x
+ a
.y
*a
.y
);
48 inline T
distance(const vector2_base
<T
> a
, const vector2_base
<T
> &b
)
54 inline T
dot(const vector2_base
<T
> a
, const vector2_base
<T
> &b
)
56 return a
.x
*b
.x
+ a
.y
*b
.y
;
60 inline vector2_base
<T
> normalize(const vector2_base
<T
> &v
)
62 T l
= (T
)(1.0f
/sqrtf(v
.x
*v
.x
+ v
.y
*v
.y
));
63 return vector2_base
<T
>(v
.x
*l
, v
.y
*l
);
66 typedef vector2_base
<float> vec2
;
67 typedef vector2_base
<bool> bvec2
;
68 typedef vector2_base
<int> ivec2
;
71 inline vector2_base
<T
> closest_point_on_line(vector2_base
<T
> line_point0
, vector2_base
<T
> line_point1
, vector2_base
<T
> target_point
)
73 vector2_base
<T
> c
= target_point
- line_point0
;
74 vector2_base
<T
> v
= (line_point1
- line_point0
);
76 T d
= length(line_point0
-line_point1
);
78 return mix(line_point0
, line_point1
, clamp(t
, (T
)0, (T
)1));
81 if (t > 1.0f) return 1.0f;
85 // ------------------------------------
95 vector3_base(float nx
, float ny
, float nz
)
102 const vector3_base
&operator =(const vector3_base
&v
) { x
= v
.x
; y
= v
.y
; z
= v
.z
; return *this; }
104 vector3_base
operator -(const vector3_base
&v
) const { return vector3_base(x
-v
.x
, y
-v
.y
, z
-v
.z
); }
105 vector3_base
operator -() const { return vector3_base(-x
, -y
, -z
); }
106 vector3_base
operator +(const vector3_base
&v
) const { return vector3_base(x
+v
.x
, y
+v
.y
, z
+v
.z
); }
107 vector3_base
operator *(const T v
) const { return vector3_base(x
*v
, y
*v
, z
*v
); }
108 vector3_base
operator *(const vector3_base
&v
) const { return vector3_base(x
*v
.x
, y
*v
.y
, z
*v
.z
); }
109 vector3_base
operator /(const T v
) const { return vector3_base(x
/v
, y
/v
, z
/v
); }
111 const vector3_base
&operator +=(const vector3_base
&v
) { x
+= v
.x
; y
+= v
.y
; z
+= v
.z
; return *this; }
112 const vector3_base
&operator -=(const vector3_base
&v
) { x
-= v
.x
; y
-= v
.y
; z
-= v
.z
; return *this; }
113 const vector3_base
&operator *=(const T v
) { x
*= v
; y
*= v
; z
*= v
; return *this; }
115 bool operator ==(const vector3_base
&v
) const { return x
== v
.x
&& y
== v
.y
&& z
== v
.z
; } //TODO: do this with an eps instead
117 operator const T
* () { return &x
; }
121 inline T
length(const vector3_base
<T
> &a
)
123 return sqrtf(a
.x
*a
.x
+ a
.y
*a
.y
+ a
.z
*a
.z
);
127 inline T
distance(const vector3_base
<T
> &a
, const vector3_base
<T
> &b
)
133 inline T
dot(const vector3_base
<T
> &a
, const vector3_base
<T
> &b
)
135 return a
.x
*b
.x
+ a
.y
*b
.y
+ a
.z
*b
.z
;
139 inline vector3_base
<T
> normalize(const vector3_base
<T
> &v
)
141 T l
= (T
)(1.0f
/sqrtf(v
.x
*v
.x
+ v
.y
*v
.y
+ v
.z
*v
.z
));
142 return vector3_base
<T
>(v
.x
*l
, v
.y
*l
, v
.z
*l
);
146 inline vector3_base
<T
> cross(const vector3_base
<T
> &a
, const vector3_base
<T
> &b
)
148 return vector3_base
<T
>(
154 typedef vector3_base
<float> vec3
;
155 typedef vector3_base
<bool> bvec3
;
156 typedef vector3_base
<int> ivec3
;
158 // ------------------------------------
170 vector4_base(float nx
, float ny
, float nz
, float nw
)
178 vector4_base
operator +(const vector4_base
&v
) const { return vector4_base(x
+v
.x
, y
+v
.y
, z
+v
.z
, w
+v
.w
); }
179 vector4_base
operator -(const vector4_base
&v
) const { return vector4_base(x
-v
.x
, y
-v
.y
, z
-v
.z
, w
-v
.w
); }
180 vector4_base
operator -() const { return vector4_base(-x
, -y
, -z
, -w
); }
181 vector4_base
operator *(const vector4_base
&v
) const { return vector4_base(x
*v
.x
, y
*v
.y
, z
*v
.z
, w
*v
.w
); }
182 vector4_base
operator *(const T v
) const { return vector4_base(x
*v
, y
*v
, z
*v
, w
*v
); }
184 const vector4_base
&operator =(const vector4_base
&v
) { x
= v
.x
; y
= v
.y
; z
= v
.z
; w
= v
.w
; return *this; }
186 const vector4_base
&operator +=(const vector4_base
&v
) { x
+= v
.x
; y
+= v
.y
; z
+= v
.z
; w
+= v
.w
; return *this; }
187 const vector4_base
&operator -=(const vector4_base
&v
) { x
-= v
.x
; y
-= v
.y
; z
-= v
.z
; w
-= v
.w
; return *this; }
188 const vector4_base
&operator *=(const T v
) { x
*= v
; y
*= v
; z
*= v
; w
*= v
; return *this; }
190 bool operator ==(const vector4_base
&v
) const { return x
== v
.x
&& y
== v
.y
&& z
== v
.z
&& w
== v
.w
; } //TODO: do this with an eps instead
192 operator const T
* () { return &x
; }
195 typedef vector4_base
<float> vec4
;
196 typedef vector4_base
<bool> bvec4
;
197 typedef vector4_base
<int> ivec4
;