simeon3000

09. Rectangle Intersection

Feb 15th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         var rectangles = new Dictionary<string, Rectangle>();
  10.  
  11.         int[] inputNums = Console.ReadLine().Split().Select(int.Parse).ToArray();
  12.  
  13.         AddRectangles(rectangles, inputNums[0]);
  14.  
  15.         CheckRectangles(rectangles, inputNums[1]);
  16.  
  17.     }
  18.  
  19.     private static void CheckRectangles(Dictionary<string, Rectangle> rectangles, int m)
  20.     {
  21.  
  22.         for (int i = 0; i < m; i++)
  23.         {
  24.             string[] outputArgs = Console.ReadLine().Split();
  25.             string firstId = outputArgs[0];
  26.             string secondId = outputArgs[1];
  27.            
  28.             Console.WriteLine($"{Rectangle.rectOverlap(rectangles[firstId], rectangles[secondId])}".ToLower());
  29.         }
  30.     }
  31.  
  32.     private static void AddRectangles(Dictionary<string, Rectangle> rectangles, int n)
  33.     {
  34.  
  35.         for (int i = 0; i < n; i++)
  36.         {
  37.             string[] rectangleArgs = Console.ReadLine().Split();
  38.             string Id = rectangleArgs[0];
  39.             double width = double.Parse(rectangleArgs[1]);
  40.             double height = double.Parse(rectangleArgs[2]);
  41.             double x = double.Parse(rectangleArgs[3]);
  42.             double y = double.Parse(rectangleArgs[4]);
  43.  
  44.             Rectangle rectangle = new Rectangle(x, y, width, height);
  45.             rectangles[Id] = rectangle;
  46.         }
  47.     }
  48. }
  49.  
  50. //////////////////
  51.  
  52. public class Rectangle
  53. {
  54.     double x;
  55.     double y;
  56.     double width;
  57.     double height;
  58.  
  59.     public Rectangle(double x, double y, double width, double height)
  60.     {
  61.         this.x = x;
  62.         this.y = y;
  63.         this.width = width;
  64.         this.height = height;
  65.     }
  66.  
  67.     private static bool valueInRange(double value, double min, double max)
  68.     { return (value >= min) && (value <= max); }
  69.  
  70.     public static bool rectOverlap(Rectangle A, Rectangle B)
  71.     {
  72.         bool xOverlap = valueInRange(A.x, B.x, B.x + B.width) ||
  73.                         valueInRange(B.x, A.x, A.x + A.width);
  74.  
  75.         bool yOverlap = valueInRange(A.y, B.y, B.y + B.height) ||
  76.                         valueInRange(B.y, A.y, A.y + A.height);
  77.  
  78.         return xOverlap && yOverlap;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment