Last active
April 14, 2021 23:40
-
-
Save emilong/d3f496321f45e051bb84a5fc52670142 to your computer and use it in GitHub Desktop.
High-level outline of an asynchronous tree drawing algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// render the given branch, which is specified by its startingPoint, length, and angle | |
// If there are more branches to grow, grow randomly to the left and right | |
function growBranch({ startingPoint, length, angle, remainingBranches }) { | |
endingPoint = computeEndpoint({ startingPoint, length, angle }); | |
renderBranch({ startingPoint, endingPoint }); | |
const newRemainingBranches = remainingBranches - 1; | |
if (newRemainingBranches <= 0) { | |
return; | |
} | |
["left", "right"].forEach(direction => { | |
const newAngle = computeRandomAngle(direction); | |
const newLength = shrinkBranchLength(length); | |
setTimeout(function() { | |
growBranch({ | |
startingPoint: endingPoint, | |
length: newLength, | |
angle: newAngle, | |
remainingBranches: newRemainingBranches | |
}); | |
}, DELAY); | |
}); | |
} | |
// Starts off the tree by growing the first "branch," i.e. the trunk itself | |
function growTree() { | |
growBranch({ | |
startingPoint: GROUND, | |
length: TRUNK_LENGTH, | |
angle: STRAIGHT_UP, | |
remainingBranches: TREE_COMPLEXITY | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment