142 lines
5.2 KiB
JavaScript
142 lines
5.2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Box2 = void 0;
|
|
var Vector2_js_1 = require("./Vector2.js");
|
|
var Box2 = /** @class */ (function () {
|
|
function Box2(min, max) {
|
|
if (min === void 0) { min = new Vector2_js_1.Vector2(+Number.POSITIVE_INFINITY, +Number.POSITIVE_INFINITY); }
|
|
if (max === void 0) { max = new Vector2_js_1.Vector2(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY); }
|
|
this.min = min;
|
|
this.max = max;
|
|
}
|
|
Object.defineProperty(Box2.prototype, "area", {
|
|
/** 获取面积 */
|
|
get: function () {
|
|
return (this.max.x - this.min.x) * (this.max.y - this.min.y);
|
|
},
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
/** */
|
|
Box2.prototype.set = function (min, max) {
|
|
this.min.copy(min);
|
|
this.max.copy(max);
|
|
return this;
|
|
};
|
|
Box2.prototype.setFromPoints = function (points) {
|
|
this.makeEmpty();
|
|
for (var _i = 0, points_1 = points; _i < points_1.length; _i++) {
|
|
var p = points_1[_i];
|
|
this.expandByPoint(p);
|
|
}
|
|
return this;
|
|
};
|
|
Box2.prototype.setFromCenterAndSize = function (center, size) {
|
|
var v1 = Box2._setFromCenterAndSize_v1;
|
|
var halfSize = v1.copy(size).multiplyScalar(0.5);
|
|
this.min.copy(center).sub(halfSize);
|
|
this.max.copy(center).add(halfSize);
|
|
return this;
|
|
};
|
|
Box2.prototype.clone = function () {
|
|
return new this.constructor().copy(this);
|
|
};
|
|
Box2.prototype.copy = function (box) {
|
|
this.min.copy(box.min);
|
|
this.max.copy(box.max);
|
|
return this;
|
|
};
|
|
Box2.prototype.makeEmpty = function () {
|
|
this.min.x = this.min.y = +Number.POSITIVE_INFINITY;
|
|
this.max.x = this.max.y = Number.NEGATIVE_INFINITY;
|
|
return this;
|
|
};
|
|
Box2.prototype.isEmpty = function () {
|
|
// this is a more robust check for empty than (volume <= 0) because volume can get positive with two negative axes
|
|
return (this.max.x < this.min.x) || (this.max.y < this.min.y);
|
|
};
|
|
Box2.prototype.getCenter = function (result) {
|
|
if (result === void 0) { result = new Vector2_js_1.Vector2(); }
|
|
return this.isEmpty() ? result.set(0, 0) : result.addVectors(this.min, this.max).multiplyScalar(0.5);
|
|
};
|
|
Box2.prototype.getSize = function (result) {
|
|
if (result === void 0) { result = new Vector2_js_1.Vector2(); }
|
|
return this.isEmpty() ? result.set(0, 0) : result.subVectors(this.max, this.min);
|
|
};
|
|
Box2.prototype.expandByPoint = function (point) {
|
|
this.min.min(point);
|
|
this.max.max(point);
|
|
return this;
|
|
};
|
|
Box2.prototype.expandByVector = function (vector) {
|
|
this.min.sub(vector);
|
|
this.max.add(vector);
|
|
return this;
|
|
};
|
|
Box2.prototype.expandByScalar = function (scalar) {
|
|
this.min.addScalar(-scalar);
|
|
this.max.addScalar(scalar);
|
|
return this;
|
|
};
|
|
Box2.prototype.containsPoint = function (point) {
|
|
if (point.x < this.min.x || point.x > this.max.x
|
|
|| point.y < this.min.y || point.y > this.max.y) {
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
Box2.prototype.containsBox = function (box) {
|
|
if ((this.min.x <= box.min.x) && (box.max.x <= this.max.x)
|
|
&& (this.min.y <= box.min.y) && (box.max.y <= this.max.y)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
Box2.prototype.getParameter = function (point, result) {
|
|
if (result === void 0) { result = new Vector2_js_1.Vector2(); }
|
|
// This can potentially have a divide by zero if the box
|
|
// has a size dimension of 0.
|
|
return result.set((point.x - this.min.x) / (this.max.x - this.min.x), (point.y - this.min.y) / (this.max.y - this.min.y));
|
|
};
|
|
Box2.prototype.intersectsBox = function (box) {
|
|
// using 6 splitting planes to rule out intersections.
|
|
if (box.max.x < this.min.x || box.min.x > this.max.x
|
|
|| box.max.y < this.min.y || box.min.y > this.max.y) {
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
Box2.prototype.clampPoint = function (point, result) {
|
|
if (result === void 0) { result = new Vector2_js_1.Vector2(); }
|
|
return result.copy(point).clamp(this.min, this.max);
|
|
};
|
|
Box2.prototype.distanceToPoint = function (point) {
|
|
var v1 = Box2._distanceToPoint_v1;
|
|
var clampedPoint = v1.copy(point).clamp(this.min, this.max);
|
|
return clampedPoint.sub(point).length();
|
|
};
|
|
Box2.prototype.intersect = function (box) {
|
|
this.min.max(box.min);
|
|
this.max.min(box.max);
|
|
return this;
|
|
};
|
|
Box2.prototype.union = function (box) {
|
|
this.min.min(box.min);
|
|
this.max.max(box.max);
|
|
return this;
|
|
};
|
|
Box2.prototype.translate = function (offset) {
|
|
this.min.add(offset);
|
|
this.max.add(offset);
|
|
return this;
|
|
};
|
|
Box2.prototype.equals = function (box) {
|
|
return box.min.equals(this.min) && box.max.equals(this.max);
|
|
};
|
|
Box2._setFromCenterAndSize_v1 = new Vector2_js_1.Vector2();
|
|
Box2._distanceToPoint_v1 = new Vector2_js_1.Vector2();
|
|
return Box2;
|
|
}());
|
|
exports.Box2 = Box2;
|
|
;
|