#!/usr/bin/python3 # ==================================================================== # Plot sine function # Note: cartesian coordinates origin (0,0) is the center of the # graphics window # ==================================================================== import numpy as np import graphics as gr import draw_xy_axes as ax # -------------------------------------------------------------------- # ---- sine func # -------------------------------------------------------------------- def sine_func(deg): rad = np.deg2rad(deg) y = np.sin(rad) return y # -------------------------------------------------------------------- # ---- range function for floats # -------------------------------------------------------------------- def frange(start,end,inc): while start < end: yield start start += inc # -------------------------------------------------------------------- # ---- draw a point (small circle) using window coordinates # -------------------------------------------------------------------- def draw_point(x,y,color='red',size=4): p = gr.Circle(gr.Point(x,y),size) p.setFill(color) p.setOutline('black') p.draw(win) return p # -------------------------------------------------------------------- # ---- main # -------------------------------------------------------------------- if __name__ == '__main__': # ---- graphics window size window_width = 801 window_height = 801 # ---- setup graphics window win = gr.GraphWin('Sine Function',window_width,window_height) win.setBackground("white") # ---- draw X,Y axes ax.draw_xy_axes(win) # ---- plot sine function # ---- X scale factor 1 offset 400 # ---- Y scale factor 100 offset 400 xmin = 0 xmax = 0 ymin = 0 ymax = 0 for deg in range(-720,721,36): y = sine_func(deg) x = 400 + (deg * 1) y = 400 + (y * 100) draw_point(x,y) if x < xmin: xmin = x if x > xmax: xmax = x if y < ymin: ymin = y if y > ymax: ymax = y print(f'Xmin = {xmin} Xmax = {xmax}') print(f'Ymin = {ymin} Ymax = {ymax}') # ---- pause program (click in window to continue) click = win.getMouse() win.close()