Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5.  
  6. public class RectangleIntersection
  7. {
  8.     static void Main()
  9.     {
  10.         var input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse)
  11.             .ToArray();
  12.         int numberOfRectangles = input[0];
  13.         int intersectionChecks = input[1];
  14.  
  15.         var rectangles = new List<Rectangles>();
  16.  
  17.         //Adding all Rectangles in a List
  18.         for (int i = 0; i < numberOfRectangles; i++)
  19.         {
  20.             var rectangleInfo = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  21.  
  22.             rectangles.Add(new Rectangles(
  23.                 rectangleInfo[0],
  24.                 int.Parse(rectangleInfo[1]),
  25.                 int.Parse(rectangleInfo[2]),
  26.                 int.Parse(rectangleInfo[3]),
  27.                 int.Parse(rectangleInfo[4])
  28.                 ));
  29.         }
  30.  
  31.         // Taking all intersection checks
  32.         for (int i = 0; i < intersectionChecks; i++)
  33.         {
  34.             var rectangleIds = Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  35.             // checking the List of rectangles, and taking only these which ID is contained in the Array of "IDs" we want to check for each iteration
  36.             bool isIntersect = CheckIfRectanglesIntersect(rectangles.Where(r => rectangleIds.Contains(r.Id)).ToArray());
  37.             Console.WriteLine(isIntersect.ToString().ToLower());
  38.  
  39.         }
  40.     }
  41.  
  42.     private static bool CheckIfRectanglesIntersect(params Rectangles[] rectangles)
  43.     {
  44.         bool intersect = true;
  45.  
  46.         //checking if we have more then to rectangles
  47.         if (rectangles.Length > 1)
  48.         {
  49.             // starting from 1 so I can take every time the 1 behind
  50.             for (int i = 1; i < rectangles.Length; i++)
  51.             {
  52.                
  53.                 Rectangle firstRec = new Rectangle(rectangles[i].CorX, rectangles[i].CorY, rectangles[i].Width, rectangles[i].Height);
  54.                 Rectangle secRec = new Rectangle(rectangles[i - 1].CorX, rectangles[i - 1].CorY, rectangles[i - 1].Width, rectangles[i - 1].Height);
  55.                 var checkIfIntersect = Rectangle.Intersect(firstRec, secRec);
  56.  
  57.                 // Integrated Method which checks the Intersect condition
  58.                 if (!firstRec.IntersectsWith(secRec))
  59.                 {
  60.                     intersect = false;
  61.                     break;
  62.                 }
  63.  
  64.                 // I saw this check in StackOverFlow, in certain condition it returns empty rectangle which means False  
  65.                 if (checkIfIntersect == Rectangle.Empty)
  66.                 {
  67.                     intersect = false;
  68.                     break;
  69.                 }
  70.             }
  71.         }
  72.         // if we have just one rectangle to check is "intersected with itself". I tried to put throw new Exception here, but in Judge nothing changed
  73.         if (rectangles.Length == 1)
  74.         {
  75.             return true;
  76.         }
  77.  
  78.         return intersect;
  79.     }
  80. }
  81.  
  82. public class Rectangles
  83. {
  84.     private string id;
  85.     private int width;
  86.     private int height;
  87.     private int corX;
  88.     private int corY;
  89.  
  90.     public Rectangles(string id, int width, int height, int corX, int corY)
  91.     {
  92.         this.id = id;
  93.         this.width = width;
  94.         this.height = height;
  95.         this.corX = corX;
  96.         this.corY = corY;
  97.     }
  98.  
  99.     public string Id
  100.     {
  101.         get { return id; }
  102.         set { id = value; }
  103.     }
  104.  
  105.     public int Width
  106.     {
  107.         get { return width; }
  108.         set { width = value; }
  109.     }
  110.  
  111.     public int Height
  112.     {
  113.         get { return height; }
  114.         set { height = value; }
  115.     }
  116.  
  117.     public int CorX
  118.     {
  119.         get { return corX; }
  120.         set { corX = value; }
  121.     }
  122.  
  123.     public int CorY
  124.     {
  125.         get { return corY; }
  126.         set { corY = value; }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement