Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Bounding
  4. {
  5.     class Program
  6.     {
  7.         struct Point
  8.         {
  9.             public int X { get; set; }
  10.             public int Y { get; set; }
  11.         }
  12.         struct BoundingBox
  13.         {
  14.             public Point? LowerLeft { get; private set; }
  15.             public Point? UpperRight { get; private set; }
  16.             public static BoundingBox FromCircle(int x, int y, int radius)
  17.             {
  18.                 return new BoundingBox
  19.                 {
  20.                     LowerLeft = new Point { X = x - radius, Y = y - radius },
  21.                     UpperRight = new Point { X = x + radius, Y = y + radius }
  22.                 };
  23.             }
  24.             public static BoundingBox FromPoint(int x, int y)
  25.             {
  26.                 return new BoundingBox
  27.                 {
  28.                     LowerLeft = new Point { X = x, Y = y },
  29.                     UpperRight = new Point { X = x, Y = y }
  30.                 };
  31.             }
  32.             public static BoundingBox FromLine(int x1, int y1, int x2, int y2)
  33.             {
  34.                 return FromLine(new Point { X = x1, Y = y1 }, new Point { X = x2, Y = y2 });
  35.             }
  36.             public static BoundingBox FromLine(Point first, Point second)
  37.             {
  38.                 return new BoundingBox
  39.                 {
  40.                     LowerLeft = new Point { X = Math.Min(first.X, second.X), Y = Math.Min(first.Y, second.Y) },
  41.                     UpperRight = new Point { X = Math.Max(first.X, second.X), Y = Math.Max(first.Y, second.Y) }
  42.                 };
  43.             }
  44.             public void Combine(BoundingBox boundingBox)
  45.             {
  46.                 if (!boundingBox.LowerLeft.HasValue || !boundingBox.UpperRight.HasValue)
  47.                 {
  48.                     return;
  49.                 }
  50.                 else if (!LowerLeft.HasValue || !UpperRight.HasValue)
  51.                 {
  52.                     LowerLeft = boundingBox.LowerLeft;
  53.                     UpperRight = boundingBox.UpperRight;
  54.                 }
  55.                 LowerLeft = new Point
  56.                 {
  57.                     X = Math.Min(LowerLeft.Value.X, boundingBox.LowerLeft.Value.X),
  58.                     Y = Math.Min(LowerLeft.Value.Y, boundingBox.LowerLeft.Value.Y)
  59.                 };
  60.                 UpperRight = new Point
  61.                 {
  62.                     X = Math.Max(UpperRight.Value.X, boundingBox.UpperRight.Value.X),
  63.                     Y = Math.Max(UpperRight.Value.Y, boundingBox.UpperRight.Value.Y)
  64.                 };
  65.             }
  66.         }
  67.  
  68.         static void ProcessInput(string input, ref BoundingBox boundingBox)
  69.         {
  70.             string[] shape = input.Split(' ');
  71.             switch (shape[0])
  72.             {
  73.                 case "p":
  74.                     boundingBox.Combine(BoundingBox.FromPoint(int.Parse(shape[1]), int.Parse(shape[2])));
  75.                     break;
  76.                 case "c":
  77.                     boundingBox.Combine(BoundingBox.FromCircle(int.Parse(shape[1]), int.Parse(shape[2]), int.Parse(shape[3])));
  78.                     break;
  79.                 case "l":
  80.                     boundingBox.Combine(BoundingBox.FromLine(int.Parse(shape[1]), int.Parse(shape[2]), int.Parse(shape[3]), int.Parse(shape[4])));
  81.                     break;
  82.             }
  83.         }
  84.  
  85.         static void Main(string[] args)
  86.         {
  87.             var numCasesLeft = int.Parse(Console.ReadLine());
  88.             var boundingBoxes = new BoundingBox[numCasesLeft];
  89.             while(numCasesLeft-- > 0)
  90.             {
  91.                 var numShapesLeft = int.Parse(Console.ReadLine());
  92.                 while (numShapesLeft-- > 0)
  93.                 {
  94.                     ProcessInput(Console.ReadLine(), ref boundingBoxes[boundingBoxes.Length - numCasesLeft - 1]);
  95.                 }
  96.                 Console.ReadLine();
  97.             }
  98.             foreach (var boundingBox in boundingBoxes)
  99.             {
  100.                 Console.WriteLine($"{boundingBox.LowerLeft.Value.X} {boundingBox.LowerLeft.Value.Y} {boundingBox.UpperRight.Value.X} {boundingBox.UpperRight.Value.Y}");
  101.             }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement