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)
- {
- var leftFirst = (r1.Left <= r2.Left) && (r1.Right >= r2.Left);
- var leftSecond = (r2.Left <= r1.Left) && (r2.Right >= r1.Left);
- var rightFirst = (r1.Top <= r2.Top) && (r1.Bottom >= r2.Top);
- var rightSecond = (r2.Top <= r1.Top) && (r2.Bottom >= r1.Top);
- return (leftFirst || leftSecond) && (rightFirst || rightSecond);
- }
- public static int IntersectionSquare(Rectangle r1, Rectangle r2)
- {
- if (!AreIntersected(r1, r2))
- return 0;
- var width = Math.Max(r1.Left, r2.Left) - Math.Min(r1.Right, r2.Right);
- var height = Math.Max(r1.Top, r2.Top) - Math.Min(r1.Bottom, r2.Bottom);
- return width * height;
- }
- public static int IndexOfInnerRectangle(Rectangle r1, Rectangle r2)
- {
- if (IsInner(r1, r2))
- return 0;
- return IsInner(r2, r1) ? 1 : -1;
- }
- static bool IsInner(Rectangle r1, Rectangle r2)
- {
- return (r1.Left >= r2.Left) && (r1.Right <= r2.Right)
- && (r1.Top >= r2.Top) && (r1.Bottom <= r2.Bottom);
- }
- }
- }
Add Comment
Please, Sign In to add comment