geometric-pack
Version:
Geometric pack with lots of available calculations for 2D and 3D geometry
148 lines • 5.46 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuadraticFunction = void 0;
var normalize_result_1 = require("../../utils/normalize-result");
var QuadraticFunction = /** @class */ (function () {
function QuadraticFunction() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this.validateInput(args);
this.a = args[0];
this.b = args[1];
this.c = args[2];
}
QuadraticFunction.prototype.validateInput = function (args) {
args.forEach(function (arg) {
if (typeof arg !== "number") {
throw new Error("Argument must be a number");
}
});
if (args.length !== 3) {
throw new Error("QuadraticFunction constructor takes 3 arguments");
}
if (args[0] === 0) {
throw new Error("Coefficient 'a' can't be equal 0");
}
};
QuadraticFunction.prototype.getDefinition = function () {
return {
a: this.a,
b: this.b,
c: this.c,
delta: this.getDelta(),
solutions: this.getSolutions(),
vertex: this.getVertex(),
monotonicity: this.getMonotonicity(),
positiveRange: this.getPositiveRange(),
negativeRange: this.getNegativeRange(),
};
};
QuadraticFunction.prototype.getDelta = function () {
return Math.pow(this.b, 2) - 4 * this.a * this.c;
};
QuadraticFunction.prototype.getSolutions = function () {
if (0 < this.getDelta()) {
var x1 = (-this.b - Math.sqrt(this.getDelta())) / (2 * this.a);
var x2 = (-this.b + Math.sqrt(this.getDelta())) / (2 * this.a);
return [x1, x2].sort(function (a, b) { return a - b; });
}
if (0 === this.getDelta()) {
var x = (-this.b - Math.sqrt(this.getDelta())) / (2 * this.a);
return [x];
}
return [];
};
QuadraticFunction.prototype.getVertex = function () {
var x = -this.b / (2 * this.a);
var y = -this.getDelta() / (4 * this.a);
return {
x: x,
y: y,
};
};
QuadraticFunction.prototype.getMonotonicity = function () {
var p = this.getVertex().x;
var leftRange = [-Infinity, p];
var rightRange = [p, Infinity];
if (this.a > 0) {
return {
increasing: rightRange,
decreasing: leftRange,
};
}
return {
increasing: leftRange,
decreasing: rightRange,
};
};
QuadraticFunction.prototype.getPositiveRange = function () {
var solutions = this.getSolutions();
var yCoordinate = this.getVertex().y;
if (solutions.length === 2) {
if (this.a > 0) {
return [
[-Infinity, solutions[0]],
[solutions[1], Infinity],
];
}
return [[solutions[0], solutions[1]]];
}
if (solutions.length === 1) {
if (this.a > 0) {
return [
[-Infinity, solutions[0]],
[solutions[1], Infinity],
];
}
return [];
}
if (yCoordinate > 0) {
return [[-Infinity, Infinity]];
}
return [];
};
QuadraticFunction.prototype.getNegativeRange = function () {
var solutions = this.getSolutions();
var yCoordinate = this.getVertex().y;
if (solutions.length === 2) {
if (this.a > 0) {
return [[solutions[0], solutions[1]]];
}
return [
[-Infinity, solutions[0]],
[solutions[1], Infinity],
];
}
if (solutions.length === 1) {
if (this.a > 0) {
return [];
}
return [
[-Infinity, solutions[0]],
[solutions[1], Infinity],
];
}
if (yCoordinate > 0) {
return [];
}
return [[-Infinity, Infinity]];
};
QuadraticFunction = __decorate([
normalize_result_1.NormalizeResults(),
__metadata("design:paramtypes", [Number])
], QuadraticFunction);
return QuadraticFunction;
}());
exports.QuadraticFunction = QuadraticFunction;
//# sourceMappingURL=quadratic-function.js.map