Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Plotsh 1.0
- #
- # Arkanon <arkanon@lsd.org.br>
- # 2012/12/18 Thu 16:45:01
- #
- # Output sample: <http://goo.gl/y32hh>
- export LC_ALL=C
- export TERM=xterm
- resize &> /dev/null
- tput civis
- oldstty=$(stty -g)
- stty -echo -icanon time 0 intr '^-'
- clear
- black=0
- blue=1
- green=2
- cyan=3
- red=4
- magenta=5
- yellow=6
- white=7
- vxradio=10
- vyradio=5
- bg=$black
- style="b"
- rxradio=$(echo "scale=3;$(tput cols )/2" | bc)
- ryradio=$(echo "scale=3;$(tput lines)/2" | bc)
- xfactor=$(echo "scale=3;$rxradio/$vxradio" | bc)
- yfactor=$(echo "scale=3;$ryradio/$vyradio" | bc)
- print()
- {
- local _c=$1 # column
- local _l=$2 # line
- local _f=$3 # foreground color
- local _b=$4 # background color
- local _s=$5 # style
- local _t=$6 # text
- [ $_l = c ] && _l=$[ $(tput lines) /2]
- [ $_c = c ] && _c=$[($(tput cols )-${#_t})/2]
- [ $_l -lt 0 -o $_l = -0 ] && _l=$[$(tput lines)+$_l-1 ]
- [ $_c -lt 0 -o $_c = -0 ] && _c=$[$(tput cols )+$_c-${#_t}]
- tput setf $_f
- tput setb $_b
- case $_s in
- b) tput bold ;; # bold
- u) tput smul ;; # underlined
- r) tput rev ;; # reverse
- *) tput dim ;; # normal
- esac
- tput sc
- tput cup $_l $_c
- echo -en "$_t"
- tput rc
- tput sgr0
- }
- round()
- {
- # round(a) = sign(a)*(abs(a)+.5)
- # sign(a) = a/abs(a)
- # abs(a) = ${a#-}
- [ $1 = 0 ] && echo 0 || echo "($1/${1#-})*(${1#-}+.5)/1" | bc
- }
- map()
- {
- local x=$1
- local y=$2
- local c=$(echo "scale=3; ($vxradio+1*$x)*$xfactor" | bc)
- local l=$(echo "scale=3; ($vyradio-1*$y)*$yfactor" | bc)
- echo "$c $l"
- echo "$(round $c) $(round $l)"
- }
- plot()
- {
- local fn=$1
- local incr=$2
- local fg=$3
- local dot=$4
- for x in $(seq -$vxradio $incr $vxradio)
- do
- y=$(echo "scale=3;"$(echo $fn | sed "s/x/$x/g") | bc -l)
- (( $(bc <<< "$y>-$vyradio") > 0 & $(bc <<< "$y< $vyradio") > 0 )) && print $(map $x $y | tail -n1) $fg $bg $style "$dot"
- done
- }
- # examples
- plot "s(x)" .1 $red "o"
- plot "x^2/2-3" .05 $blue "+"
- plot "-1*x/2-.5" .1 $green "."
- tput cnorm
- stty "$oldstty"
- # EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement