Euclidean Distance

Main.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(
  28. "Minimum distance: {0}",
  29. points.ElementAt(distances.IndexOf(distances.Min())).ToPoint());
  30. Console.WriteLine(
  31. "Maximum distance: {0}",
  32. points.ElementAt(distances.IndexOf(distances.Max())).ToPoint());
  33. }
  34. /// <summary>
  35. /// Initials the data points.
  36. /// </summary>
  37. /// <returns>
  38. /// The data points.
  39. /// </returns>
  40. private static IEnumerable<IEnumerable<double>> InitialDataPoints()
  41. {
  42. var points = new List<IEnumerable<double>>();
  43. Console.WriteLine("Please enter the initial points");
  44. while (true)
  45. {
  46. Console.Write("Point [blank to stop]: ");
  47. var p = Console.ReadLine().ToPoint();
  48. if (p == null)
  49. {
  50. if (points.Count() > 0)
  51. {
  52. break;
  53. }
  54. else
  55. {
  56. Console.WriteLine("No points have been entered.");
  57. }
  58. }
  59. else if (points.Count() > 0 && p.Count() != points.First().Count())
  60. {
  61. Console.WriteLine("Dimensions do not match.");
  62. }
  63. else
  64. {
  65. points.Add(p);
  66. }
  67. }
  68. return points;
  69. }
  70. /// <summary>
  71. /// Focals the point.
  72. /// </summary>
  73. /// <returns>
  74. /// The point.
  75. /// </returns>
  76. /// <param name='points'>
  77. /// Points.
  78. /// </param>
  79. private static IEnumerable<double> FocalPoint(IEnumerable<IEnumerable<double>> points)
  80. {
  81. Console.WriteLine();
  82. Console.WriteLine("Please enter a focal point");
  83. while (true)
  84. {
  85. Console.Write("Point: ");
  86. var p = Console.ReadLine().ToPoint();
  87. if (p == null)
  88. {
  89. Console.WriteLine("Invalid input. Please try again.");
  90. }
  91. else if (p.Count() != points.First().Count())
  92. {
  93. Console.WriteLine("Dimensions do not match.");
  94. }
  95. else
  96. {
  97. return p;
  98. }
  99. }
  100. }
  101. }
  102. }