Merge pull request #664 from IgnacioFDM/add-sqdistance

Add square distance functions to xs.inc
This commit is contained in:
Vincent Herbet 2019-06-19 22:37:56 +02:00 committed by GitHub
commit 5a257a7a42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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]));
}
/**
* 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,
* but with the same direction as the original vector.