Skip to content

Instantly share code, notes, and snippets.

@kettanaito
Created June 22, 2018 09:33
Show Gist options
  • Save kettanaito/1241c86345b0635b760d1a194908374c to your computer and use it in GitHub Desktop.
Save kettanaito/1241c86345b0635b760d1a194908374c to your computer and use it in GitHub Desktop.
Rectangles intersection
/**
* Returns the intersection area (in px) based on the intersection
* between two given rectangles (i.e. ClientRect).
*/
export default function getIntersection(rectA, rectB) {
const intersectionX = Math.max(0, Math.min(rectA.right, rectB.right) - Math.max(rectA.left, rectB.left))
const intersectionY = Math.max(0, Math.min(rectA.bottom, rectB.bottom) - Math.max(rectA.top, rectB.top))
const intersectionArea = intersectionX * intersectionY
return intersectionArea
}
@kettanaito
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment