trigplot-h.py

"""
Created on Mon Apr  5 11:14:01 2021

@author: rmontant
"""
import math
import shutil

def myftn(x, scale):
    return (math.sin(x) * math.cos(scale * x))
#--------

def plot_function(xvalues, yvalues, width, fill=' '):
    nsteps = len(xvalues)
    for n in range(nsteps):
        line = '{:5.2f}: '.format(xvalues[n]) \
            + fill * int(width*(yvalues[n]+1)/2) \
            + '{:.2f}'.format(yvalues[n])
        print(line)
#--------

def plot_horizontal(xvalues, yvalues, width, fill=' '):
    width = shutil.get_terminal_size()[0] - 8
    height = shutil.get_terminal_size()[1] - 2

    xmax = max(xvalues)
    rows =[]
    cols =[]
    for i, x in enumerate(xvalues):
        y = yvalues[i]
        for c in range(0, width):
            if c > (width * x/xmax) and (c+1 >= (width * x/xmax)):
                cols.append(x)
                rows.append(y)

    for r in range(height, 0, -1):
        print('{:5.2f}'.format(2*r/height - 1), end=': ')
        for c in range(width):
            if (2*r/height - 1) > rows[c]:
                print(' ', end='')
            else:
                print('*', end='')
        print()
#--------               

def main(argv=[__name__]):
    if len(argv) == 3:
        width = int(argv[1])
        scale = float(argv[2])
    else:
        width = shutil.get_terminal_size()[0] - 10
        print(f'terminal width {width:d}')
        scale = float(input('Scale? '))

    x = 1e-99
    xmax = 8 * math.pi
    nsteps = 200
    xstep = (xmax - x) / nsteps
    xvalues, yvalues = [], []

    while x < xmax:
        xvalues.append(x)
        yvalues.append(myftn(x, scale))
        x += xstep

    #plot_function(xvalues, yvalues, width, fill='*')    
    plot_horizontal(xvalues, yvalues, width, fill='*')
#--------

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))
#--------