UNPKG

@turbox3d/math

Version:

Large-scale graphics application math library

96 lines (95 loc) 2.91 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.Line3 = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _Vector = require("./Vector3"); var _MathUtils = require("../MathUtils"); var _startP = new _Vector.Vector3(); var _startEnd = new _Vector.Vector3(); var Line3 = exports.Line3 = /*#__PURE__*/function () { function Line3(start, end) { (0, _classCallCheck2["default"])(this, Line3); this.start = start !== undefined ? start : new _Vector.Vector3(); this.end = end !== undefined ? end : new _Vector.Vector3(); } return (0, _createClass2["default"])(Line3, [{ key: "set", value: function set(start, end) { this.start.copy(start); this.end.copy(end); return this; } }, { key: "clone", value: function clone() { return new Line3().copy(this); } }, { key: "copy", value: function copy(line) { this.start.copy(line.start); this.end.copy(line.end); return this; } }, { key: "getCenter", value: function getCenter(target) { return target.addVectors(this.start, this.end).multiplyScalar(0.5); } }, { key: "delta", value: function delta(target) { return target.subVectors(this.end, this.start); } }, { key: "distanceSq", value: function distanceSq() { return this.start.distanceToSquared(this.end); } }, { key: "distance", value: function distance() { return this.start.distanceTo(this.end); } }, { key: "at", value: function at(t, target) { return this.delta(target).multiplyScalar(t).add(this.start); } }, { key: "closestPointToPointParameter", value: function closestPointToPointParameter(point, clampToLine) { _startP.subVectors(point, this.start); _startEnd.subVectors(this.end, this.start); var startEnd2 = _startEnd.dot(_startEnd); var startEndStartP = _startEnd.dot(_startP); var t = startEndStartP / startEnd2; if (clampToLine) { t = _MathUtils.MathUtils.clamp(t, 0, 1); } return t; } }, { key: "closestPointToPoint", value: function closestPointToPoint(point, clampToLine, target) { var t = this.closestPointToPointParameter(point, clampToLine); return this.delta(target).multiplyScalar(t).add(this.start); } }, { key: "applyMatrix4", value: function applyMatrix4(matrix) { this.start.applyMatrix4(matrix); this.end.applyMatrix4(matrix); return this; } }, { key: "equals", value: function equals(line) { return line.start.equals(this.start) && line.end.equals(this.end); } }]); }();