Skip to content

Instantly share code, notes, and snippets.

@ueokande
Last active December 29, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ueokande/7687234 to your computer and use it in GitHub Desktop.
Save ueokande/7687234 to your computer and use it in GitHub Desktop.
A script which draws sin graph in terminal
#!/bin/bash
## Put a pixel at (x,y). Position x and y are given by argument $1
## and $2. The color of the pixel is given by $3, which runs 0 - 7
function putPixel {
if [ $# -lt 3 ]; then
echo too few arguments >&2
return 1
fi
x=`echo "$1 * 2" | bc`
y=$2
fg=3$3
bg=4$3
printf "\033[${y};${x}f"
printf "\033[$attr;${bg};${fg}m \033[m"
}
## Returns a number as integer from a decimal. A decimal is given by $1.
function decimalToInt {
tmp=${1/.*}
if [ -z $tmp ]; then
echo 0
else
echo $tmp
fi
}
size=(`stty size`)
terminal_width=${size[1]}
terminal_height=${size[0]}
pi2=`echo "4*a(1) * 2" | bc -l`
clear
color=0
for i in `seq 0 0.2 $pi2`; do
color=`echo "($color + 1) % 8" | bc`
x=`echo "$i / $pi2 * $terminal_width / 2" | bc -l`
y=`echo "-s($i) / 2 * $terminal_height + $terminal_height / 2" | bc -l`
intx=`decimalToInt $x`
inty=`decimalToInt $y`
putPixel $intx $inty $color
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment