Functions and Fractals - Recursive Trees - Bash!

  • + 0 comments
    declare -A matrix
    NUM_ROWS=63
    NUM_COLS=100
    
    for ((i=1; i<=NUM_ROWS; i++)) do
        for ((j=1; j<=NUM_COLS; j++)) do
            matrix["$i,$j"]="_"
        done 
    done
    
    read RECURSION_LEVEL
      
    func() {
        local COL="$1"   
        local N="$2" 
          
        local ROW=$((2**(7-$N)))   
        local SIZE=$(( 2**(5-$N) ))  
         
        for ((height=0; height<=$SIZE; height++)) do
            row=$(( $ROW - $height ))  
            matrix[$row,$COL]="1" 
            
            row=$(( $row -$SIZE ))  
            col1=$(( $COL+$height ))
            col2=$(( $COL-$height ))
            matrix[$row,$col1]="1"
            matrix[$row,$col2]="1"
        done   
     
        if [[ $N -lt $RECURSION_LEVEL && $SIZE -gt 1 ]]; then  
            func $(($COL-$SIZE)) $((N+1))  
            func $(($COL+$SIZE)) $((N+1))
        fi 
    }
      
    func 50 1
      
    for ((i=1; i<=NUM_ROWS; i++)) do
        for ((j=1; j<=NUM_COLS; j++)) do
            echo -n ${matrix["$i,$j"]}
        done
        echo 
    done