Add new xs stocks (#426)
xs_vec_len_2d returns the length of a 2D vector xs_vec_distance returns the distance between two vectors xs_vec_distance_2d returns the distance between two 2D vectors xs_vec_add_scaled and xs_vec_sub_scaled do two operations in one for convenience and performance. They add/subtract a vector scaled by a scalar to another vector. Very useful when working with unit vectors.
This commit is contained in:
parent
da80667fb0
commit
ff488dd81f
@ -369,6 +369,24 @@ stock xs_vec_sub(const Float:in1[], const Float:in2[], Float:out[])
|
||||
out[2] = in1[2] - in2[2];
|
||||
}
|
||||
|
||||
// Adds the second vector scaled by a scalar to the first
|
||||
// tested
|
||||
stock xs_vec_add_scaled(const Float:in1[], const Float:in2[], Float:scalar, Float:out[])
|
||||
{
|
||||
out[0] = in1[0] + in2[0] * scalar;
|
||||
out[1] = in1[1] + in2[1] * scalar;
|
||||
out[2] = in1[2] + in2[2] * scalar;
|
||||
}
|
||||
|
||||
// Subtracts the second vector scaled by a scalar to the first
|
||||
// tested
|
||||
stock xs_vec_sub_scaled(const Float:in1[], const Float:in2[], Float:scalar, Float:out[])
|
||||
{
|
||||
out[0] = in1[0] - in2[0] * scalar;
|
||||
out[1] = in1[1] - in2[1] * scalar;
|
||||
out[2] = in1[2] - in2[2] * scalar;
|
||||
}
|
||||
|
||||
// Are vectors equal?
|
||||
// untested, but should work
|
||||
stock bool:xs_vec_equal(const Float:vec1[], const Float:vec2[])
|
||||
@ -409,6 +427,30 @@ stock Float:xs_vec_len(const Float:vec[])
|
||||
return xs_sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
|
||||
}
|
||||
|
||||
// Compute 2D vector length
|
||||
// tested
|
||||
stock Float:xs_vec_len_2d(const Float:vec[])
|
||||
{
|
||||
return xs_sqrt(vec[0]*vec[0] + vec[1]*vec[1]);
|
||||
}
|
||||
|
||||
// Compute distance between two vectors
|
||||
// tested
|
||||
stock Float:xs_vec_distance(const Float:vec1[], const Float:vec2[])
|
||||
{
|
||||
return xs_sqrt((vec1[0]-vec2[0]) * (vec1[0]-vec2[0]) +
|
||||
(vec1[1]-vec2[1]) * (vec1[1]-vec2[1]) +
|
||||
(vec1[2]-vec2[2]) * (vec1[2]-vec2[2]));
|
||||
}
|
||||
|
||||
// Compute distance between two 2D vectors
|
||||
// tested
|
||||
stock Float:xs_vec_distance_2d(const Float:vec1[], const Float:vec2[])
|
||||
{
|
||||
return xs_sqrt((vec1[0]-vec2[0]) * (vec1[0]-vec2[0]) +
|
||||
(vec1[1]-vec2[1]) * (vec1[1]-vec2[1]));
|
||||
}
|
||||
|
||||
// Normalize vec
|
||||
// tested
|
||||
stock xs_vec_normalize(const Float:vec[], Float:out[])
|
||||
|
Loading…
Reference in New Issue
Block a user