Here's a very funny possibility. The thing I'm going to show you here is probably not what you're looking for, it's probably not optimized, it's probably a whole bunch of junk, but it's really funny. (I'm not expecting any upvotes for it).
This script more or less plots a * at each point the coordinates of which are given in a file (given as first argument of the script). It computes the number of rows and columns of your terminal (using the command stty, if you happen to have it installed, it's good, otherwise, well, it's not going to work).
#!/bin/bash
isnumber() {
for i; do
[[ "$i" =~ [[:digit:]] ]] || return 1
[[ "$i" =~ ^[+-]?[[:digit:]]*\.?[[:digit:]]*$ ]] || return 1
done
return 0
}
die() {
echo >&2 "$@"
exit 1
}
[[ -n $1 ]] || die "You must provide an argument"
xarray=()
yarray=()
# Read file input
while read x y _; do
isnumber "$x" "$y" || continue
xarray+=( "$x" )
yarray+=( "$y" )
done < "$1"
# Check that we have at least one point
(( ${#xarray[@]} )) || die "Error, there's no valid point inf file \`$1'"
# Compute xmin, xmax, ymin and ymax:
read xmin xmax ymin ymax valid < <(
bc -l < <(
echo "ymin=${yarray[0]}; ymax=${yarray[0]}"
echo "xmin=${xarray[0]}; xmax=${xarray[0]}"
for i in "${!xarray[@]}"; do
echo "y=${yarray[i]}; if (ymax<y) ymax=y; if (ymin>y) ymin=y"
echo "x=${xarray[i]}; if (xmax<x) xmax=x; if (xmin>x) xmin=x"
done
echo 'print xmin," ",xmax," ",ymin," ",ymax'
# This will tell us if we have xmin<xmax and ymin<ymax
echo 'print " ",(xmin<xmax)*(ymin<ymax)'
)
)
# Check that xmin<xmax and ymin<ymax
(( valid )) || die "Error, ! ( (xmin<xmax) && (ymin<ymax) )"
# Get terminal's number of rows and columns
IFS=' ;' read _ _ _ _ nbrows _ nbcols _ < <(stty -a)
((nbrows-=1))
((maxcols=nbcols-1))
((maxrows=nbrows-1))
# Create an array full of spaces:
points=()
for ((i=0;i<nbrows*nbcols;++i)); do points+=( ' ' ); done
# Put a '*' at each x y in array points
while read r c; do
printf -v X "%.f" "$c"
printf -v Y "%.f" "$r"
points[X+Y*nbcols]='*'
done < <(
bc -l < <(
echo "xmin=$xmin; dx=$maxcols/($xmax-xmin)"
echo "ymax=$ymax; dy=$maxrows/(ymax-($ymin))"
for i in "${!xarray[@]}"; do
echo "print (ymax-(${yarray[i]}))*dy,\" \",(${xarray[i]}-xmin)*dx,\"\n\""
done
)
)
# Now, print it! The clear is not mandatory
clear
printf "%c" "${points[@]}"
Call the script plot_file_in_terminal (or maybe a shorter name).
You can try it, it's very funny, with a Mandelbrot set: the following script generates an M-set (you can give the number of pixels for the x and y coordinates as input, well, figure out a few things by yourself):
#!/bin/bash
die() {
echo >&2 "$@"
exit 1
}
nbx=${1:-100}
nby=${2:-100}
Nmax=${3:-100}
[[ $nbx =~ ^[[:digit:]]+$ ]] && ((nbx>5)) || die "First argument (nbx) must be an integer > 5"
[[ $nby =~ ^[[:digit:]]+$ ]] && ((nby>5)) || die "Second argument (nby) must be an integer > 5"
[[ $Nmax =~ ^[[:digit:]]+$ ]] && ((Nmax>5)) || die "Third argument (Nmax) must be an integer > 5"
xmin=-1.5
xmax=1
ymin=-1
ymax=1
bc -l <<EOF
for (k=0;k<$nbx;++k) {
for (l=0;l<$nby;++l) {
x0=k*($xmax-($xmin))/($nbx-1)+($xmin)
y0=l*($ymax-($ymin))/($nby-1)+($ymin)
x=x0
y=y0
isin=1
for (i=0;i<$Nmax;++i) {
if(x^2+y^2>1) {
isin=0
break
}
xn=x^2-y^2+x0
y=2*x*y+y0
x=xn
}
if(isin) print x0," ",y0,"\n"
}
}
EOF
Name it genMandelbrot and use it as ./genMandelbrot > Mset or ./genMandelbrot 100 50 > Mset if your terminal is more or less 100x50. Then:
./plot_file_in_terminal Mset
Nice, eh?
If you want a sine function (say from 0 to 2*pi):
bc -l <<< "for (i=0;i<400;++i) { x=i*6.28/400; print x,\" \",s(x),\"\\n\" }" > sine
Then call:
./plot_file_in_terminal sine
Outputs
Mandelbrot Set (in 80x24)
**
**********
***********
* *************** * *
*********************************** ****
**************************************** *
**********************************************
**************************************************
************** **************************************************
******************** ****************************************************
** **************************************************************************
****************************************************************************
** **************************************************************************
******************** ****************************************************
************** **************************************************
**************************************************
**********************************************
**************************************** *
*********************************** ****
* *************** * *
***********
**********
**
gniourf@somewhere:~/cool_path$
Sine (in 80x24)
*********
**** ***
*** **
** **
** **
** **
*** **
** **
** **
** **
** **
* ** *
** **
** **
** **
** **
** **
*** **
** **
*** ***
*** ***
*** ****
********
gniourf@somewhere:~/an_even_cooler_path$
In your OP you mentionned "the easiest way of doing"... well, you can notice, this solution it's not exactly a one-liner.
Edits.
- Robustness: check that
xmin!=xmax and ymin!=ymax so as to not divide by 0
- Speed: Computing
xmin, xmax, ymin, ymax in the same bc instance.
- Speed: Don't use
tput anymore, it was retarded and too slow! Instead, build an array of points (each field contains a space or *).
Todos.
- Lots of stuff to have something we can actually work with confortably. This exercise is left to the reader.