# -*- coding: utf-8 -*- """math.sin.2D.v2.2.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1DNFcyaRIYqZSxr2ucL5YD5XEPNtlfS4J """ # # Polt Sine/Cosine Waves... # # # Author: Jing-Shin Chang # Emails: jshin@csie.ncnu.edu.tw, shin@nlp.csie.ncnu.edu.tw, shin.jsc@gmail.com # Revision: 2.2 # Last Update: 2019/12/20 # Language: Python 2 # # # Hint: # # 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 hbar for x in range(0,x_ticks,x_step): bar = "'" * 2 * y_0 # bar = ' ' * 2 * y_0 bar = bar[:y_0+0]+'|'+bar[y_0+0+1:] 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 hbar for x in range(0,x_ticks,x_step): bar = ' ' * 2 * y_0 bar = bar[:y_0+0]+'|'+bar[y_0+0+1:] # 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 hbar for x in range(0,x_ticks,x_step): bar = ' ' * 2 * y_0 bar = bar[:y_0+0]+'|'+bar[y_0+0+1:] 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 # ''' # Hints: 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])) ''' # # Plot (6) - Sine + Cosine # matrix = [] n_cols = 80 # # create blank screen (y_0*2+1 rows & n_cols columns) # 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*2-1,x_step): # double #ticks for higher resolution sin = m.sin(deltaAngle * x * 0.5) # half deltaAngle for higher resolution y = int(sin * y_scale *1.5) # # y = sin * y_scale y += y_0 # shift up matrix[y]=matrix[y][:x]+'s'+matrix[y][x+1:] # add mark at y-th row & x-th col cos = m.cos(deltaAngle * x * 0.5) y = int(cos * y_scale *1.5) # # y = sin * y_scale y += y_0 matrix[y]=matrix[y][:x]+'c'+matrix[y][x+1:] # add mark at y-th row & x-th col # # print the curves # n_rows = len(matrix) print 'n_rows =', n_rows for y in range(n_rows-1,-1,-1): # print ('%3d: %s' % (y, matrix[y])) # without offset print ('%3d: %s' % (y-y_0, matrix[y])) # # print the curves upside-down (smaller y first (at top)) # print 'n_rows =', n_rows, '(reversed)' for y in range(n_rows): # print ('%3d: %s' % (y, matrix[y])) # without offset print ('%3d: %s' % (y-y_0, matrix[y])) # # Using Array module (TODO) # ''' 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]) '''