simonradev

Closest Two Points

Nov 26th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System.Linq;
  2. using System.Collections.Generic;
  3. using System;
  4.  
  5. namespace ClosestTwoPoints
  6. {
  7.     class Program
  8.     {
  9.         static int numberOfPoints;
  10.         static int firstPointIndex;
  11.         static int secondPointIndex;
  12.        
  13.         public static void Main(string[] args)
  14.         {
  15.             numberOfPoints = int.Parse(Console.ReadLine());
  16.            
  17.             Point[] allPoints = ReadPoints();
  18.            
  19.             double smallestDistance = FindTheSmallestDistance(allPoints);
  20.            
  21.             PrintResult(smallestDistance, allPoints);
  22.         }
  23.        
  24.         static void PrintResult(double smallestDistance, Point[] allPoints)
  25.         {
  26.             Console.WriteLine("{0:f3}\r\n", smallestDistance);
  27.            
  28.             Console.WriteLine("({0} {1})", allPoints[firstPointIndex].X, allPoints[firstPointIndex].Y);
  29.             Console.WriteLine("({0} {1})", allPoints[secondPointIndex].X, allPoints[secondPointIndex].Y);
  30.         }
  31.        
  32.         static double FindTheSmallestDistance(Point[] allPoints)
  33.         {
  34.             double smallestDistance = double.MaxValue;
  35.            
  36.             for (int currentPoint = 0; currentPoint < allPoints.Length; currentPoint++)
  37.             {
  38.                 for (int currentCheck = currentPoint + 1; currentCheck < allPoints.Length; currentCheck++)
  39.                 {
  40.                     double firstSide = allPoints[currentPoint].X - allPoints[currentCheck].X;
  41.                     double secondSide = allPoints[currentPoint].Y - allPoints[currentCheck].Y;
  42.                    
  43.                     double distance = CalculateTheDistance(firstSide, secondSide);
  44.                    
  45.                     if (distance < smallestDistance)
  46.                     {
  47.                         smallestDistance = distance;
  48.                         firstPointIndex = currentPoint;
  49.                         secondPointIndex = currentCheck;
  50.                     }
  51.                 }
  52.             }
  53.            
  54.             return smallestDistance;
  55.         }
  56.        
  57.         static double CalculateTheDistance(double firstSide, double secondSide)
  58.         {
  59.             return Math.Sqrt(firstSide * firstSide + secondSide * secondSide);
  60.         }
  61.        
  62.         static Point[] ReadPoints()
  63.         {
  64.             Point[] points = new Point[numberOfPoints];
  65.            
  66.             for (int currentInput = 0; currentInput < numberOfPoints; currentInput++)
  67.             {
  68.                 int[] currentPoints = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  69.                
  70.                 points[currentInput] = new Point()
  71.                 {
  72.                     X = currentPoints[0],
  73.                     Y = currentPoints[1]
  74.                 };
  75.             }
  76.            
  77.             return points;
  78.         }
  79.     }
  80.    
  81.     class Point
  82.     {
  83.         public int X { get; set; }
  84.        
  85.         public int Y { get; set; }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment