Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Rectangles
- {
- public static class RectanglesTask
- {
- // Пересекаются ли два прямоугольника (пересечение только по границе также считается пересечением)
- public static bool AreIntersected(Rectangle r1, Rectangle r2)
- {
- // так можно обратиться к координатам левого верхнего угла первого прямоугольника: r1.Left, r1.Top
- 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;
- 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;
- else return false;
- }
- // Площадь пересечения прямоугольников
- public static int IntersectionSquare(Rectangle r1, Rectangle r2)
- {
- int x = Math.Min(r2.Right, r1.Right) - Math.Max(r1.Left, r1.Left);
- int y = Math.Min(r2.Bottom, r1.Bottom) - Math.Max(r1.Top, r1.Top);
- return(x*y);
- }
- // Если один из прямоугольников целиком находится внутри другого — вернуть номер (с нуля) внутреннего.
- // Иначе вернуть -1
- // Если прямоугольники совпадают, можно вернуть номер любого из них.
- public static int IndexOfInnerRectangle(Rectangle r1, Rectangle r2)
- {
- if (((r1.Left >= r2.Left) && (r1.Right <= r2.Right) && (r1.Top >= r2.Top) && (r1.Bottom <= r2.Bottom))) return 0;
- else if (((r2.Left >= r1.Left) && (r2.Right <= r1.Right) && (r2.Top >= r1.Top) && (r2.Bottom <= r1.Bottom))) return 1;
- else return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement