Euclidean Distance

Main.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace EuclideanDistance
  5. {
  6. /// <summary>
  7. /// Main class.
  8. /// </summary>
  9. public class MainClass
  10. {
  11. /// <summary>
  12. /// The entry point of the program, where the program control starts and ends.
  13. /// </summary>
  14. /// <param name='args'>
  15. /// The command-line arguments.
  16. /// </param>
  17. public static void Main(string[] args)
  18. {
  19. // Accumalate the intial data points needed before computation.
  20. var points = InitialDataPoints();
  21. // Retrieve the focal point to determine our distances.
  22. var input = FocalPoint(points);
  23. // Store all the distances.
  24. var distances = points.Select(x => input.EuclideanDistance(x)).ToList();
  25. // Display both the closest and furthest data points from the focal point.
  26. Console.WriteLine();
  27. Console.WriteLine("Minimum distance: {0}", points.ElementAt(distances.IndexOf(distances.Min())).ToPoint());
  28. Console.WriteLine("Maximum distance: {0}", points.ElementAt(distances.IndexOf(distances.Max())).ToPoint());
  29. }
  30. /// <summary>
  31. /// Initials the data points.
  32. /// </summary>
  33. /// <returns>
  34. /// The data points.
  35. /// </returns>
  36. private static IEnumerable<IEnumerable<double>> InitialDataPoints()
  37. {
  38. var points = new List<IEnumerable<double>>();
  39. Console.WriteLine("Please enter the initial points");
  40. while (true)
  41. {
  42. Console.Write("Point [blank to stop]: ");
  43. var p = Console.ReadLine().ToPoint();
  44. if (p == null)
  45. {
  46. if (points.Count() > 0)
  47. {
  48. break;
  49. }
  50. else
  51. {
  52. Console.WriteLine("No points have been entered.");
  53. }
  54. }
  55. else if (points.Count() > 0 && p.Count() != points.First().Count())
  56. {
  57. Console.WriteLine("Dimensions do not match.");
  58. }
  59. else
  60. {
  61. points.Add(p);
  62. }
  63. }
  64. return points;
  65. }
  66. /// <summary>
  67. /// Focals the point.
  68. /// </summary>
  69. /// <returns>
  70. /// The point.
  71. /// </returns>
  72. /// <param name='points'>
  73. /// Points.
  74. /// </param>
  75. private static IEnumerable<double> FocalPoint(IEnumerable<IEnumerable<double>> points)
  76. {
  77. Console.WriteLine();
  78. Console.WriteLine("Please enter a focal point");
  79. while (true)
  80. {
  81. Console.Write("Point: ");
  82. var p = Console.ReadLine().ToPoint();
  83. if (p == null)
  84. {
  85. Console.WriteLine("Invalid input. Please try again.");
  86. }
  87. else if (p.Count() != points.First().Count())
  88. {
  89. Console.WriteLine("Dimensions do not match.");
  90. }
  91. else
  92. {
  93. return p;
  94. }
  95. }
  96. }
  97. }
  98. }