Skip to content

Instantly share code, notes, and snippets.

@b-cancel
Last active July 3, 2018 00:24
Show Gist options
  • Save b-cancel/a5be2f600f82950e33f54844903a6077 to your computer and use it in GitHub Desktop.
Save b-cancel/a5be2f600f82950e33f54844903a6077 to your computer and use it in GitHub Desktop.
UNITY 3D => Calculate Euclidean Distance for N-Dimensional Space
static float euclideanDistance(float[] pointA, float[] pointB)
{
if (pointA.Length == pointB.Length)
{
float sum = 0;
for (int dim = 0; dim < pointA.Length; dim++)
sum += Mathf.Pow((pointA[dim] - pointB[dim]), 2);
return Mathf.Sqrt(sum); //distance is always positive
}
else
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment