I had to write code for a competition which calculates sum of the perimeter of three overlapping rectangles in a 2D space. rectangle A and rectangles B and rectangle c is overlapping each other.They have also one common point of interaction. i need to calculate the overlapping area of each rectangle. But there is one common overlapping area also exist, which is creating problem for me.
Here is my code.
sample input 3 1 4 4 6 1 4 3 6 6 2 2 2 5 4 3
class Program
{
public static void Main()
{
FindPoints();
}
static void FindPoints()
{
int T = int.Parse(Console.ReadLine());
int[,] arr = new int[T, 4];
int Counter = T;
List<int> cost= new List<int>();
while (T-- > 0)
{
string[] s = Console.ReadLine().Split();
int x1 = int.Parse(s[0]);
int y1 = int.Parse(s[1]);
int x2 = int.Parse(s[2]);
int y2 = int.Parse(s[3]);
int C = int.Parse(s[4]);
cost.Add(C);
for (int x = 0; x <= T; x++)
{
for (int y = 0; y <= 3; y++)
{
if (y == 0)
arr[x, y] = x1;
if (y == 1)
arr[x, y] = y1;
if (y == 2)
arr[x, y] = x2;
if (y == 3)
arr[x, y] = y2;
}
}
}
if (Counter > 1)
{
// A and B intersection
int ABx5 = Math.Max(arr[2, 0], arr[1, 0]);
int ABy5 = Math.Max(arr[2, 1], arr[1, 1]);
int ABx6 = Math.Min(arr[2, 2], arr[1, 2]);
int ABy6 = Math.Min(arr[2, 3], arr[1, 3]);
// gives top-right point
// of intersection rectangle A and B
// no intersection
if (ABx5 > ABx6 || ABy5 > ABy6)
{
Console.WriteLine("No intersection");
Console.ReadLine();
return;
}
// A and C intersection
int ACx5 = Math.Max(arr[2, 0], arr[0, 0]);
int ACy5 = Math.Max(arr[2, 1], arr[0, 1]);
int ACx6 = Math.Min(arr[2, 2], arr[0, 2]);
int ACy6 = Math.Min(arr[2, 3], arr[0, 3]);
// no intersection
if (ACx5 > ACx6 || ACy5 > ACy6)
{
Console.WriteLine("No intersection");
Console.ReadLine();
return;
}
// B and C intersection
int BCx5 = Math.Max(arr[1, 0], arr[0, 0]);
int BCy5 = Math.Max(arr[1, 1], arr[0, 1]);
int BCx6 = Math.Min(arr[1, 2], arr[0, 2]);
int BCy6 = Math.Min(arr[1, 3], arr[0, 3]);
// no intersection
if (BCx5 > BCx6 || BCy5 > BCy6)
{
Console.WriteLine("No intersection");
Console.ReadLine();
return;
}
// A and B and C intersection
int ABCx5 = Math.Max(arr[2, 0], Math.Max(arr[1, 0], arr[0, 0]));
int ABCy5 = Math.Max(arr[2, 1], Math.Max(arr[1, 1], arr[0, 1]));
int ABCx6 = Math.Min(arr[2, 2], Math.Min(arr[1, 2], arr[0, 2]));
int ABCy6 = Math.Min(arr[2, 3], Math.Min(arr[1, 3], arr[0, 3]));
// gives top-right point
// of intersection rectangle A and B and C
// no intersection
if (ABCx5 > ABCx6 || ABCy5 > ABCy6)
{
Console.WriteLine("No intersection");
Console.ReadLine();
return;
}
//Here point is a block distance between 4 to 6 block is 3
// than we need to add 1 if distance between two point is non zero
int AB_1 = Math.Abs(ABx5 - ABy5 );
int AB_2 = Math.Abs(ABx6 - ABy6 ) ;
//origin point id 0
int AB_Interection = AB_1 + AB_2 +1;
int AC_1 = Math.Abs(ACx5 - ACy5 ) ;
int AC_2 = Math.Abs(ACx6 - ACy6 ) ;
//origin point id 0
int AC_Interection = AC_1 + AC_2+1;
int BC_1 = Math.Abs(BCx5 - BCy5 ) + 1;
int BC_2 = Math.Abs(BCx6 - BCy6 ) + 1;
//origin point is not 0 means each point have one block...coutning will start from 0
// so we need to add 1
int BC_Interection = BC_1 + BC_2 ;
int ABC_1 = Math.Abs(ABCx5 - ABCy5 );
int ABC_2 = Math.Abs(ABCx6 - ABCy6 );
// common interection point distance is 0 , means this is one block;
// than we need to add 1
int ABC_CommonInterection = ABC_1 + ABC_2 + 1;
int TotalInterectionWith_Area_A = (AB_Interection + AC_Interection)- ABC_CommonInterection;
int Compensation_Money_A = TotalInterectionWith_Area_A * cost[0];
int TotalInterectionWith_Area_B = (AB_Interection + BC_Interection) - ABC_CommonInterection;
int Compensation_Money_B = TotalInterectionWith_Area_B * cost[1];
int TotalInterectionWith_Area_C = (AC_Interection + BC_Interection) - ABC_CommonInterection;
int Compensation_Money_C = TotalInterectionWith_Area_C * cost[2];
int Total_Compensation_Money = Compensation_Money_A + Compensation_Money_B + Compensation_Money_C;
Console.WriteLine(Total_Compensation_Money);
Console.ReadLine();
}
}
}`