# -*- coding: utf-8 -*- """math.sin.2D.v2.3.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/12tdDRtHdW_zRudl6hjX2uIAunriDdGjw # Ploting Functional Curves ## Display a pattern using character grids """ # # A display device normally consists of a matrix of grid points # or lines of grid points. # A figure or picture is displayed by turning some points ON and other points OFF. # # Example: # A 8x8 dot matrix that represent the 'A' character, # 'a': ON, '.' (or blank): OFF # dotMatrix = """ ...aa... ..a..a.. .a....a. a......a aaaaaaaa a......a a......a ........ """ print(dotMatrix) print(dotMatrix.replace('.',' ')) print(dotMatrix.replace('.',' ').replace('a','*')) """## Representing a display device using strings""" # To display a 2D pattern or curve, that is, a list of (x,y) coordinate pairs, # we need to be able to turn points at these (x,y)'s ON, given an initial blank screen. # Each grid line can be represented by a string variable, which is initially blank. # We then replace some blank points with special marks to turn them ON. """## Turning a grid point ON""" # # Mark a special position (y) with a special symbol # # # How to replace a character at a particular position in a string with a special symbol?? # # Eample: # How to replace the first 'o' in the HelloWorld string with 'O' s = 'Hello World' print s # # How to replace the character s[4] (small 'o') with 'O' (big O)?? # # s[4] = 'O' # ERROR: TypeError: 'str' object does not support item assignment # Unlike change of list elements, we CANNOT directly assign a new value to it. # # Instead, we can compose a string consisting of: # 1. the substring BEFORE the character to be replaced # 2. the new replacement character or symbol # 3. the substring AFTER the character to be replaced # We can concatenate these 3 pieces and save it to the original string variable. # # s = s[:4] + 'x' + s[5:] # replace the 3rd char with 'x' print(s[:4],s[4],s[5:]) # s[:4]: substring before s[4], s[5:]: substring after s[4] print(s[:4],'O',s[5:]) # replace s[4] with 'O' print(s[:4]+'O'+s[5:]) # concate them s = s[:4]+'*'+s[5:] # replace the 4-th character with '*' print(s) # # How to replace a character at any position 'y' # print("." * 10) s = ' ' # s = s[:4]+'*'+s[5:] # replace s[4] y = 8 s = s[:y]+'*'+s[y+1:] # replace s[y] print(s) # So, if s represent a vertical grid line (for some particular x) # we can use the above statement to 'turn the point (x,y) ON' ... """## Example: ploting a functional curve""" # # Basics: Plot (x,y) pairs using a string variable # import random def myfunc(x): # y = f(x), y calculated from x # y = int(random.random()*10) # f-1: random numbers y = x + 1 # f-2: linear # y = x ** 2 # f-3: quadratic # y = x ** 2 - 20 # f-4: quadratic (with negative values) return y # # logical to device coordinates conversion parameters # yScale = 1 # magnification in y-direction # yScale = 0.1 # f3/f4: magnification in y-direction y0 = 0 # translation/shift in y-direction # y0 = 2 # f4 print("(%2s, %2s) %s" % ('x', 'y', "-" * 40)) random.seed(10) for x in range(20): # iterate through some x's s = "." * 40 # a scan line with no data mark (OFF) y = myfunc(x) # y calculated from x (use random number here) # logical coordinates to device coordinates conversion y *= yScale # magnify to fit device width/height y += y0 # shift y=0 line (to change negative y to fit into positive device index ranges) y = int(y) # round to integer index # We may also need to reverse y's polarity (y = -y) # before scaling # if the device coordinates have opposite y-direction # as logical coordinates. # place a marker at position y for the scanline for x s = s[:y]+'*'+s[y+1:] # mark data point (x,y) at the y-th position of the scan line print("(%2d, %2d) %s" % (x, y, s)) """# Ploting Sine/Cosine Waves ## Plotting Parameters """ # # 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.3 # Last Update: 2019/12/25 # 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 # number of grid points above/below the central axis line y_scale = 10 # magnification in the y-direction x_resolution = 10 # increment pi/10 per step deltaAngle = m.pi/x_resolution x_ticks = 41 # run 41 steps (== 4 * pi) x_step = 1 """## Sine (with one string as vertical scan line)""" # # Plot (1) - Sine # hbar = '-' * y_0 + '+' + '-' * y_0 print hbar n_rows = len(hbar) for x in range(0,x_ticks,x_step): bar = "'" * n_rows # bar = ' ' * n_rows # mark '|' at origin (y=0) bar = bar[:y_0+0]+'|'+bar[y_0+1:] # mark 's' at y=sin(x) [with magnification & translation to fit device coordinates] sin = m.sin(deltaAngle * x) y = int(sin * y_scale) # scale up in y-direction y += y_0 # translate/shift up in y-direction (to make positive character index) bar = bar[:y]+'s'+bar[y+1:] # mark 'c' at y=cos(x) [with magnification & translation to fit device coordinates] # cos = m.cos(deltaAngle * x) # y = int(cos * y_scale) # scale up in y-direction # y += y_0 # translate/shift up in y-direction (to make positive character index) # bar = bar[:y]+'c'+bar[y+1:] print bar """### Cosine""" # # Plot (2) - Cosine # hbar = '-' * y_0 + '+' + '-' * y_0 print hbar n_rows = len(hbar) for x in range(0,x_ticks,x_step): # bar = "'" * n_rows bar = ' ' * n_rows # mark '|' at origin (y=0) bar = bar[:y_0+0]+'|'+bar[y_0+1:] # mark 's' at y=sin(x) [with magnification & translation to fit device coordinates] # sin = m.sin(deltaAngle * x) # y = int(sin * y_scale) # scale up in y-direction # y += y_0 # translate/shift up in y-direction (to make positive character index) # bar = bar[:y]+'s'+bar[y+1:] # mark 'c' at y=cos(x) [with magnification & translation to fit device coordinates] cos = m.cos(deltaAngle * x) y = int(cos * y_scale) # scale up in y-direction y += y_0 # translate/shift up in y-direction (to make positive character index) bar = bar[:y]+'c'+bar[y+1:] print bar """### Sine + Cosine""" # # Plot (3) - Sin + Cos # hbar = '-' * y_0 + '+' + '-' * y_0 print hbar n_rows = len(hbar) for x in range(0,x_ticks,x_step): # bar = "'" * n_rows bar = ' ' * n_rows # mark '|' at origin (y=0) bar = bar[:y_0+0]+'|'+bar[y_0+1:] # mark 's' at y=sin(x) [with magnification & translation to fit device coordinates] sin = m.sin(deltaAngle * x) y = int(sin * y_scale) # scale up in y-direction y += y_0 # translate/shift up in y-direction (to make positive character index) bar = bar[:y]+'s'+bar[y+1:] # mark 'c' at y=cos(x) [with magnification & translation to fit device coordinates] cos = m.cos(deltaAngle * x) y = int(cos * y_scale) # scale up in y-direction y += y_0 # translate/shift up in y-direction (to make positive character index) bar = bar[:y]+'c'+bar[y+1:] print bar """## Sine (without using auxilirary strings)""" # # 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 Rows of Horizontal Scan Lines We can also use a list of strings to represent different (horizontal) scan lines, use the list index as y-coordinate (row address) and character position as x-cordinate (column address) to place special markers to point (x,y). ## Hint """ #''' # Hints: rows = [ # initial OFF state scan lines '.' * 10, '=' * 10, '-' * 10 ] # place markers at different (device) coordinates rows[0]=rows[0][:3]+'a'+rows[0][4:] # x=3, y=0 rows[1]=rows[1][:4]+'b'+rows[1][5:] # x=4, y=1 rows[2]=rows[2][:5]+'c'+rows[2][6:] # x=5, y=2 print ('y=2: %s\ny=1: %s\ny=0: %s\n' % (rows[2], rows[1], rows[0])) #''' """## Sine (Using 2D Scan Lines)""" # # Using 2D scan lines -- Example # # # Plot (6) - Sine + Cosine # rows = [] n_rows = y_0*2+1 n_cols = 80 # # create blank screen (y_0*2+1 rows & n_cols columns) # for y in range(n_rows): rows.append(' ' * n_cols) row_id = len(rows)-1 #+ print ('%2d: %s' % (row_id, rows[row_id])) # # create centeral line # rows[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 rows[y]=rows[y][:x]+'s'+rows[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 rows[y]=rows[y][:x]+'c'+rows[y][x+1:] # add mark at y-th row & x-th col # # print the curves # #+ n_rows = len(rows) print 'n_rows =', n_rows for y in range(n_rows-1,-1,-1): # print ('%3d: %s' % (y, rows[y])) # without offset print ('%3d: %s' % (y-y_0, rows[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, rows[y])) # without offset print ('%3d: %s' % (y-y_0, rows[y])) """## Challenge Implement a string function to place a specified marker character at designated position, like: - placeMarker(string, marker, potision) - re-write the above drawing functions using placeMarker() ## TODO """ # # Using Array module (TODO) # x = ''' 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]) '''