Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using System.Collections.Generic;
- using System;
- namespace ClosestTwoPoints
- {
- class Program
- {
- static int numberOfPoints;
- static int firstPointIndex;
- static int secondPointIndex;
- public static void Main(string[] args)
- {
- numberOfPoints = int.Parse(Console.ReadLine());
- Point[] allPoints = ReadPoints();
- double smallestDistance = FindTheSmallestDistance(allPoints);
- PrintResult(smallestDistance, allPoints);
- }
- static void PrintResult(double smallestDistance, Point[] allPoints)
- {
- Console.WriteLine("{0:f3}\r\n", smallestDistance);
- Console.WriteLine("({0} {1})", allPoints[firstPointIndex].X, allPoints[firstPointIndex].Y);
- Console.WriteLine("({0} {1})", allPoints[secondPointIndex].X, allPoints[secondPointIndex].Y);
- }
- static double FindTheSmallestDistance(Point[] allPoints)
- {
- double smallestDistance = double.MaxValue;
- for (int currentPoint = 0; currentPoint < allPoints.Length; currentPoint++)
- {
- for (int currentCheck = currentPoint + 1; currentCheck < allPoints.Length; currentCheck++)
- {
- double firstSide = allPoints[currentPoint].X - allPoints[currentCheck].X;
- double secondSide = allPoints[currentPoint].Y - allPoints[currentCheck].Y;
- double distance = CalculateTheDistance(firstSide, secondSide);
- if (distance < smallestDistance)
- {
- smallestDistance = distance;
- firstPointIndex = currentPoint;
- secondPointIndex = currentCheck;
- }
- }
- }
- return smallestDistance;
- }
- static double CalculateTheDistance(double firstSide, double secondSide)
- {
- return Math.Sqrt(firstSide * firstSide + secondSide * secondSide);
- }
- static Point[] ReadPoints()
- {
- Point[] points = new Point[numberOfPoints];
- for (int currentInput = 0; currentInput < numberOfPoints; currentInput++)
- {
- int[] currentPoints = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- points[currentInput] = new Point()
- {
- X = currentPoints[0],
- Y = currentPoints[1]
- };
- }
- return points;
- }
- }
- class Point
- {
- public int X { get; set; }
- public int Y { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment