Quick actions

cmd+k|ctrl+k

Navigation

Languages

Python 03a Sin

Snippet info

Language

Python

Visibility

public

Author

tony

Created

2018-08-27T13:42:05Z

Updated

2018-09-03T12:00:56Z

# Draw a character based sin wave

# https://docs.python.org/3/library/math.html#trigonometric-functions
# math.sin(x) -- Return the sine of x radians.

# This is not the neatest code in the world. It doesn't format that well when zero is varied.

import math

print('Sin(x)\n')

zero = 40
for x in range(0, 360, 5):
    r = math.radians(x)
    y = math.sin(r)
    
    scaled = int((y + 1) * zero)
    
    if abs(y) < 0.00001: # floating point numbers are not exact
        print('{:03.0f} {:+1.4f}'.format(x,y), ' ' * scaled, '*')
    elif y > 0:
        print('{:03.0f} {:+1.4f}'.format(x,y), ' ' * zero, '.' + ' ' * (scaled - zero) + '*')
    else:
        print('{:03.0f} {:+1.4f}'.format(x,y), ' ' * scaled + '*' + ' ' * (zero - scaled - 1), '.')
INFO