xtorcga
Version:
Xtor Compute Geometry Algorithm Libary 计算几何算法库
346 lines (345 loc) • 11.1 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.v2 = exports.Vec2 = void 0;
var eventhandler_1 = require("../render/eventhandler");
var Vec2 = /** @class */ (function (_super) {
__extends(Vec2, _super);
function Vec2(_x, _y) {
if (_x === void 0) { _x = 0; }
if (_y === void 0) { _y = 0; }
var _this = _super.call(this) || this;
_this._x = _x;
_this._y = _y;
_this.isVec2 = true;
return _this;
}
Object.defineProperty(Vec2.prototype, "x", {
get: function () {
return this._x;
},
set: function (value) {
if (this._x !== value) {
this._x = value;
this.fire('change', 'x', this._x, value);
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(Vec2.prototype, "y", {
get: function () {
return this._y;
},
set: function (value) {
if (this._y !== value) {
this._y = value;
this.fire('change', 'y', this._y, value);
}
},
enumerable: false,
configurable: true
});
Vec2.isVec2 = function (v) {
return !isNaN(v.x) && !isNaN(v.y) && isNaN(v.z) && isNaN(v.w);
};
Object.defineProperty(Vec2.prototype, "width", {
get: function () {
return this._x;
},
set: function (value) {
this._x = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Vec2.prototype, "height", {
get: function () {
return this._y;
},
set: function (value) {
this._y = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Vec2, "UnitX", {
get: function () {
return new Vec2(1, 0);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Vec2, "UnitY", {
get: function () {
return new Vec2(0, 1);
},
enumerable: false,
configurable: true
});
Vec2.prototype.set = function (x, y) {
this._x = x;
this._y = y;
return this;
};
Vec2.prototype.setScalar = function (scalar) {
this._x = scalar;
this._y = scalar;
return this;
};
Vec2.prototype.setX = function (x) {
this._x = x;
return this;
};
Vec2.prototype.setY = function (y) {
this._y = y;
return this;
};
Vec2.prototype.setComponent = function (index, value) {
switch (index) {
case 0:
this._x = value;
break;
case 1:
this._y = value;
break;
default:
throw new Error("index is out of range: " + index);
}
return this;
};
Vec2.prototype.getComponent = function (index) {
switch (index) {
case 0:
return this._x;
case 1:
return this._y;
default:
throw new Error("index is out of range: " + index);
}
};
Vec2.prototype.clone = function () {
return new Vec2(this._x, this._y);
};
Vec2.prototype.copy = function (v) {
this._x = v.x;
this._y = v.y;
return this;
};
Vec2.prototype.add = function (v, w) {
if (w !== undefined) {
console.warn("Vec2: .add() now only accepts one argument. Use .addVecs( a, b ) instead.");
return this.addVecs(v, w);
}
this._x += v.x;
this._y += v.y;
return this;
};
Vec2.prototype.addScalar = function (s) {
this._x += s;
this._y += s;
return this;
};
Vec2.prototype.addVecs = function (a, b) {
this._x = a.x + b.x;
this._y = a.y + b.y;
return this;
};
Vec2.prototype.addScaledVec = function (v, s) {
this._x += v.x * s;
this._y += v.y * s;
return this;
};
Vec2.prototype.sub = function (v, w) {
if (w !== undefined) {
console.warn("Vec2: .sub() now only accepts one argument. Use .subVecs( a, b ) instead.");
return this.subVecs(v, w);
}
this._x -= v.x;
this._y -= v.y;
return this;
};
Vec2.prototype.subScalar = function (s) {
this._x -= s;
this._y -= s;
return this;
};
Vec2.prototype.subVecs = function (a, b) {
this._x = a.x - b.x;
this._y = a.y - b.y;
return this;
};
Vec2.prototype.multiply = function (v) {
this._x *= v.x;
this._y *= v.y;
return this;
};
Vec2.prototype.multiplyScalar = function (scalar) {
this._x *= scalar;
this._y *= scalar;
return this;
};
Vec2.prototype.divide = function (v) {
this._x /= v.x;
this._y /= v.y;
return this;
};
Vec2.prototype.divideScalar = function (scalar) {
return this.multiplyScalar(1 / scalar);
};
Vec2.prototype.applyMat3 = function (m) {
var x = this._x, y = this._y;
var e = m.elements;
this._x = e[0] * x + e[3] * y + e[6];
this._y = e[1] * x + e[4] * y + e[7];
return this;
};
Vec2.prototype.min = function (v) {
this._x = Math.min(this._x, v.x);
this._y = Math.min(this._y, v.y);
return this;
};
Vec2.prototype.max = function (v) {
this._x = Math.max(this._x, v.x);
this._y = Math.max(this._y, v.y);
return this;
};
Vec2.prototype.clamp = function (min, max) {
// assumes min < max, componentwise
this._x = Math.max(min.x, Math.min(max.x, this._x));
this._y = Math.max(min.y, Math.min(max.y, this._y));
return this;
};
Vec2.prototype.clampScalar = function (minVal, maxVal) {
this._x = Math.max(minVal, Math.min(maxVal, this._x));
this._y = Math.max(minVal, Math.min(maxVal, this._y));
return this;
};
Vec2.prototype.clampLength = function (min, max) {
var length = this.length();
return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
};
Vec2.prototype.floor = function () {
this._x = Math.floor(this._x);
this._y = Math.floor(this._y);
return this;
};
Vec2.prototype.ceil = function () {
this._x = Math.ceil(this._x);
this._y = Math.ceil(this._y);
return this;
};
Vec2.prototype.round = function () {
this._x = Math.round(this._x);
this._y = Math.round(this._y);
return this;
};
Vec2.prototype.roundToZero = function () {
this._x = this._x < 0 ? Math.ceil(this._x) : Math.floor(this._x);
this._y = this._y < 0 ? Math.ceil(this._y) : Math.floor(this._y);
return this;
};
Vec2.prototype.negate = function () {
this._x = -this._x;
this._y = -this._y;
return this;
};
Vec2.prototype.dot = function (v) {
return this._x * v.x + this._y * v.y;
};
Vec2.prototype.cross = function (v) {
return this._x * v.y - this._y * v.x;
};
Vec2.prototype.lengthSq = function () {
return this._x * this._x + this._y * this._y;
};
Vec2.prototype.length = function () {
return Math.sqrt(this._x * this._x + this._y * this._y);
};
Vec2.prototype.manhattanLength = function () {
return Math.abs(this._x) + Math.abs(this._y);
};
Vec2.prototype.normalize = function () {
return this.divideScalar(this.length() || 1);
};
Vec2.prototype.angle = function () {
// computes the angle in radians with respect to the positive x-axis
var angle = Math.atan2(this._y, this._x);
if (angle < 0)
angle += 2 * Math.PI;
return angle;
};
Vec2.prototype.distanceTo = function (v) {
return Math.sqrt(this.distanceToSquared(v));
};
Vec2.prototype.distanceToSquared = function (v) {
var dx = this._x - v.x, dy = this._y - v.y;
return dx * dx + dy * dy;
};
Vec2.prototype.manhattanDistanceTo = function (v) {
return Math.abs(this._x - v.x) + Math.abs(this._y - v.y);
};
Vec2.prototype.setLength = function (length) {
return this.normalize().multiplyScalar(length);
};
Vec2.prototype.lerp = function (v, alpha) {
this._x += (v.x - this._x) * alpha;
this._y += (v.y - this._y) * alpha;
return this;
};
Vec2.prototype.lerpVecs = function (v1, v2, alpha) {
return this.subVecs(v2, v1)
.multiplyScalar(alpha)
.add(v1);
};
Vec2.prototype.equals = function (v) {
return v.x === this._x && v.y === this._y;
};
Vec2.prototype.fromArray = function (array, offset) {
if (offset === void 0) { offset = 0; }
this._x = array[offset];
this._y = array[offset + 1];
return this;
};
Vec2.prototype.toArray = function (array, offset) {
if (array === void 0) { array = []; }
if (offset === void 0) { offset = 0; }
array[offset] = this._x;
array[offset + 1] = this._y;
return array;
};
Vec2.prototype.fromBufferAttribute = function (attribute, index, offset) {
if (offset !== undefined) {
console.warn("Vec2: offset has been removed from .fromBufferAttribute().");
}
this._x = attribute.getX(index);
this._y = attribute.getY(index);
return this;
};
Vec2.prototype.rotateAround = function (center, angle) {
var c = Math.cos(angle), s = Math.sin(angle);
var x = this._x - center.x;
var y = this._y - center.y;
this._x = x * c - y * s + center.x;
this._y = x * s + y * c + center.y;
return this;
};
return Vec2;
}(eventhandler_1.EventHandler));
exports.Vec2 = Vec2;
function v2() {
return new Vec2();
}
exports.v2 = v2;