UNPKG

@tdurtschi/bug

Version:

Bug is a simulation that models a weevil walking around in an HTML canvas.

49 lines 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TreeStruct = void 0; var Victor = require("victor"); var TreeStruct = (function () { function TreeStruct(depth, parent, maxDepth) { var _this = this; if (depth === void 0) { depth = 1; } if (maxDepth === void 0) { maxDepth = 6; } this.parent = null; this.left = null; this.right = null; this.node = new Victor(0, 1); this.depth = 0; this.update = function () { var diff = (_this.maxSize - _this.node.y * (Math.pow(_this.depth, _this.growthFactor))) / 100; if (_this.maxSize - _this.node.y < (_this.maxSize / 100)) { return; } _this.node.addScalarY(Math.sin(_this.node.direction()) * diff); _this.node.addScalarX(Math.cos(_this.node.direction()) * diff); if (_this.left == null && _this.right == null && _this.node.magnitude() > _this.branchFactor && _this.depth < 6) { _this.left = new TreeStruct(_this.depth + 1, _this); _this.left.node.rotateDeg(_this.node.direction() + _this.branchAngle); _this.right = new TreeStruct(_this.depth + 1, _this); _this.right.node.rotateDeg(_this.node.direction() - _this.branchAngle); } _this.updateLeft(); _this.updateRight(); }; this.updateLeft = function () { return _this.left && _this.left.update(); }; this.updateRight = function () { return _this.right && _this.right.update(); }; this.depth = depth; this.maxDepth = maxDepth; this.maxSize = range2(80, 5); this.branchFactor = range2(10, 10); this.branchAngle = range2(60, 25); this.growthFactor = rangeDecimal2(1.2, 0.2, 1); this.parent = parent; } return TreeStruct; }()); exports.TreeStruct = TreeStruct; var range2 = function (mean, variance) { return Math.floor(Math.random() * variance) + mean - (variance / 2); }; var rangeDecimal2 = function (mean, variance, places) { var multiplier = Math.pow(10, places); return range2(mean * multiplier, variance * multiplier) / multiplier; }; //# sourceMappingURL=treeStruct.js.map