-
Notifications
You must be signed in to change notification settings - Fork 2
/
_vector_scale.gml
32 lines (26 loc) · 911 Bytes
/
_vector_scale.gml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/// @func _vector_scale(v, r[, p])
/// @desc Scales a vector so that it achieves a given (Lp-) norm while maintaining its direction.
/// @requires _vector_norm
/// @param {real[]} v - Vector to scale.
/// @param {real} r - Target norm.
/// @param {real} [p=2] - Parameter to define Lp-norm (must be between 1 and infinity, inclusive).
/// @return {real[]} Rescaled input vector with the specified norm.
function _vector_scale(v, r)
{
// Check for optional p argument
var p = (argument_count > 2 ? argument[2] : 2);
// Verify that p >= 1
if (p < 1)
return v;
// Calculate initial norm
var ri = _vector_norm(v, p);
// If the norm is 0, do nothing
if (ri == 0)
return v;
// Otherwise rescale each component of the vector
var vr = array_create(array_length(v)); // output vector
for (var i = 0; i < array_length(v); i++)
vr[i] = v[i]*(r/ri);
// Return the rescaled vector
return vr;
}