# # 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 # # Using 2D scan lines -- Example # ''' matrix = [ '0' * 10, '1' * 10, '2' * 10 ] matrix[0]=matrix[0][:3]+'x'+matrix[0][4:] matrix[1]=matrix[1][:4]+'y'+matrix[1][5:] matrix[2]=matrix[2][:5]+'z'+matrix[2][6:] print ('%s\n%s\n%s\n' % (matrix[0], matrix[1], matrix[2])) ''' matrix = [] n_cols = 80 # # create blank screen # for y in range(y_0*2+1): matrix.append(' ' * n_cols) row_id = len(matrix)-1 #+ print ('%2d: %s' % (row_id, matrix[row_id-1])) # # create centeral line # matrix[y_0]= '-' * n_cols # # create function curves # for x in range(0,x_ticks,x_step): sin = m.sin(deltaAngle * x) y = int(sin * y_scale) # # y = sin * y_scale y += y_0 # shift up matrix[y]=matrix[y][:x]+'S'+matrix[y][x+1:] cos = m.cos(deltaAngle * x) y = int(cos * y_scale) # # y = sin * y_scale y += y_0 matrix[y]=matrix[y][:x]+'c'+matrix[y][x+1:] n_rows = len(matrix) print 'n_rows =', n_rows for y in range(n_rows-1,-1,-1): print ('%2d: %s' % (y, matrix[y])) #- print ('%2d: %s' % (y, 'xxx')) print 'n_rows =', n_rows, '(reversed)' for y in range(n_rows): print ('%2d: %s' % (y, matrix[y])) # # Using Array module # ''' import array as arr a = arr.array('d', [1.1, 3.5, 4.5]) print(a) print(a[0]) print(a[1]) print(a[2]) '''