Skip to content

Instantly share code, notes, and snippets.

@JessicaGillan
Created January 16, 2017 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JessicaGillan/9fb0c4634f751c3cbcfd7f0c298ff5bf to your computer and use it in GitHub Desktop.
Save JessicaGillan/9fb0c4634f751c3cbcfd7f0c298ff5bf 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