Created
June 22, 2018 09:33
-
-
Save kettanaito/1241c86345b0635b760d1a194908374c to your computer and use it in GitHub Desktop.
Rectangles intersection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code origin: https://math.stackexchange.com/a/99576