freeeedomdive

Untitled

Jul 28th, 2021 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Rectangles
  4. {
  5.     public static class RectanglesTask
  6.     {
  7.         public static bool AreIntersected(Rectangle r1, Rectangle r2)
  8.         {
  9.             var leftFirst = (r1.Left <= r2.Left) && (r1.Right >= r2.Left);
  10.             var leftSecond = (r2.Left <= r1.Left) && (r2.Right >= r1.Left);
  11.             var rightFirst = (r1.Top <= r2.Top) && (r1.Bottom >= r2.Top);
  12.             var rightSecond = (r2.Top <= r1.Top) && (r2.Bottom >= r1.Top);
  13.  
  14.             return (leftFirst || leftSecond) && (rightFirst || rightSecond);
  15.         }
  16.  
  17.         public static int IntersectionSquare(Rectangle r1, Rectangle r2)
  18.         {
  19.             if (!AreIntersected(r1, r2))
  20.                 return 0;
  21.  
  22.             var width = Math.Max(r1.Left, r2.Left) - Math.Min(r1.Right, r2.Right);
  23.             var height = Math.Max(r1.Top, r2.Top) - Math.Min(r1.Bottom, r2.Bottom);
  24.             return width * height;
  25.         }
  26.  
  27.         public static int IndexOfInnerRectangle(Rectangle r1, Rectangle r2)
  28.         {
  29.             if (IsInner(r1, r2))
  30.                 return 0;
  31.  
  32.             return IsInner(r2, r1) ? 1 : -1;
  33.         }
  34.  
  35.         static bool IsInner(Rectangle r1, Rectangle r2)
  36.         {
  37.             return (r1.Left >= r2.Left) && (r1.Right <= r2.Right)
  38.                 && (r1.Top >= r2.Top) && (r1.Bottom <= r2.Bottom);
  39.         }
  40.     }
  41. }
Add Comment
Please, Sign In to add comment