Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wout/b3d26eb16da69887d812606de004f312 to your computer and use it in GitHub Desktop.
Save wout/b3d26eb16da69887d812606de004f312 to your computer and use it in GitHub Desktop.
// x,y is bottom left corner
var Rectangle = function Rectangle(x,y,w,h){
this.x = x;
this.y = y;
this.width = w;
this.height = h;
}
// SOLUTION
var checkIntersect = function checkIntersect(r1,r2) {
if(!intersect(r1,r2)){
return new Rectangle(0,0,0,0);
}
return new Rectangle(
Math.max(r1.x, r2.x), Math.max(r1.y, r2.y),
Math.min(r1.x + r1.width, r2.x + r2.width) - Math.max(r1.x, r2.x),
Math.min(r1.y + r1.height, r2.y + r2.height) - Math.max(r1.y, r2.y)
)
}
var intersect = function intersect(r1, r2) {
return r1.x <= (r2.x + r2.width) && (r1.x + r1.width) >= r2.x &&
r1.y <= (r2.y + r2.height) && (r1.y + r1.height) >= r2.y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment