# # Polt Sine Wave... # # # Replace a character in-place... # # s = 'foobar' # s = s[:3] + 'x' + s[4:] # replace the 3rd char with 'x' # print s import math as m y_0 = 20 y_scale = 10 x_resolution = 10 deltaAngle = m.pi/x_resolution x_ticks = 41 x_step = 1 # # Plot (1) - Sine # hbar = '-' * y_0 + '+' + '-' * y_0 # print '-' * y_0, '+', '-' * y_0 print hbar for x in range(0,x_ticks,x_step): bar = ' ' * 2 * y_0 sin = m.sin(deltaAngle * x) y = int(sin * y_scale) # # y = sin * y_scale bar = bar[:y_0+y]+'s'+bar[y_0+y+1:] # cos = m.cos(deltaAngle * x) # y = int(cos * y_scale) # # y = sin * y_scale # bar = bar[:y_0+y]+'c'+bar[y_0+y+1:] bar = bar[:y_0+0]+'|'+bar[y_0+0+1:] print bar # # Plot (2) - Cosine # hbar = '-' * y_0 + '+' + '-' * y_0 # print '-' * y_0, '+', '-' * y_0 print hbar for x in range(0,x_ticks,x_step): bar = ' ' * 2 * y_0 # sin = m.sin(deltaAngle * x) # y = int(sin * y_scale) # # y = sin * y_scale # bar = bar[:y_0+y]+'s'+bar[y_0+y+1:] cos = m.cos(deltaAngle * x) y = int(cos * y_scale) # # y = sin * y_scale bar = bar[:y_0+y]+'c'+bar[y_0+y+1:] bar = bar[:y_0+0]+'|'+bar[y_0+0+1:] print bar # # Plot (3) - Sin + Cos # hbar = '-' * y_0 + '+' + '-' * y_0 # print '-' * y_0, '+', '-' * y_0 print hbar for x in range(0,x_ticks,x_step): bar = ' ' * 2 * y_0 sin = m.sin(deltaAngle * x) y = int(sin * y_scale) # # y = sin * y_scale bar = bar[:y_0+y]+'s'+bar[y_0+y+1:] cos = m.cos(deltaAngle * x) y = int(cos * y_scale) # # y = sin * y_scale bar = bar[:y_0+y]+'c'+bar[y_0+y+1:] bar = bar[:y_0+0]+'|'+bar[y_0+0+1:] print bar # # Plot (4) - Sine # print '-' * y_0, '+', '-' * y_0 for x in range(0,x_ticks,x_step): sin = m.sin(deltaAngle * x) y = int(sin * y_scale) # # y = sin * y_scale #+ print ' ' * y_0, '*' * (y+y_0) #+ print ' ' * (y+y_0), '$' #+ print "sin(",x,"/",x_resolution," pi) =", sin, y if ( y > 0 ): print ' ' * y_0, '|', ' ' * (y-1), '@' elif y == 0: print ' ' * y_0, ':' else: print ' ' * (y_0+y-2), '@', ' ' * (-y-1), '|' print # # Plot (5) - Sine # print '-' * y_0, '+', '-' * y_0 for x in range(0,x_ticks,x_step): sin = m.sin(deltaAngle * x) y = int(sin * y_scale) # # y = sin * y_scale if ( y > 0 ): print ' ' * y_0, '|', '@' * y else: print ' ' * (y_0+y-1), '@' * (-y), '|' # y_axis = " " # y_axis[y] = '*' # CANNOT change string member # print y_axis