Last active
July 3, 2018 00:24
-
-
Save b-cancel/a5be2f600f82950e33f54844903a6077 to your computer and use it in GitHub Desktop.
UNITY 3D => Calculate Euclidean Distance for N-Dimensional Space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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