Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Bounding
- {
- class Program
- {
- struct Point
- {
- public int X { get; set; }
- public int Y { get; set; }
- }
- struct BoundingBox
- {
- public Point? LowerLeft { get; private set; }
- public Point? UpperRight { get; private set; }
- public static BoundingBox FromCircle(int x, int y, int radius)
- {
- return new BoundingBox
- {
- LowerLeft = new Point { X = x - radius, Y = y - radius },
- UpperRight = new Point { X = x + radius, Y = y + radius }
- };
- }
- public static BoundingBox FromPoint(int x, int y)
- {
- return new BoundingBox
- {
- LowerLeft = new Point { X = x, Y = y },
- UpperRight = new Point { X = x, Y = y }
- };
- }
- public static BoundingBox FromLine(int x1, int y1, int x2, int y2)
- {
- return FromLine(new Point { X = x1, Y = y1 }, new Point { X = x2, Y = y2 });
- }
- public static BoundingBox FromLine(Point first, Point second)
- {
- return new BoundingBox
- {
- LowerLeft = new Point { X = Math.Min(first.X, second.X), Y = Math.Min(first.Y, second.Y) },
- UpperRight = new Point { X = Math.Max(first.X, second.X), Y = Math.Max(first.Y, second.Y) }
- };
- }
- public void Combine(BoundingBox boundingBox)
- {
- if (!boundingBox.LowerLeft.HasValue || !boundingBox.UpperRight.HasValue)
- {
- return;
- }
- else if (!LowerLeft.HasValue || !UpperRight.HasValue)
- {
- LowerLeft = boundingBox.LowerLeft;
- UpperRight = boundingBox.UpperRight;
- }
- LowerLeft = new Point
- {
- X = Math.Min(LowerLeft.Value.X, boundingBox.LowerLeft.Value.X),
- Y = Math.Min(LowerLeft.Value.Y, boundingBox.LowerLeft.Value.Y)
- };
- UpperRight = new Point
- {
- X = Math.Max(UpperRight.Value.X, boundingBox.UpperRight.Value.X),
- Y = Math.Max(UpperRight.Value.Y, boundingBox.UpperRight.Value.Y)
- };
- }
- }
- static void ProcessInput(string input, ref BoundingBox boundingBox)
- {
- string[] shape = input.Split(' ');
- switch (shape[0])
- {
- case "p":
- boundingBox.Combine(BoundingBox.FromPoint(int.Parse(shape[1]), int.Parse(shape[2])));
- break;
- case "c":
- boundingBox.Combine(BoundingBox.FromCircle(int.Parse(shape[1]), int.Parse(shape[2]), int.Parse(shape[3])));
- break;
- case "l":
- boundingBox.Combine(BoundingBox.FromLine(int.Parse(shape[1]), int.Parse(shape[2]), int.Parse(shape[3]), int.Parse(shape[4])));
- break;
- }
- }
- static void Main(string[] args)
- {
- var numCasesLeft = int.Parse(Console.ReadLine());
- var boundingBoxes = new BoundingBox[numCasesLeft];
- while(numCasesLeft-- > 0)
- {
- var numShapesLeft = int.Parse(Console.ReadLine());
- while (numShapesLeft-- > 0)
- {
- ProcessInput(Console.ReadLine(), ref boundingBoxes[boundingBoxes.Length - numCasesLeft - 1]);
- }
- Console.ReadLine();
- }
- foreach (var boundingBox in boundingBoxes)
- {
- Console.WriteLine($"{boundingBox.LowerLeft.Value.X} {boundingBox.LowerLeft.Value.Y} {boundingBox.UpperRight.Value.X} {boundingBox.UpperRight.Value.Y}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement