UNPKG

@dominicstop/utils

Version:

Yet another event emitter written in typescript.

124 lines (123 loc) 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Triangle = void 0; const Line_1 = require("./Line"); const Point_1 = require("./Point"); /// ``` /// top point /// + /// / \ /// leading --> / \ < trailing /// side / \ side /// / \ /// leading > +---------+ < trailing /// point ^ point /// bottom /// side /// ``` class Triangle { constructor(args) { this.topPoint = new Point_1.Point(args.topPoint); this.leadingPoint = new Point_1.Point(args.leadingPoint); this.trailingPoint = new Point_1.Point(args.trailingPoint); } ; // MARK: - Computed Properties // --------------------------- get asValue() { return { topPoint: this.topPoint, leadingPoint: this.leadingPoint, trailingPoint: this.trailingPoint, }; } ; get leadingSide() { return new Line_1.Line({ startPoint: this.topPoint, endPoint: this.leadingPoint }); } ; get trailingSide() { return new Line_1.Line({ startPoint: this.topPoint, endPoint: this.trailingPoint }); } ; get bottomSide() { return new Line_1.Line({ startPoint: this.leadingPoint, endPoint: this.trailingPoint }); } ; get centerLine() { const bottomMidPoint = this.bottomSide.midPoint; return new Line_1.Line({ startPoint: this.topPoint, endPoint: bottomMidPoint, }); } ; get height() { const bottomMidPoint = this.bottomSide.midPoint; const distanceSigned = this.topPoint.getDistance(bottomMidPoint); return Math.floor(distanceSigned); } ; get width() { return this.bottomSide.distance; } ; get centroid() { const sumTotalOfAllPoints = Point_1.Point.sumOfAllPoints(this.topPoint, this.leadingPoint, this.trailingPoint); return new Point_1.Point({ x: sumTotalOfAllPoints.x / 3, y: sumTotalOfAllPoints.y / 3, }); } ; get area() { const { x: x1, y: y1 } = this.topPoint; const { x: x2, y: y2 } = this.leadingPoint; const { x: x3, y: y3 } = this.trailingPoint; const area = 0.5 * Math.abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)); return area; } ; get isCollinear() { return isNaN(this.area) || this.area === 0; } ; // MARK: - Methods // --------------- clone() { return new Triangle(this.asValue); } ; /// Resize triangle to new height, preserving slope, and topmost point /// (i.e. the resizing is pinned to the top). /// /// ``` /// /\ /\ /\ /// / \ -> / \ -> '--' /// / \ '----' /// '------' /// ``` /// resizeTriangleRelativeToTopPoint(newHeight) { const centerLineCurrent = this.centerLine; const { percentTraversed } = centerLineCurrent.traverseByDistance(newHeight); const leadingPointNext = this.leadingSide.traverseByPercent(percentTraversed); const trailingPointNext = this.trailingSide.traverseByPercent(percentTraversed); this.leadingPoint = leadingPointNext; this.trailingPoint = trailingPointNext; } ; } exports.Triangle = Triangle; ;