vecks
Version:
2D and 3D Vector Algebra library
90 lines (79 loc) • 2.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var V3 = /*#__PURE__*/function () {
function V3(x, y, z) {
_classCallCheck(this, V3);
if (_typeof(x) === 'object') {
this.x = x.x;
this.y = x.y;
this.z = x.z;
} else if (x === undefined) {
this.x = 0;
this.y = 0;
this.z = 0;
} else {
this.x = x;
this.y = y;
this.z = z;
}
}
_createClass(V3, [{
key: "equals",
value: function equals(other, eps) {
if (eps === undefined) {
eps = 0;
}
return Math.abs(this.x - other.x) <= eps && Math.abs(this.y - other.y) <= eps && Math.abs(this.z - other.z) <= eps;
}
}, {
key: "length",
value: function length() {
return Math.sqrt(this.dot(this));
}
}, {
key: "neg",
value: function neg() {
return new V3(-this.x, -this.y, -this.z);
}
}, {
key: "add",
value: function add(b) {
return new V3(this.x + b.x, this.y + b.y, this.z + b.z);
}
}, {
key: "sub",
value: function sub(b) {
return new V3(this.x - b.x, this.y - b.y, this.z - b.z);
}
}, {
key: "multiply",
value: function multiply(w) {
return new V3(this.x * w, this.y * w, this.z * w);
}
}, {
key: "norm",
value: function norm() {
return this.multiply(1 / this.length());
}
}, {
key: "dot",
value: function dot(b) {
return this.x * b.x + this.y * b.y + this.z * b.z;
}
}, {
key: "cross",
value: function cross(b) {
return new V3(this.y * b.z - this.z * b.y, this.z * b.x - this.x * b.z, this.x * b.y - this.y * b.x);
}
}]);
return V3;
}();
var _default = V3;
exports["default"] = _default;