Range = function () {
this.initialize.apply(this, arguments);
}
Range.prototype = function () {};
Range.prototype.constructor = Range;
Range.prototype.initialize = function (start, stop) {
this.start = start;
this.stop = stop;
}
Range.prototype.containValue = function (value) {
return value >= this.start && value <= this.stop;
}
Range.prototype.intersects = function (range) {
return !(this.stop < range.start || this.start > range.stop);
}
AABB = function () {
this.initialize.apply(this, arguments);
}
AABB.prototype = Rectangle;
AABB.prototype.constructor = AABB;
AABB.prototype.initialize = function (x, y, width, height) {
Rectangle.prototype.initialize.call(this, x, y, width, height);
this.setCencter();
this.setCorners();
this.setRange();
}
AABB.prototype.setCencter = function () {
this._cencter = new Point(this.x / 2, this.y / 2);
}
AABB.prototype.setCorners = function () {
this._leftUpPoint = new Point(this.x, this.y);
this._rightUpPoint = new Point(this.x + this.width, this.y);
this._leftDownPoint = new Point(this.x, this.y + this.height);
this._rightDownPoint = new Point(this.x + this.width, this.y + this.height);
}
AABB.prototype.setRange = function () {
this._wRange = new Range(this.x, this.x + this.width);
this._hRange = new Range(this.y, this.y + this.height);
}
AABB.prototype.getCenter = function () {
return this._cencter;
}
AABB.prototype.getCorners = function () {
return [this._leftUpPoint, this._rightUpPoint, this._leftDownPoint, this._rightDownPoint];
}
AABB.prototype.intersects = function (aabb) {
return this._wRange.intersects(aabb._wRange) && this._hRange.intersects(aabb._hRange);
}
AABB.prototype.containPoint = function (point) {
return point.x >= this.x && point.x <= this._rightDownPoint.x && point.y >= this.y && point.y <= this._rightDownPoint.y;
}
AABB.prototype.merge = function (aabb) {
var x = Math.min(this.x, aabb.x);
var y = Math.min(this.y, aabb.y);
var width = Math.max(this.x + this.width, aabb.x + aabb.width) - x;
var height = Math.max(this.y + this.height, this.y + aabb.height) - y;
return new AABB(x, y, width, height);
}
AABB.prototype.reset = function (x, y, width, height) {
Rectangle.prototype.initialize.call(this, x, y, width, height);
this.setCencter();
this.setCorners();
this.setRange();
}
轴对齐包围盒。那个 Range 差不多相当于一维的包围盒吧【笑
构造函数和rectangle一样。
getCenter 获得中心点 返回值是个Point对象
getCorners 获得四个顶点 返回值是个Point对象数组
intersects 检查两个包围盒是否相交 参数是个AABB对象
containPoint 检查点是否在包围盒内 参数是个Point对象
merge 返回一个能容纳两个包围盒的最小包围盒 参数是个AABB对象
reset 重置包围盒对象