Advertisement
arkanon

Plotsh

Dec 18th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.14 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Plotsh 1.0
  4. #
  5. # Arkanon <arkanon@lsd.org.br>
  6. # 2012/12/18 Thu 16:45:01
  7. #
  8. # Output sample: <http://goo.gl/y32hh>
  9.  
  10. export LC_ALL=C
  11. export   TERM=xterm
  12.  
  13. resize &> /dev/null
  14. tput civis
  15. oldstty=$(stty -g)
  16. stty -echo -icanon time 0 intr '^-'
  17. clear
  18.  
  19.  
  20.   black=0
  21.    blue=1
  22.   green=2
  23.    cyan=3
  24.     red=4
  25. magenta=5
  26.  yellow=6
  27.   white=7
  28.  
  29.  
  30. vxradio=10
  31. vyradio=5
  32.      bg=$black
  33.   style="b"
  34.  
  35.  
  36. rxradio=$(echo "scale=3;$(tput cols )/2"   | bc)
  37. ryradio=$(echo "scale=3;$(tput lines)/2"   | bc)
  38. xfactor=$(echo "scale=3;$rxradio/$vxradio" | bc)
  39. yfactor=$(echo "scale=3;$ryradio/$vyradio" | bc)
  40.  
  41.  
  42. print()
  43. {
  44.   local _c=$1       # column
  45.   local _l=$2       # line
  46.   local _f=$3       # foreground color
  47.   local _b=$4       # background color
  48.   local _s=$5       # style
  49.   local _t=$6       # text
  50.   [ $_l = c ]               && _l=$[ $(tput lines)        /2]
  51.   [ $_c = c ]               && _c=$[($(tput cols )-${#_t})/2]
  52.   [ $_l -lt 0 -o $_l = -0 ] && _l=$[$(tput lines)+$_l-1     ]
  53.   [ $_c -lt 0 -o $_c = -0 ] && _c=$[$(tput cols )+$_c-${#_t}]
  54.   tput setf $_f
  55.   tput setb $_b
  56.   case $_s in
  57.     b) tput bold ;; # bold
  58.     u) tput smul ;; # underlined
  59.     r) tput rev  ;; # reverse
  60.     *) tput dim  ;; # normal
  61.   esac
  62.   tput sc
  63.   tput cup  $_l $_c
  64.   echo -en "$_t"
  65.   tput rc
  66.   tput sgr0
  67. }
  68.  
  69.  
  70. round()
  71. {
  72.   # round(a) = sign(a)*(abs(a)+.5)
  73.   #  sign(a) = a/abs(a)
  74.   #   abs(a) = ${a#-}
  75.   [ $1 = 0 ] && echo 0 || echo "($1/${1#-})*(${1#-}+.5)/1" | bc
  76. }
  77.  
  78.  
  79. map()
  80. {
  81.   local x=$1
  82.   local y=$2
  83.   local c=$(echo "scale=3; ($vxradio+1*$x)*$xfactor" | bc)
  84.   local l=$(echo "scale=3; ($vyradio-1*$y)*$yfactor" | bc)
  85.   echo "$c $l"
  86.   echo "$(round $c) $(round $l)"
  87. }
  88.  
  89.  
  90. plot()
  91. {
  92.   local   fn=$1
  93.   local incr=$2
  94.   local   fg=$3
  95.   local  dot=$4
  96.   for x in $(seq -$vxradio $incr $vxradio)
  97.   do
  98.     y=$(echo "scale=3;"$(echo $fn | sed "s/x/$x/g") | bc -l)
  99.     (( $(bc <<< "$y>-$vyradio") > 0 & $(bc <<< "$y< $vyradio") > 0 )) && print $(map $x $y | tail -n1) $fg $bg $style "$dot"
  100.   done
  101. }
  102.  
  103.  
  104. # examples
  105.   plot "s(x)"      .1  $red   "o"
  106.   plot "x^2/2-3"   .05 $blue  "+"
  107.   plot "-1*x/2-.5" .1  $green "."
  108.  
  109.  
  110. tput cnorm
  111. stty "$oldstty"
  112.  
  113.  
  114. # EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement