DEV Community

Cover image for Rectangle Intersection
Zevan Rosser
Zevan Rosser

Posted on

Rectangle Intersection

Very useful for complex UI or simple games... checking if two arbitrary non-rotated rectangles intersect:

This is the real key, most of the rest of the code here is boilerplate...

 rectA.left < rectB.right &&
 rectA.right > rectB.left &&
 rectA.top < rectB.bottom &&
 rectA.bottom > rectB.top
Enter fullscreen mode Exit fullscreen mode

See more stuff like this over @ Snippet Zone

Discussion (0)