@turbox3d/math
Version:
Large-scale graphics application math library
526 lines (525 loc) • 14.5 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Vector2 = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _MathUtils = require("../MathUtils");
var _Tolerance = require("./Tolerance");
var _Vector = require("./Vector3");
var Vector2 = exports.Vector2 = /*#__PURE__*/function () {
function Vector2() {
var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
(0, _classCallCheck2["default"])(this, Vector2);
this.isVector2 = true;
this.x = x;
this.y = y;
}
return (0, _createClass2["default"])(Vector2, [{
key: "width",
get: function get() {
return this.x;
},
set: function set(value) {
this.x = value;
}
}, {
key: "height",
get: function get() {
return this.y;
},
set: function set(value) {
this.y = value;
}
}, {
key: "set",
value: function set(x, y) {
this.x = x;
this.y = y;
return this;
}
}, {
key: "setScalar",
value: function setScalar(scalar) {
this.x = scalar;
this.y = scalar;
return this;
}
}, {
key: "setX",
value: function setX(x) {
this.x = x;
return this;
}
}, {
key: "setY",
value: function setY(y) {
this.y = y;
return this;
}
}, {
key: "setComponent",
value: function setComponent(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: ".concat(index));
}
return this;
}
}, {
key: "getComponent",
value: function getComponent(index) {
switch (index) {
case 0:
return this.x;
case 1:
return this.y;
default:
throw new Error("index is out of range: ".concat(index));
}
}
}, {
key: "clone",
value: function clone() {
return new Vector2(this.x, this.y);
}
}, {
key: "copy",
value: function copy(v) {
this.x = v.x;
this.y = v.y;
return this;
}
}, {
key: "add",
value: function add(v) {
this.x += v.x;
this.y += v.y;
return this;
}
}, {
key: "added",
value: function added(v) {
return new Vector2(this.x + v.x, this.y + v.y);
}
}, {
key: "addScalar",
value: function addScalar(s) {
this.x += s;
this.y += s;
return this;
}
}, {
key: "addVectors",
value: function addVectors(a, b) {
this.x = a.x + b.x;
this.y = a.y + b.y;
return this;
}
}, {
key: "addScaledVector",
value: function addScaledVector(v, s) {
this.x += v.x * s;
this.y += v.y * s;
return this;
}
}, {
key: "sub",
value: function sub(v) {
this.x -= v.x;
this.y -= v.y;
return this;
}
}, {
key: "subtracted",
value: function subtracted(v) {
return new Vector2(this.x - v.x, this.y - v.y);
}
}, {
key: "subScalar",
value: function subScalar(s) {
this.x -= s;
this.y -= s;
return this;
}
}, {
key: "subVectors",
value: function subVectors(a, b) {
this.x = a.x - b.x;
this.y = a.y - b.y;
return this;
}
}, {
key: "reverse",
value: function reverse() {
this.x = -this.x;
this.y = -this.y;
return this;
}
}, {
key: "reversed",
value: function reversed() {
return new Vector2(-this.x, -this.y);
}
}, {
key: "multiply",
value: function multiply(v) {
this.x *= v.x;
this.y *= v.y;
return this;
}
/**
* Return a new vector which is the scaler result of this vector.
* @param scale
*/
}, {
key: "multiplied",
value: function multiplied(scale) {
return new Vector2(this.x * scale, this.y * scale);
}
}, {
key: "multiplyScalar",
value: function multiplyScalar(scalar) {
this.x *= scalar;
this.y *= scalar;
return this;
}
}, {
key: "divide",
value: function divide(v) {
this.x /= v.x;
this.y /= v.y;
return this;
}
}, {
key: "divideScalar",
value: function divideScalar(scalar) {
return this.multiplyScalar(1 / scalar);
}
}, {
key: "applyMatrix3",
value: function applyMatrix3(m) {
var x = this.x;
var 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;
}
}, {
key: "min",
value: function min(v) {
this.x = Math.min(this.x, v.x);
this.y = Math.min(this.y, v.y);
return this;
}
}, {
key: "max",
value: function max(v) {
this.x = Math.max(this.x, v.x);
this.y = Math.max(this.y, v.y);
return this;
}
}, {
key: "clamp",
value: function clamp(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;
}
}, {
key: "clampScalar",
value: function clampScalar(minVal, maxVal) {
this.x = Math.max(minVal, Math.min(maxVal, this.x));
this.y = Math.max(minVal, Math.min(maxVal, this.y));
return this;
}
}, {
key: "clampLength",
value: function clampLength(min, max) {
var length = this.length;
return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
}
}, {
key: "floor",
value: function floor() {
this.x = Math.floor(this.x);
this.y = Math.floor(this.y);
return this;
}
}, {
key: "ceil",
value: function ceil() {
this.x = Math.ceil(this.x);
this.y = Math.ceil(this.y);
return this;
}
}, {
key: "round",
value: function round() {
this.x = Math.round(this.x);
this.y = Math.round(this.y);
return this;
}
}, {
key: "roundToZero",
value: function roundToZero() {
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;
}
}, {
key: "negate",
value: function negate() {
this.x = -this.x;
this.y = -this.y;
return this;
}
}, {
key: "dot",
value: function dot(v) {
return this.x * v.x + this.y * v.y;
}
}, {
key: "cross",
value: function cross(v) {
return this.x * v.y - this.y * v.x;
}
}, {
key: "lengthSq",
get: function get() {
return this.x * this.x + this.y * this.y;
}
}, {
key: "length",
get: function get() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}, {
key: "manhattanLength",
get: function get() {
return Math.abs(this.x) + Math.abs(this.y);
}
}, {
key: "normalize",
value: function normalize() {
return this.divideScalar(this.length || 1);
}
/**
* The angle in [0, PI]
*/
}, {
key: "angle",
value: function angle(v) {
if (!v) {
return Math.atan2(-this.y, -this.x) + Math.PI;
}
return Math.atan2(Math.abs(this.cross(v)), this.dot(v));
}
/**
* The angle in [0, 2PI)
*/
}, {
key: "angleTo",
value: function angleTo(v) {
var crossed = this.cross(v);
var angle = this.angle(v);
var pi2 = Math.PI * 2;
if (crossed < 0.0 && angle < Math.PI && angle > 0) {
return pi2 - angle;
}
return angle;
}
/**
* 返回单位向量
*/
}, {
key: "normalized",
value: function normalized() {
var tol = 1e-16;
var len = this.lengthSq;
if (len === 0 || _MathUtils.MathUtils.isEqual(len, 1, tol)) {
return new Vector2(this.x, this.y);
}
len = Math.sqrt(len);
var newX = this.x / len;
var newY = this.y / len;
if (!Number.isFinite(newX) || !Number.isFinite(newY)) {
return new Vector2(this.x, this.y);
}
return new Vector2(newX, newY);
}
}, {
key: "distanceTo",
value: function distanceTo(v) {
return Math.sqrt(this.distanceToSquared(v));
}
}, {
key: "distanceToSquared",
value: function distanceToSquared(v) {
var dx = this.x - v.x;
var dy = this.y - v.y;
return dx * dx + dy * dy;
}
}, {
key: "manhattanDistanceTo",
value: function manhattanDistanceTo(v) {
return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
}
}, {
key: "setLength",
value: function setLength(length) {
return this.normalize().multiplyScalar(length);
}
}, {
key: "lerp",
value: function lerp(v, alpha) {
this.x += (v.x - this.x) * alpha;
this.y += (v.y - this.y) * alpha;
return this;
}
}, {
key: "lerpVectors",
value: function lerpVectors(v1, v2, alpha) {
this.x = v1.x + (v2.x - v1.x) * alpha;
this.y = v1.y + (v2.y - v1.y) * alpha;
return this;
}
}, {
key: "equals",
value: function equals(v) {
var distTol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _Tolerance.Tolerance.global.distTol;
var cosTol = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _Tolerance.Tolerance.global.cosTol;
var sLen0 = this.lengthSq;
var sLen1 = v.lengthSq;
if (_MathUtils.MathUtils.isZero(sLen0, distTol * distTol) && _MathUtils.MathUtils.isZero(sLen1, distTol * distTol)) {
return true;
}
// 向量距离相等并且方向相同
return _MathUtils.MathUtils.isEqual(Math.sqrt(sLen0), Math.sqrt(sLen1), distTol) && this.isSameDirection(v, new _Tolerance.Tolerance(cosTol, distTol, _Tolerance.Tolerance.global.numTol), false);
}
}, {
key: "isZero",
value: function isZero() {
var distTol = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _Tolerance.Tolerance.global.distTol;
return this.lengthSq < distTol * distTol;
}
/**
* 是否平行
*/
}, {
key: "isParallel",
value: function isParallel(vec, tol) {
var checkZeroVec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var cosTol = tol ? tol.cosTol : _Tolerance.Tolerance.global.cosTol;
var distTol = tol ? tol.distTol : _Tolerance.Tolerance.global.distTol;
var len1 = this.length;
var len2 = vec.length;
if (len1 === 0 || len2 === 0 || checkZeroVec && (_MathUtils.MathUtils.isZero(len1, distTol) || _MathUtils.MathUtils.isZero(len2, distTol))) {
return false;
}
var multiLen = len1 * len2;
return Math.abs(multiLen - Math.abs(this.dot(vec))) <= cosTol * multiLen;
}
/**
* 是否垂直
*/
}, {
key: "isPerpendicular",
value: function isPerpendicular(vec, tol) {
var checkZeroVec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var cosTol = tol ? tol.cosTol : _Tolerance.Tolerance.global.cosTol;
var distTol = tol ? tol.distTol : _Tolerance.Tolerance.global.distTol;
var len1 = this.length;
var len2 = vec.length;
if (len1 === 0 || len2 === 0 || checkZeroVec && (_MathUtils.MathUtils.isZero(len1, distTol) || _MathUtils.MathUtils.isZero(len2, distTol))) {
return false;
}
var multiLen = len1 * len2;
return Math.abs(multiLen - Math.abs(this.cross(vec))) <= cosTol * multiLen;
}
}, {
key: "isSameDirection",
value: function isSameDirection(v, tol) {
var checkZeroVec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var cosTol = tol ? tol.cosTol : _Tolerance.Tolerance.global.cosTol;
var distTol = tol ? tol.distTol : _Tolerance.Tolerance.global.distTol;
var len1 = this.length;
var len2 = v.length;
if (len1 === 0 || len2 === 0 || checkZeroVec && (_MathUtils.MathUtils.isZero(len1, distTol) || _MathUtils.MathUtils.isZero(len2, distTol))) {
return false;
}
var multiLen = len1 * len2;
return Math.abs(multiLen - this.dot(v)) <= cosTol * multiLen;
}
}, {
key: "isOpposite",
value: function isOpposite(vec, tol) {
var checkZeroVec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var cosTol = tol ? tol.cosTol : _Tolerance.Tolerance.global.cosTol;
var distTol = tol ? tol.distTol : _Tolerance.Tolerance.global.distTol;
var len1 = this.length;
var len2 = vec.length;
if (len1 === 0 || len2 === 0 || checkZeroVec && (_MathUtils.MathUtils.isZero(len1, distTol) || _MathUtils.MathUtils.isZero(len2, distTol))) {
return false;
}
var multiLen = len1 * len2;
return Math.abs(multiLen + this.dot(vec)) <= cosTol * multiLen;
}
}, {
key: "fromArray",
value: function fromArray(array) {
var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
this.x = array[offset];
this.y = array[offset + 1];
return this;
}
}, {
key: "toArray",
value: function toArray() {
var array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
array[offset] = this.x;
array[offset + 1] = this.y;
return array;
}
}, {
key: "toVector3",
value: function toVector3() {
return new _Vector.Vector3(this.x, this.y, 0);
}
}, {
key: "rotateAround",
value: function rotateAround(center, angle) {
var c = Math.cos(angle);
var 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;
}
}, {
key: "random",
value: function random() {
this.x = Math.random();
this.y = Math.random();
return this;
}
}], [{
key: "fromObject",
value: function fromObject(obj) {
return new Vector2(obj.x, obj.y);
}
}]);
}();