Advertisement
Nikolay_Kashev

Distance between Points

May 27th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _04._Distance_Between_Points
  5. {
  6.     class Point
  7.     {
  8.         public int X { get; set; }
  9.         public int Y { get; set; }
  10.     }
  11.  
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             // Reads both points separately
  17.             Point p1 = ReadPoint();
  18.             Point p2 = ReadPoint();
  19.  
  20.             // Calculate the distance between them
  21.             double distance = CalcDistance(p1, p2);
  22.  
  23.             // Print the distance
  24.             Console.WriteLine("{0:f3}", distance);
  25.         }
  26.  
  27.  
  28.         static double CalcDistance(Point p1, Point p2)
  29.         {
  30.             int deltaX = p2.X - p1.X;
  31.             int deltaY = p2.Y - p1.Y;
  32.             return Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
  33.         }
  34.  
  35.         static Point ReadPoint()
  36.         {
  37.             int[] pointInfo = Console.ReadLine().Split()
  38.                .Select(int.Parse).ToArray();
  39.             Point point = new Point();
  40.             point.X = pointInfo[0];
  41.             point.Y = pointInfo[1];
  42.             return point;
  43.         }
  44.  
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement