Area of Overlapping Rectangles

Published by persolut in

Create a function that returns the area of the overlap between two rectangles. The function will receive two rectangles,each with the coordinates of the lower left corner followed by the width and the height int[] { x, y, width, height }.

Examples

OverlappingRectangles(new int[] { 2, 1, 3, 4 }, new int[] { 3, 2, 2, 5  }) ➞ 6

OverlappingRectangles(new int[] { 2, -9, 11, 5 }, new int[] { 5, -11, 2, 9 }) ➞ 10

OverlappingRectangles(new int[] { -8, -7, 4, 7 },  new int[] { -5, -9, 4, 7 }) ➞ 5

Example 1

Example 2

Example 3

Notes

  • Coordinates can be positive or negative integers.
  • Not all examples have overlaps.
Watch a quick demo on how Edabit works.