UNPKG

geometric-pack

Version:

Geometric pack with lots of available calculations for 2D and 3D geometry

95 lines 4.1 kB
"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.Polygon = void 0; var transform_1 = require("../../utils/transform"); var normalize_result_1 = require("../../utils/normalize-result"); var Polygon = /** @class */ (function () { function Polygon() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } this.validateInput(args); this.sideLength = args[0]; this.vertexCount = args[1]; } Polygon.prototype.validateInput = function (args) { args.forEach(function (arg) { if (typeof arg !== "number") { throw new Error("Argument must be a number"); } }); if (args.length !== 2) { throw new Error("Polygon constructor takes 2 arguments"); } if (this.hasNegative(args)) { throw new Error("Side length and vertex count must be positive numbers"); } if (this.requirement(args[1])) { throw new Error("Vertex count must be between 3 and 14"); } }; Polygon.prototype.hasNegative = function (_a) { var sideLength = _a[0], vertexCount = _a[1]; return sideLength <= 0 || vertexCount <= 0; }; Polygon.prototype.requirement = function (vertexCount) { return vertexCount < 3 || vertexCount > 14; }; Polygon.prototype.getDefinition = function () { return { sideLength: this.sideLength, vertexCount: this.vertexCount, circumference: this.getCircumference(), area: this.getArea(), angels: this.getAngles(), outerCircleRadius: this.getOuterCircleRadius(), innerCircleRadius: this.getInnerCircleRadius(), }; }; Polygon.prototype.getCircumference = function () { return this.sideLength * this.vertexCount; }; Polygon.prototype.getArea = function () { return ((1 / 4) * this.vertexCount * Math.pow(this.sideLength, 2) * (1 / Math.tan(Math.PI / this.vertexCount))); }; Polygon.prototype.getInteriorAngle = function () { var radians = ((this.vertexCount - 2) * Math.PI) / this.vertexCount; return transform_1.transformRadiansToDegrees(radians); }; Polygon.prototype.getExteriorAngle = function () { var radians = (2 * Math.PI) / this.vertexCount; return transform_1.transformRadiansToDegrees(radians); }; Polygon.prototype.getAngles = function () { return { interiorAngle: this.getInteriorAngle(), exteriorAngle: this.getExteriorAngle(), }; }; Polygon.prototype.getOuterCircleRadius = function () { return ((1 / 2) * this.sideLength * (1 / Math.sin(Math.PI / this.vertexCount))); }; Polygon.prototype.getInnerCircleRadius = function () { return ((1 / 2) * this.sideLength * (1 / Math.tan(Math.PI / this.vertexCount))); }; Polygon = __decorate([ normalize_result_1.NormalizeResults(), __metadata("design:paramtypes", [Number]) ], Polygon); return Polygon; }()); exports.Polygon = Polygon; //# sourceMappingURL=polygon.js.map