vecks
Version:
2D and 3D Vector Algebra library
75 lines (65 loc) • 2.35 kB
JavaScript
;
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 V2 = /*#__PURE__*/function () {
function V2(x, y) {
_classCallCheck(this, V2);
if (_typeof(x) === 'object') {
this.x = x.x;
this.y = x.y;
} else {
this.x = x;
this.y = y;
}
}
_createClass(V2, [{
key: "equals",
value: function equals(other) {
return this.x === other.x && this.y === other.y;
}
}, {
key: "length",
value: function length() {
return Math.sqrt(this.dot(this));
}
}, {
key: "neg",
value: function neg() {
return new V2(-this.x, -this.y);
}
}, {
key: "add",
value: function add(b) {
return new V2(this.x + b.x, this.y + b.y);
}
}, {
key: "sub",
value: function sub(b) {
return new V2(this.x - b.x, this.y - b.y);
}
}, {
key: "multiply",
value: function multiply(w) {
return new V2(this.x * w, this.y * 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;
}
}]);
return V2;
}();
var _default = V2;
exports["default"] = _default;