Here's how to translate this code into JavaScript. Please note that there is a typo in your code and sentences in the article. In particular, r2->right left must be r2->right < r1->left , and r2->bottom top must be r2->bottom < r1->top for the function to work.
function intersectRect(r1, r2) { return !(r2.left > r1.right || r2.right < r1.left || r2.top > r1.bottom || r2.bottom < r1.top); }
Test case:
var rectA = { left: 10, top: 10, right: 30, bottom: 30 }; var rectB = { left: 20, top: 20, right: 50, bottom: 50 }; var rectC = { left: 70, top: 70, right: 90, bottom: 90 }; intersectRect(rectA, rectB);
Daniel Vassallo May 02 '10 at 3:50 a.m. 2010-05-02 03:50
source share