You are viewing a single comment's thread. Return to all comments →
#!/bin/bash #Usage script [branch_size] [depth] #example ./script 10 4 ./script 77 7 pad=$(printf "_%.0s" {1..500}) create_fractal_pattern() { local left_length=${1} local right_length=${2} local branch_size=${3} local depth=${4} if [[ ${depth} == 0 ]] then return fi paste -d "" <( create_fractal_pattern $(( left_length - branch_size )) ${branch_size} $(( branch_size / 2 )) $(( depth - 1 )) ) \ <( create_fractal_pattern $(( branch_size - 1 )) $(( right_length - branch_size )) $(( branch_size / 2 )) $(( depth - 1 )) ) for (( i=${branch_size}-1; i>=0; i-=1 )) do printf "%s1%s1%s\n" "${pad:1:left_length-i-1}" "${pad:1:i*2+1}" "${pad:1:right_length-i-1}" done for (( i=0; i<${branch_size}; i+=1 )) do printf "%s1%s\n" ${pad:1:left_length} ${pad:1:right_length} done } compute_max_width() { local branch_size=${1} local depth=${2} local res=0 for (( i=0; i<depth; ++i )) do res=$(( res + branch_size )) branch_size=$(( branch_size / 2 )) done echo $(( res * 2 + 7 )) } draw_fractal() { local branch_size=${1} local depth=${2} local width=$(compute_max_width ${branch_size} ${depth}) local left_bound=$(( (width + 1) / 2 -1 )) local right_bound=$(( width / 2 )) printf "%s\n" ${pad:1:width} create_fractal_pattern ${left_bound} ${right_bound} ${branch_size} ${depth} } draw_fractal ${1} ${2} | tr ' ' '\n'
Seems like cookies are disabled on this browser, please enable them to open this website
Functions and Fractals - Recursive Trees - Bash!
You are viewing a single comment's thread. Return to all comments →