using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rect
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Введите координату X первого прямоугольника");
int x1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите координату Y первого прямоугольника");
int y1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите ширину первого прямоугольника");
int width1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите высоту первого прямоугольника");
int height1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите координату X второго прямоугольника");
int x2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите координату Y второго прямоугольника");
int y2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите ширину второго прямоугольника");
int width2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите высоту второго прямоугольника");
int height2 = Convert.ToInt32(Console.ReadLine());
Rectangle rect1 = new Rectangle(x1, y1, width1, height1);
Rectangle rect2 = new Rectangle(x2, y2, width2, height2);
Console.WriteLine("Введите нужную цифру на клавиатуре:" + "\n" +
"1 - Произвести пересечение прямоугольников" + "\n" +
"2 - Произвести объединение прямоугольников" + "\n" +
"3 - Произвести смещение прямоугольников и вывести пересечение, объединение");
int C = Convert.ToInt32(Console.ReadLine());
switch (C)
{
case 1:
Intrsct();
break;
case 2:
Unn();
break;
case 3:
Moving();
Intrsct();
Unn();
break;
}
void Intrsct()
{
Console.WriteLine("Пересечение прямоугольников: " + Rectangle.Intersection(rect1, rect2));
}
void Unn()
{
Console.WriteLine("Объединение прямоугольников: " + Rectangle.Union(rect1, rect2));
}
void Moving()
{
Console.WriteLine("Введите смещение прямоугольника 1 по оси X");
int a1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите смещение прямоугольника 1 по оси Y");
int b1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите смещение прямоугольника 2 по оси X");
int a2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите смещение прямоугольника 2 по оси X");
int b2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Смещённый прямоугольник 1: ");
rect1.Move(a1, b1);
Console.WriteLine(rect1);
Console.WriteLine("Смещённый прямоугольник 2: ");
rect2.Move(a2, b2);
Console.WriteLine(rect2);
}
Console.ReadLine();
}
}
public class Rectangle
{
public Rectangle(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public void Move(int x, int y)
{
X += x;
Y += y;
}
public static Rectangle Intersection(Rectangle rect1, Rectangle rect2)
{
int minX = Math.Max(rect1.X, rect2.X);
int maxX = Math.Min(rect1.X + rect1.Width, rect2.X + rect2.Width);
int minY = Math.Max(rect1.Y, rect2.Y);
int maxY = Math.Min(rect1.Y + rect1.Height, rect2.Y + rect2.Height);
if (maxX >= minX && maxY >= minY)
{
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
}
return null;
}
public static Rectangle Union(Rectangle rect1, Rectangle rect2)
{
int minX = Math.Min(rect1.X, rect2.X);
int maxX = Math.Max(rect1.X + rect1.Width, rect2.X + rect2.Width);
int minY = Math.Min(rect1.Y, rect2.Y);
int maxY = Math.Max(rect1.Y + rect1.Height, rect2.Y + rect2.Height);
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
}
public override string ToString()
{
return $"X = {X}, Y = {Y}, Width = {Width}, Height = {Height}";
}
}
}