Guest User

Untitled

a guest
May 31st, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 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.  
  17.             #region Fields
  18.  
  19.             public double X { get; }
  20.             public double Y { get; }
  21.  
  22.             #endregion
  23.  
  24.             #region Static methods
  25.  
  26.             public static CustomPoint GetInputPoint()
  27.             {
  28.                 double x = double.Parse(Console.ReadLine());
  29.                 double y = double.Parse(Console.ReadLine());
  30.                 return new CustomPoint(x, y);
  31.             }
  32.  
  33.             #endregion
  34.  
  35.             public CustomPoint(double x = 0, double y = 0)
  36.             {
  37.                 X = x;
  38.                 Y = y;
  39.             }
  40.  
  41.             public double DistanceToPoint(CustomPoint distantPoint)
  42.             {
  43.                 double xDiff = distantPoint.X - X;
  44.                 double yDiff = distantPoint.Y - Y;
  45.                 return Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
  46.             }
  47.  
  48.             public bool IsCloserToCenterThanPoint(CustomPoint otherPoint)
  49.             {
  50.                 return this.DistanceToPoint(new CustomPoint()) <= otherPoint.DistanceToPoint(new CustomPoint());
  51.             }
  52.  
  53.             public override string ToString()
  54.             {
  55.                 return $"({this.X}, {this.Y})";
  56.             }
  57.  
  58.         }
  59.  
  60.         // Summary:
  61.         //     Represents a pair of CustomPoint that defines a line
  62.         //     in a two-dimensional plane.
  63.         public struct Line
  64.         {
  65.             #region Fields
  66.  
  67.             public CustomPoint P1 { get; }
  68.             public CustomPoint P2 { get; }
  69.  
  70.             #endregion
  71.  
  72.             #region Static methods
  73.  
  74.             public static Line GetInputLine()
  75.             {
  76.                 return new Line(CustomPoint.GetInputPoint(), CustomPoint.GetInputPoint());
  77.             }
  78.  
  79.             public static Line GetTheLongerOfTwoLines(Line line1, Line line2)
  80.             {
  81.                 return line1.IsLongerThanLine(line2) ? line1 : line2;
  82.             }
  83.  
  84.             #endregion
  85.  
  86.             public Line(CustomPoint p1 = new CustomPoint(), CustomPoint p2 = new CustomPoint())
  87.             {
  88.                 P1 = p1;
  89.                 P2 = p2;
  90.             }
  91.  
  92.             public double LineLength
  93.             {
  94.                 get
  95.                 {
  96.                     return this.P1.DistanceToPoint(this.P2);
  97.                 }
  98.             }
  99.  
  100.             public bool IsLongerThanLine(Line otherLine)
  101.             {
  102.                 return this.LineLength >= otherLine.LineLength;
  103.             }
  104.  
  105.             public void GetCloseAndFarFromCenterPoints(out CustomPoint closeToCenterPoint, out CustomPoint farFromCenterPoint)
  106.             {
  107.                 if (this.P1.IsCloserToCenterThanPoint(this.P2))
  108.                 {
  109.                     closeToCenterPoint = this.P1;
  110.                     farFromCenterPoint = this.P2;
  111.                 }
  112.                 else
  113.                 {
  114.                     closeToCenterPoint = this.P2;
  115.                     farFromCenterPoint = this.P1;
  116.                 }
  117.             }
  118.  
  119.             public void Print()
  120.             {
  121.                 CustomPoint farFromCenterLinePoint;
  122.                 CustomPoint closerToCenterLinePoint;
  123.                 this.GetCloseAndFarFromCenterPoints(out closerToCenterLinePoint, out farFromCenterLinePoint);
  124.  
  125.                 Console.Write(closerToCenterLinePoint.ToString());
  126.                 Console.WriteLine(farFromCenterLinePoint.ToString());
  127.             }
  128.  
  129.         }
  130.  
  131.         #endregion
  132.  
  133.         static void Main(string[] args)
  134.         {
  135.             {
  136.                 var line1 = Line.GetInputLine();
  137.                 var line2 = Line.GetInputLine();
  138.                 var longerLine = Line.GetTheLongerOfTwoLines(line1, line2);
  139.                 longerLine.Print();
  140.             }
  141.         }
  142.  
  143.     }
  144. }
Add Comment
Please, Sign In to add comment