Advertisement
Guest User

Untitled

a guest
May 20th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System;
  2.  
  3. namespace LongerLine
  4. {
  5.     class Program
  6.     {
  7.  
  8.         #region Helper Classes
  9.  
  10.         //
  11.         // Summary:
  12.         //     Represents a CustomPoint that has Point.X and Point.Y
  13.         //     values set to zero.
  14.         public struct CustomPoint
  15.         {
  16.             public double X { get; }
  17.             public double Y { get; }
  18.  
  19.             public CustomPoint(double x = 0, double y = 0)
  20.             {
  21.                 X = x;
  22.                 Y = y;
  23.             }
  24.  
  25.             public double DistanceToPoint(CustomPoint distantPoint)
  26.             {
  27.                 double xDiff = distantPoint.X - X;
  28.                 double yDiff = distantPoint.Y - Y;
  29.                 return Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
  30.             }
  31.            
  32.             public bool IsCloserToCenterThanPoint(CustomPoint otherPoint)
  33.             {
  34.                 return this.DistanceToPoint(new CustomPoint()) <= otherPoint.DistanceToPoint(new CustomPoint());
  35.             }
  36.  
  37.             public override string ToString()
  38.             {
  39.                 return $"({this.X}, {this.Y})";
  40.             }
  41.  
  42.         }
  43.  
  44.         // Summary:
  45.         //     Represents a pair of CustomPoint that defines a line
  46.         //     in a two-dimensional plane.
  47.         public struct Line
  48.         {
  49.             public CustomPoint P1 { get; }
  50.             public CustomPoint P2 { get; }
  51.             public Line(CustomPoint p1 = new CustomPoint(), CustomPoint p2 = new CustomPoint())
  52.             {
  53.                 P1 = p1;
  54.                 P2 = p2;
  55.             }
  56.         }
  57.  
  58.         #endregion
  59.  
  60.         static void Main(string[] args)
  61.         {
  62.             {
  63.                 var line1 = GetInputLine();
  64.                 var line2 = GetInputLine();
  65.                 var longerLine = LongerOfTwoLines(line1, line2);
  66.                 PrintLine(longerLine);
  67.             }
  68.         }
  69.  
  70.         #region Tools
  71.  
  72.         static CustomPoint GetInputPoint()
  73.         {
  74.             double x = double.Parse(Console.ReadLine());
  75.             double y = double.Parse(Console.ReadLine());
  76.             return new CustomPoint(x, y);
  77.         }
  78.  
  79.         static Line GetInputLine()
  80.         {
  81.             return new Line(GetInputPoint(), GetInputPoint());
  82.         }
  83.  
  84.         static Line LongerOfTwoLines(Line line1, Line line2)
  85.         {
  86.             return line1.P1.DistanceToPoint(line1.P2) >= line2.P1.DistanceToPoint(line2.P2) ? line1 : line2;
  87.         }
  88.  
  89.         static void PrintLine(Line line)
  90.         {
  91.             CustomPoint farFromCenterLinePoint;
  92.             CustomPoint closerToCenterLinePoint = GetCloserToCenterLinePoint(line, out farFromCenterLinePoint);
  93.  
  94.             Console.Write(closerToCenterLinePoint.ToString());
  95.             Console.WriteLine(farFromCenterLinePoint.ToString());
  96.         }
  97.  
  98.         static CustomPoint GetCloserToCenterLinePoint(Line line, out CustomPoint farFromCenterLinePoint)
  99.         {
  100.             if (line.P1.IsCloserToCenterThanPoint(line.P2))
  101.             {
  102.                 farFromCenterLinePoint = line.P2;
  103.                 return line.P1;
  104.             }
  105.             farFromCenterLinePoint = line.P1;
  106.             return line.P2;
  107.         }
  108.  
  109.         #endregion
  110.  
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement