Advertisement
Guest User

Closest Two Points

a guest
Jun 30th, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleApp2
  6. {
  7.     class Program
  8.     {
  9.         public class point
  10.         {
  11.             public double x;
  12.             public double y;
  13.             public void read()
  14.             {
  15.                 string[] input = Console.ReadLine().Split(' ').ToArray();
  16.                 this.x = double.Parse(input[0]);
  17.                 this.y = double.Parse(input[1]);
  18.             }
  19.         }
  20.         static double dist(point a, point b)
  21.         {
  22.             double x1 = a.x, x2=b.x, y1=a.y, y2=b.y;
  23.             double dist = (x1-x2)*(x1-x2) +(y1-y2)*(y1-y2);
  24.             return ((int)(Math.Sqrt(dist)*10000))/10000.0;
  25.         }
  26.         static void Main(string[] args)
  27.         {
  28.             int x = int.Parse(Console.ReadLine());
  29.             point p1 = new point();
  30.             point p2 = new point();
  31.             List<point> points = new List<point>();
  32.             while (x!=0)
  33.             {
  34.                 p1.read();
  35.                 points.Add(p1);
  36.                 x--;
  37.             }
  38.             x = points.Count;
  39.             double minDist = double.MaxValue;
  40.             for(int i=0; i<x; i++)
  41.             {
  42.                 for(int j=i+1; j<x; j++)
  43.                 {
  44.                     double d = dist(points[i], points[j]);
  45.                     if (d<minDist)
  46.                     {
  47.                         minDist = d;
  48.                         p1 = points[i];
  49.                         p2 = points[j];
  50.                     }
  51.                 }
  52.             }
  53.             Console.WriteLine("{0:f3}\n{1} {2}\n{3} {4}", minDist, p1.x, p1.y, p2.x, p2.y);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement