Created
January 16, 2017 20:03
-
-
Save JessicaGillan/9fb0c4634f751c3cbcfd7f0c298ff5bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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