# overlapping rectangles c#

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[,] arr = new int[T, 4];
int Counter = T;
List<int> cost= new List<int>();
while (T-- > 0)
{
int x1 = int.Parse(s);
int y1 = int.Parse(s);
int x2 = int.Parse(s);
int y2 = int.Parse(s);
int C = int.Parse(s);
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");
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");
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");
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");
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;

int TotalInterectionWith_Area_B = (AB_Interection + BC_Interection) - ABC_CommonInterection;
int Compensation_Money_B = TotalInterectionWith_Area_B * cost;

int TotalInterectionWith_Area_C = (AC_Interection + BC_Interection) - ABC_CommonInterection;
int Compensation_Money_C = TotalInterectionWith_Area_C * cost;

int Total_Compensation_Money = Compensation_Money_A + Compensation_Money_B + Compensation_Money_C;

Console.WriteLine(Total_Compensation_Money);

}

}
}

• Welcome to Code Review! Does you code work as intended? If yes, please add some sample input and the resulting output to your question. If no, then your question is off-topic. Oct 18, 2019 at 12:22
• Yes my code is working fine. Oct 18, 2019 at 12:29
• @Heslacher, I have added sample input. Thanks Oct 18, 2019 at 12:34
• @Heslacher I have tested with may test cases. it is working fine. I don't know the website's test cases. Oct 18, 2019 at 12:47

## DRY Code

There is a programming principle called the Don't Repeat Yourself Principle sometimes referred to as DRY code. If you find yourself repeating the same code multiple times it is better to encapsulate it in a function. If it is possible to loop through the code that can reduce repetition as well.

This code repeats many times with minor variations:

            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]);


It might be better to create a function that performs this.

## Complexity

The function FindPoints() is too complex (does too much). All software design methods include breaking the problem into smaller and smaller pieces until the problem is very easy to solve. Smaller functions are easier to read, write, debug and maintain.

There is a programming principle called the Single Responsibility Principle that applies here. The Single Responsibility Principle states:

that every module, class, or function should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by that module, class or function.

In addition to the possible function mentioned above in the DRY Code section, it might be better to add a function that only gets the input and creates the arrays, a function that finds the overlap of the rectangles and a function that calculates the area of each rectangle.

## Variable Names

From the problem description, it is not clear why there are any variable names related to money or compensation, which means that the variables that refer to this are unclear. The variable name T` may come from the original problem description on the website, but it isn't clear here either.