Task: You are given two rectangles.For each rectangle you are given its bottom-left and top-right points. Check if they overlap. If they do, check for bottom-left and top-right points of the overlapping area.
I decided to solve this by using multidimensional arrays. Using object might make it more transaprent tho. Is there anything, that I could change (especially in terms of task approach, code bugs, code style and performance ) ?
I came up with following code: Method wich checks if the give rectangles overlap
public static bool Overlapping(int[][] firstRectangle, int[][] secondRectangle)
{
bool XOverlapping = secondRectangle[0][0] <= firstRectangle[1][0] &&
firstRectangle[0][0] <= secondRectangle[1][0];
bool YOverlapping = secondRectangle[0][1] <= firstRectangle[1][1] &&
firstRectangle[0][1] <= secondRectangle[1][1];
if (XOverlapping && YOverlapping)
{
return true;
}
else
{
return false;
}
}
Method which checks for bottom-left and top-right points of the overlapping area.
public static void GetOverlappingArea(int[][] firstRectangle, int[][] secondRectangle)
{
if (Overlapping(firstRectangle, secondRectangle))
{
int startXOverlapping = Math.Max(firstRectangle[0][0], secondRectangle[0][0]);
int endXOverlapping = Math.Min(firstRectangle[1][0], secondRectangle[1][0]);
int startYOverlapping = Math.Max(firstRectangle[0][1], secondRectangle[0][1]);
int endYOverlapping = Math.Min(firstRectangle[1][1], secondRectangle[1][1]);
Console.WriteLine("Left down corner: (" + startXOverlapping + " , " + startYOverlapping + "), Upper right corner: (" + endXOverlapping + " , " + endYOverlapping + ")");
}
else
{
Console.WriteLine("Rectangles do not overlap");
}
}
My test method
static void Main(string[] args)
{
int[][] FirstRectangle = new[] { new[] { 2, 4 }, new[] { 8, 7 } };
int[][] SecondRectangle = new[] { new[] { 5, 7 }, new[] { 10, 15 } };
GetOverlappingArea(FirstRectangle, SecondRectangle);
}