#!/usr/bin/env sh # # Modified version of bash mandelbrot from Mecki stackoverflow.com/users/15809 # found at https://stackoverflow.com/a/63749612/1765658 # Avoiding forks and running bc as background task, reducing # execution time, from many hours to less than 10' (on my desktop) # # Modified version for POSIX shell by Léa Gris: # https://gist.github.com/leagris/59e1b7e72462024b278652696f375e71 # Improved to addressing fifos by FDs. BAILOUT=16 MAX_ITERATIONS=1000 ALPHWIDTH=$(( $(tput cols) / 2 )) AWIDE=$(( ALPHWIDTH -1 )) bcin=$(mktemp -u) bcout=$(mktemp -u) mkfifo "$bcin" "$bcout" exec 8<>"$bcin" exec 9<>"$bcout" bc -l <"$bcin" >"$bcout" & cat >&8 < bailout ) return i zr=zr2;zi=zi2 } return 0 } 0 InitBC read -r foo <&9 # Wait for bc before rm fifos rm "$bcin" "$bcout" bc() { retval=$1 shift echo >&8 "$@" read <&9 -r "$retval" } mandelbrot () { y=$((0-AWIDE)) while [ $y -lt $AWIDE ] ;do echo x=$((0-AWIDE)) while [ $x -lt $AWIDE ] ;do bc i "iterate ( $x / alphwidth, $y / alphwidth )" if [ "$i" -eq 0 ]; then printf "*" else printf " " fi x=$((x+1)) done y=$((y+1)) done echo } mandelbrot