using System; using System.Collections.Generic; using System.Linq; namespace EuclideanDistance { /// /// Double extensions. /// public static class DoubleExtensions { /// /// Euclideans the distance. /// /// /// The distance. /// /// /// Point a. /// /// /// Point b. /// public static double EuclideanDistance(this IEnumerable pointA, IEnumerable pointB) { // Make sure the dimensions are the same. if (pointA.Count() != pointB.Count()) { throw new ArgumentOutOfRangeException("Dimensions do not match"); } // Iterate through each point and create the vector. var vectors = new List(); for (int i = 0; i < pointA.Count(); i++) { vectors.Add(Math.Pow(Math.Abs(pointA.ElementAt(i) - pointB.ElementAt(i)), 2)); } // Return the sqare root of the sum of vectors. return Math.Sqrt(vectors.Sum()); } /// /// Tos the point. /// /// /// The point. /// /// /// Point. /// public static string ToPoint(this IEnumerable point) { return string.Format("({0})", string.Join(",", point)); } } }