Advertisement
Prohause

Closest of two point

Oct 1st, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int numberOfInputs = int.Parse(Console.ReadLine());
  11. Point[] points = new Point[numberOfInputs];
  12.  
  13. for (int i = 0; i < numberOfInputs; i++)
  14. {
  15. var point = new Point();
  16.  
  17. int[] pointCoordinates = Console.ReadLine()
  18. .Split(' ')
  19. .Select(int.Parse)
  20. .ToArray();
  21.  
  22. point.X = pointCoordinates[0];
  23. point.Y = pointCoordinates[1];
  24.  
  25. points[i] = point;
  26. }
  27.  
  28. //double closestDistance = double.MaxValue;
  29. double lowestDistance = 0.0;
  30. var listOfFinalPoints = new List<Point>();
  31.  
  32. for (int indexOfFirst = 0; indexOfFirst < numberOfInputs; indexOfFirst++)
  33. {
  34. var currentFirstPoint = points[indexOfFirst];
  35.  
  36. for (int indexOfSecond = 1; indexOfSecond < numberOfInputs; indexOfSecond++)
  37. {
  38. bool sameIndex = indexOfFirst == indexOfSecond;
  39. if (sameIndex == true)
  40. {
  41. continue;
  42. }
  43.  
  44. var currentSecondPoint = points[indexOfSecond];
  45.  
  46. lowestDistance = DistanceCalc(currentFirstPoint, currentSecondPoint, listOfFinalPoints, lowestDistance, indexOfFirst, indexOfSecond);
  47. }
  48. }
  49.  
  50. lowestDistance = Math.Round(lowestDistance, 3);
  51. Console.WriteLine($"{lowestDistance:f3}");
  52. foreach (var item in listOfFinalPoints)
  53. {
  54. Console.WriteLine($"({item.X}, {item.Y})");
  55. }
  56.  
  57. // Environment.Exit(0);
  58. }
  59. static double DistanceCalc(Point currentFirstPoint, Point currentSecondPoint, List<Point> listOfFinalPoints, double lowestDistance, int indexOfFirst, int indexOfSecond)
  60. {
  61.  
  62. int xDiff = currentFirstPoint.X - currentSecondPoint.X;
  63. int yDiff = currentFirstPoint.Y - currentSecondPoint.Y;
  64. double sumOfDiffs = Math.Pow(xDiff, 2) + Math.Pow(yDiff, 2);
  65. double result = Math.Sqrt(sumOfDiffs);
  66. bool firstResult = indexOfFirst == 0 && indexOfSecond == 1;
  67. if (firstResult == true)
  68. {
  69. listOfFinalPoints.Add(currentFirstPoint);
  70. listOfFinalPoints.Add(currentSecondPoint);
  71. return lowestDistance = result;
  72. listOfFinalPoints.Add(currentSecondPoint);
  73. }
  74. else
  75. {
  76.  
  77. bool newLowestDistance = result < lowestDistance;
  78. if (newLowestDistance == true)
  79. {
  80. listOfFinalPoints.Clear();
  81.  
  82. listOfFinalPoints.Add(currentFirstPoint);
  83. listOfFinalPoints.Add(currentSecondPoint);
  84. return lowestDistance = result;
  85. }
  86. else
  87. {
  88. return lowestDistance;
  89. }
  90. }
  91.  
  92. }
  93. }
  94.  
  95.  
  96. public class Point
  97. {
  98. public int X { get; set; }
  99.  
  100. public int Y { get; set; }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement