Advertisement
MyOnAsSalat

Untitled

Mar 29th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Rectangles
  4. {
  5.     public static class RectanglesTask
  6.     {
  7.         public static Rectangle Left_r, Right_r, Top_r, Bottom_r;
  8.         // Пересекаются ли два прямоугольника (пересечение только по границе также считается пересечением)
  9.         public static bool AreIntersected(Rectangle r1, Rectangle r2)
  10.         {
  11.             PositionOfRectangles(r1,r2);
  12.             return !(Left_r.Top > Right_r.Bottom || Left_r.Bottom < Right_r.Top || Left_r.Right < Right_r.Left);
  13.         }
  14.         // Площадь пересечения прямоугольников
  15.         public static int IntersectionSquare(Rectangle r1, Rectangle r2)
  16.         {
  17.             int intersection_height = Top_r.Bottom - Bottom_r.Bottom > 0 ?
  18.                                       Top_r.Height - Bottom_r.Top + Top_r.Top - Top_r.Bottom + Bottom_r.Bottom :
  19.                                       Top_r.Height - Bottom_r.Top + Top_r.Top;
  20.             int intersection_width  = Left_r.Right - Right_r.Right > 0 ?
  21.                                       Left_r.Width - Right_r.Left + Left_r.Left - Left_r.Right + Right_r.Right :
  22.                                       Left_r.Width - Right_r.Left + Left_r.Left;
  23.  
  24.             if (intersection_height > 0 && intersection_width > 0) return intersection_height * intersection_width;
  25.             return 0;
  26.         }
  27.         // Если один из прямоугольников целиком находится внутри другого — вернуть номер (с нуля) внутреннего.
  28.         // Иначе вернуть -1
  29.         // Если прямоугольники совпадают, можно вернуть номер любого из них.
  30.         public static int IndexOfInnerRectangle(Rectangle r1, Rectangle r2)
  31.         {
  32.             if (CheckForAccessories(r1, r2)) return 1;
  33.             if (CheckForAccessories(r2, r1)) return 0;
  34.             return -1;
  35.         }
  36.  
  37.         public static void PositionOfRectangles(Rectangle r1, Rectangle r2)
  38.         {
  39.             if (r1.Left <= r2.Left) { Left_r = r1; Right_r = r2; }
  40.             else { Left_r = r2; Right_r = r1; }
  41.  
  42.             if (r1.Top <= r2.Top) { Top_r = r1; Bottom_r = r2; }
  43.             else { Top_r = r2; Bottom_r = r1; }
  44.         }
  45.  
  46.         public static bool CheckForAccessories(Rectangle r1, Rectangle r2)
  47.         {
  48.             return (r1.Left <= r2.Left && r2.Left <= r1.Right) &&
  49.                    (r1.Right >= r2.Right) &&
  50.                    (r1.Top <= r2.Top && r1.Bottom >= r2.Top) &&
  51.                    (r1.Bottom >= r2.Bottom);
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement