We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Functions and Fractals - Recursive Trees - Bash!
Functions and Fractals - Recursive Trees - Bash!
Sort by
recency
|
149 Discussions
|
Please Login in order to post a comment
I looked at the top few, and most basically hardcoded the tree. That's weak.
I was happy with my fairly unique answer. I can be cleaned up better, but I've already spent a few hours on this.
The basic idea was to form each tree based on a base row. At first the tree function was done in 3 parts - trunk, branch left, and branch right - but I was able to roll them into the same loop after I had all three working independantly.
From the base row, the trunk just copies the previous row (if there was a 1 at "base, col)" then you put a 1 at "trunkcur,col"). For the branches you look at the base row plus the offset so "base, col +/- i" (where i is the row difference from branchcur to base) to put a 1 in "branchcur, col +/- (base - branchcur)".
You add an additional 64th row that is just a 1 at "64,50" and now the trees can be built off that single starting point.
frac()
takes the number of iterations, starting line, and depth of the tree (starting line is always 63 and depth always starts at 16). Each recursive call decreaments iteration number (base case of 0 where nothing is printed at it returns), calculates a new starting row (decrementing 2 * length of last tree), and sets the next tree length to half the previous. Sofrac 5 63 16
callsfrac 4 31 8
which callsfrac 3 15 4
...Yes, this can all be done iteratively, but I was just too lazy to refactor it for such a small n. The nice thing about this approach is that all the constants are easily adjustable to make it work on any size board with any length trees and any number of branches even.
When the table is printed at the end, just remeber to leave off the 64th starting base row and you're good.
as a bash noob, this was tough, but some brute-force algos always work lol
EZ