Add square distance functions to xs.inc

This commit is contained in:
IgnacioDM 2019-01-26 09:41:45 -03:00
parent ba083deb1e
commit ea7ffafd88

View File

@ -627,6 +627,37 @@ stock Float:xs_vec_distance_2d(const Float:vec1[], const Float:vec2[])
(vec1[1]-vec2[1]) * (vec1[1]-vec2[1])); (vec1[1]-vec2[1]) * (vec1[1]-vec2[1]));
} }
/**
* Computes the square of the distance between two vectors (points).
* This is faster than the distance.
*
* @param vec1 First vector.
* @param vec2 Second vector.
*
* @return The square of the distance between two vectors.
*/
stock Float:xs_vec_sqdistance(const Float:vec1[], const Float:vec2[])
{
return (vec1[0]-vec2[0]) * (vec1[0]-vec2[0]) +
(vec1[1]-vec2[1]) * (vec1[1]-vec2[1]) +
(vec1[2]-vec2[2]) * (vec1[2]-vec2[2]);
}
/**
* Computes the square of the distance between two 2D vectors (points).
* This is faster than the distance.
*
* @param vec1 First vector.
* @param vec2 Second vector.
*
* @return The square of the distance between two vectors.
*/
stock Float:xs_vec_sqdistance_2d(const Float:vec1[], const Float:vec2[])
{
return (vec1[0]-vec2[0]) * (vec1[0]-vec2[0]) +
(vec1[1]-vec2[1]) * (vec1[1]-vec2[1]);
}
/** /**
* Normalizes a vector. Normalized vector is a vector with the length of 1 unit, * Normalizes a vector. Normalized vector is a vector with the length of 1 unit,
* but with the same direction as the original vector. * but with the same direction as the original vector.