Functions and Fractals - Recursive Trees - Bash!

Sort by

recency

|

156 Discussions

|

  • + 0 comments

    BASH

    #!/bin/bash
    
    declare -A line_locs
    read n
    
    iterate() {
        local lines=$1 o_loc=$2 nn=$3 one_lines=$4
        [[ $nn -eq 0 ]] && return
        for ((x=1; x<=one_lines; x++)); do
            line_locs[$lines]+=" $o_loc"
            ((lines--))
        done
        local loc_left=$o_loc loc_right=$o_loc
        for ((t=1; t<=one_lines; t++)); do
            ((loc_left--, loc_right++))
            line_locs[$lines]+=" $loc_left $loc_right"
            ((lines--))
        done
        iterate $lines $loc_left $((nn-1)) $((one_lines/2))
        iterate $lines $loc_right $((nn-1)) $((one_lines/2))
    }
    
    generate_ones() {
        printf '_%.0s' {1..100}; echo
        for ((j=2; j<=63; j++)); do
            read -ra positions <<< "${line_locs[$j]}"
            undercore=""
            for ((s=1; s<=100; s++)); do
                found=0
                for pos in "${positions[@]}"; do
                    [[ $s -eq $pos ]] && { found=1; break; }
                done
                [[ $found -eq 1 ]] && undercore+="1" || undercore+="_"
            done
            echo "$undercore"
        done
    }
    
    iterate 63 50 $n 16
    generate_ones
    
  • + 0 comments
    #!/bin/bash
    
    declare -A line_locs
    
    read n
    
    
    
    iterate(){
            local lines=$1 o_loc=$2 nn=$3 one_lines=$4
            if [[ $nn -eq 0 ]]; then
                    return 0
            fi
    
    
            for ((x=1; x<=$one_lines;x++));do
                    line_locs[$lines]+=" $o_loc"
                    ((lines--))
            done
            local loc_left=$o_loc loc_right=$o_loc
            for ((t=1;t<=$one_lines;t++));do
                    ((loc_left--))
                    ((loc_right++))
                    line_locs[$lines]+=" $loc_left $loc_right"
                    ((lines--))
            done
            iterate $lines $loc_left $((nn-1)) $((one_lines/2))
            iterate $lines $loc_right $((nn-1)) $((one_lines/2))
    }
    
    generate_ones(){
            printf '_%.0s' {1..100}
            echo
            for ((j=2;j<=63;j++));do
                    list_locs=${line_locs[$j]}
                    read -ra positions <<< "$list_locs"
                    undercore=""
    
                    for ((s=1; s<=100;s++));do
                            found=0
                            for pos in "${positions[@]}";do
                                    if [[ $s -eq $pos ]];then
                                            found=1
                                            break
                                    fi
                            done
                            if [[ $found -eq 1 ]]; then
                                    undercore+="1"
                            else
                                    undercore+="_"
                            fi
                    done
    
                    echo "$undercore"
            done
    
    }
    iterate 63 50 $n 16
    generate_ones
    
  • + 0 comments

    In Bash, functions are blocks of reusable code, which is essential for recursion. For a recursive tree, a function typically represents drawing a branch and then calling itself to draw sub-branches. Cric Adda

  • + 4 comments

    Kind of sad this is not a valid answer but i get it, it is a really inefficient solution

    rows=63
    cols=100
    max_it=5
    first_y_size=16
    
    declare -A fractal_positions
    
    function fractals {
        local it=$1
        local row=$2
        local col=$3
        local y_size=$4
        
        if (( $it > $max_it)) || (( $it > $N )); then
            return
        fi
        
        fractals $(( $it + 1 )) $(( $row - $y_size * 2 )) $(( $col - $y_size )) $(( $y_size / 2 ))
        fractals $(( $it + 1 )) $(( $row - $y_size * 2 )) $(( $col + $y_size )) $(( $y_size / 2 ))
        
        for (( i = $row; i > $row - $y_size; i--)); do
            fractal_positions[$(printf "%d|%d" $((i-1)) $col)]=1
        done
        
        local tmp=1
        for (( i = $row - $y_size; i > $row - $y_size * 2; i--)); do
            fractal_positions[$(printf "%d|%d" $((i-1)) $(( $col - $tmp )))]=1
            fractal_positions[$(printf "%d|%d" $((i-1)) $(( $col + $tmp )))]=1
            tmp=$(($tmp+1))
        done
    }
    
    read -r N
    
    fractals 1 $rows $(( $cols / 2 )) $first_y_size
    
    for ((i=0; i < $rows; i++)); do
        for ((j=0; j < $cols; j++)); do
            if ((fractal_positions[$(printf "%d|%d" $i $j)] == 1)); then
                printf "1"
            else
                printf "_"
            fi
        done
        printf "\n"
    done
    
  • + 0 comments

    ITERATIVE version (ultra-compact)

    • If you know how to format the code better on this broken editor, please let me know.
    • If there is a way to update my submitted answer, please let me know
    • *
    #!/bin/bash
    read tree_depth
    xpos=(50) # Roots where we start the Ys
    for ((y=1; y<=63; y++)); do
      for ((x=1; x<=100; x++)); do
        char=_
          if (( tree_depth > printed )); then # if we need more depth
            length=$((2**(4-printed))) b=y-offset-length
            # b is the y coordinate from the point of view of the center of the Y we are drawing
            if (( b <= length )); then
              (( b < 0 )) && b=0 # Force vertical (to draw a Y instead of an X)
              for p in "${xpos[@]}"; do
                if (( p-b == x || p+b == x )); then
                  char=1
                  # If b == length, we are finishing drawing this rank's Ys
                  # So we save the x position for future Y roots
                  (( b == length )) && new_xpos+=("$x")
                fi
              done
            fi
          fi
        printf %s "$char"
      done
      if (( b == length )); then
        # We finished drawing the Ys for this level
        # We set the new roots and the offset for coordinate computation
        xpos=("${new_xpos[@]}") new_xpos=() printed=$((printed+1)) offset=$y
      fi
      echo
    done | tac