Skip to content

Instantly share code, notes, and snippets.

@yckart
Last active May 19, 2023 06:37
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yckart/7177551 to your computer and use it in GitHub Desktop.
Save yckart/7177551 to your computer and use it in GitHub Desktop.
Checks if an element is inside another or if it collides with another. - http://stackoverflow.com/a/19614185/1250044
var Rect = function (top, left, width, height) {
this.top = top || 0;
this.left = left || 0;
this.width = width || 0;
this.height = height || 0;
this.right = this.left + this.width;
this.bottom = this.top + this.height;
};
Rect.prototype = {
set: function (top, left, width, height) {
this.top = top || this.top;
this.left = left || this.left;
this.width = width || this.width;
this.height = height || this.height;
this.right = this.left + this.width;
this.bottom = this.top + this.height;
},
union: function(toUnion) {
var union = new Rect();
union.left = Math.min(this.left, toUnion.left);
union.top = Math.min(this.top, toUnion.top);
union.width = Math.max(this.left + this.width, toUnion.left + toUnion.width) - union.left;
union.height = Math.max(this.top + this.height, toUnion.top + toUnion.height) - union.top;
return union;
},
intersect: function(rect) {
var rectI = new Rect()
rectI.left = Math.max(this.left, rect.left)
rectI.top = Math.max(this.top, rect.top)
rectI.right = Math.min(this.right, rect.right)
rectI.bottom = Math.min(this.bottom, rect.bottom)
return rectI
}
}
var AABB = function (rect) {
this.top = rect.top;
this.left = rect.left;
this.width = rect.width;
this.height = rect.height;
this.right = rect.right;
this.bottom = rect.bottom;
};
AABB.prototype = {
colliding: function (other) {
return !(
this.top > other.bottom ||
this.right < other.left ||
this.bottom < other.top ||
this.left > other.right
);
},
containing: function (other) {
return (
this.left <= other.left &&
other.left < this.width &&
this.top <= other.top &&
other.top < this.height
);
},
inside: function (other) {
return (
((other.top <= this.top) && (this.top <= other.bottom)) &&
((other.top <= this.bottom) && (this.bottom <= other.bottom)) &&
((other.left <= this.left) && (this.left <= other.right)) &&
((other.left <= this.right) && (this.right <= other.right))
);
}
};
@linus-amg
Copy link

thats pretty awesome, nicely done.

@yckart
Copy link
Author

yckart commented Sep 16, 2014

@lgubenis Thanks for the laudatory words :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment