Интересная задача на C#
Ребят, помогите пожалуйста решить! Всю голову сломал!
Буду очень признателен!
using System;
namespace Rectangles
{
public static class RectanglesTask
{
// Пересекаются ли прямоугольники
public static bool AreIntersected(Rectangle r1, Rectangle r2)
{
if (A(r1.Left, r2.Left, r1.Width, r2.Width) && A(r1.Top, r2.Top, r1.Height, r2.Height))
return true;
else
return false;
}
// S пересечения прямоугольников
public static int IntersectionSquare(Rectangle r1, Rectangle r2)
{
if (!AreIntersected(r1, r2)) return 0;
else if ((r1.Left <= r2.Left) && (r1.Top <= r2.Top))
return Math.Min((r1.Top + r1.Height - r2.Top), Math.Min(r1.Height, r2.Height)) * Math.Min((r1.Left + r1.Width - r2.Left), Math.Min(r1.Width, r2.Width));
else if ((r1.Left <= r2.Left) && (r2.Top <= r1.Top))
return Math.Min((r1.Left + r1.Width - r2.Left), Math.Min(r1.Width, r2.Width)) * Math.Min((r2.Top + r2.Height - r1.Top), Math.Min(r1.Height, r2.Height));
else if ((r2.Left <= r1.Left) && (r2.Top <= r1.Top))
return Math.Min((r2.Top + r2.Height - r1.Top), Math.Min(r1.Height, r2.Height)) * Math.Min((r2.Left + r2.Width - r1.Left), Math.Min(r1.Width, r2.Width));
else return Math.Min((r2.Left + r2.Width - r1.Left), Math.Min(r1.Width, r2.Width)) * Math.Min((r1.Top + r1.Height - r2.Top), Math.Min(r1.Height, r2.Height));
}
public static int IndexOfInnerRectangle(Rectangle r1, Rectangle r2)
{
int square1 = r1.Width * r1.Height;
int square2 = r2.Width * r2.Height;
if ((IntersectionSquare(r1, r2) == square1) && (square1 != 0) || (square1 == 0) && (r1.Left >= r2.Left) && (r1.Left <= r2.Left + r2.Width) && (r1.Top >= r2.Top) && (r1.Top <= r2.Top + r2.Height)) return 0;
else if ((IntersectionSquare(r1, r2) == square2) && (square2 != 0) || (square2 == 0) && (r2.Left >= r1.Left) && (r2.Left <= r1.Left + r1.Width) && (r2.Top >= r1.Top) && (r2.Top <= r1.Top + r1.Height)) return 1;
else return -1;
}
static bool A(int x1, int x2, int y1, int y2)
{
return ((x1 + y1 - x2) >= 0) && ((y1 + y2) >= (x1 + y1 - x2));
}
}
}