Skip to content

Instantly share code, notes, and snippets.

@thomasvaeth
Created March 13, 2018 01:33
Show Gist options
  • Save thomasvaeth/a48f3bd4ddcfa9bd76dd3f5dcbc868c5 to your computer and use it in GitHub Desktop.
Save thomasvaeth/a48f3bd4ddcfa9bd76dd3f5dcbc868c5 to your computer and use it in GitHub Desktop.
"Spiteful words can hurt your feelings but silence breaks your heart." - C.S. Lewis
function overlapArea(arr1, arr2) {
const rect1 = {
left: arr1[0],
right: arr1[2],
top: arr1[1],
bottom: arr1[3]
};
const rect2 = {
left: arr2[0],
right: arr2[2],
top: arr2[1],
bottom: arr2[3]
};
const x = Math.min(rect1.right, rect2.right) - Math.max(rect1.left, rect2.left);
const y = Math.min(rect1.bottom, rect2.bottom) - Math.max(rect1.top, rect2.top);
return x > 0 && y > 0 ? x * y : 0;
}
overlapArea([0,0,4,4], [2,2,4,4]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment