I have code that (correctly) gets the 4 corner's x and y position. I want to know if element A is within the bounds of element B at all (even by 1px). I can't for the life of me figure out the right equation for this.
I have this, but this only checks if the top left of A is inside of B.
xIntersects = a.topLeft.x >= b.topLeft.x && a.topLeft.x <= b.topRight.x;
yIntersects = a.topLeft.y >= b.topLeft.y && a.topLeft.y <= b.bottomLeft.y;
console.log(xIntersects && yIntersects) // true only when topLeft of A is in B
I can write a statement like this for each of the four corners, but that seems really expensive and there's probably a much nicer way of handling this.
My code is in JS, but this answer could really be in any language.