Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Rectangles
  4. {
  5. public static class RectanglesTask
  6. {
  7. // Пересекаются ли два прямоугольника (пересечение только по границе также считается пересечением)
  8. public static bool AreIntersected(Rectangle r1, Rectangle r2)
  9. {
  10. // так можно обратиться к координатам левого верхнего угла первого прямоугольника: r1.Left, r1.Top
  11. f (((r1.Left <= r2.Left) && (r2.Left <= r1.Right) && (((r1.Bottom >= r2.Bottom) && (r1.Bottom <= r2.Top))) || ((r2.Bottom >= r1.Bottom) && (r2.Bottom <= r1.Top)))) return true;
  12. else if (((r2.Left <= r1.Left) && (r1.Left <= r2.Right) && (((r1.Bottom >= r2.Bottom) && (r1.Bottom <= r2.Top))) || ((r2.Bottom >= r1.Bottom) && (r2.Bottom <= r1.Top)))) return true;
  13. else return false;
  14. }
  15.  
  16. // Площадь пересечения прямоугольников
  17. public static int IntersectionSquare(Rectangle r1, Rectangle r2)
  18. {
  19. int x = Math.Min(r2.Right, r1.Right) - Math.Max(r1.Left, r1.Left);
  20. int y = Math.Min(r2.Bottom, r1.Bottom) - Math.Max(r1.Top, r1.Top);
  21. return(x*y);
  22. }
  23.  
  24. // Если один из прямоугольников целиком находится внутри другого — вернуть номер (с нуля) внутреннего.
  25. // Иначе вернуть -1
  26. // Если прямоугольники совпадают, можно вернуть номер любого из них.
  27. public static int IndexOfInnerRectangle(Rectangle r1, Rectangle r2)
  28. {
  29. if (((r1.Left >= r2.Left) && (r1.Right <= r2.Right) && (r1.Top >= r2.Top) && (r1.Bottom <= r2.Bottom))) return 0;
  30. else if (((r2.Left >= r1.Left) && (r2.Right <= r1.Right) && (r2.Top >= r1.Top) && (r2.Bottom <= r1.Bottom))) return 1;
  31. else return -1;
  32. }
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement