Basic Graphics Window

#!/usr/bin/python3 # ============================================================== # basic graphics window # ============================================================== import graphics as gr import coordinate_conversion as cc import math import sys # -------------------------------------------------------------- # ---- create a graphics window # -------------------------------------------------------------- def create_a_graphics_window(width:int, height:int, axes:bool=False, title:str='Ellipse') -> gr.GraphWin: ''' create graphics window and maybe Cartesian axes ''' # ---- create window win = gr.GraphWin(title,width,height) win.setBackground("white") # ---- draw axes? if axes: # ---- X,Y axes origin in window coordinates xaxis = round(width/2.0) yaxis = round(height/2.0) # ---- draw X axis (line) xl = gr.Line(gr.Point(0,yaxis),gr.Point(width-1,yaxis)) xl.setWidth(1) xl.setFill("black") xl.draw(win) # ---- draw Y axis (line) yl = gr.Line(gr.Point(xaxis,0),gr.Point(xaxis,height-1)) yl.setWidth(1) yl.setFill("black") yl.draw(win) return win # -------------------------------------------------------------- # ---- create a list of points # -------------------------------------------------------------- def create_points(n:int=20) -> list: ''' create points to draw (Cartesian coordinates) Note: a point is a tuple (x,y) ''' pts = [] for x in range(n): x = x * 20 y = x pts.append((x,y)) return pts # -------------------------------------------------------------- # ---- draw points on an Ellipse # -------------------------------------------------------------- def draw_points(win:gr.GraphWin,pts:list) -> None: ''' draw points Note: a point is a tuple (x,y) ''' for pt in pts: x = pt[0] # Cartesian coord y = pt[1] # Cartesian coord # ---- convert point Cartesian coords to window coords wx1,wy1 = cc.center_to_win_coords(x,y, win.width,win.height) # ---- draw a point (circle) c = gr.Circle(gr.Point(wx1,wy1),4) c.setFill('red') c.draw(win) return # -------------------------------------------------------------- # ----- draw a line between two points # -------------------------------------------------------------- def draw_a_line(win:gr.GraphWin,pt1:tuple,pt2:tuple) -> None: ''' draw a line between two points Note: a point is a tuple (x,y) ''' # ---- convert point 1 Cartesian coords to window coords x = pt1[0] # Cartesian coord y = pt1[1] # Cartesian coord wx1,wy1 = cc.center_to_win_coords(x,y, win.width,win.height) # ---- convert point 2 Cartesian coords to window coords x = pt2[0] # Cartesian coord y = pt2[1] # Cartesian coord wx2,wy2 = cc.center_to_win_coords(x,y, win.width,win.height) # ---- draw a line l = gr.Line(gr.Point(wx1,wy1),gr.Point(wx2,wy2)) l.setWidth(3) l.setFill('black') l.draw(win) return # -------------------------------------------------------------- # ----- draw lines connecting points # -------------------------------------------------------------- def draw_lines(win:gr.GraphWin,pts:list) -> None: ''' draw liness Note: points are tuples (x,y) ''' i = 0 l = len(pts) while True: if i >= l-1: break # end of point list? draw_a_line(win,pts[i],pts[i+1]) i += 1 return # -------------------------------------------------------------- # ---- main # -------------------------------------------------------------- if __name__ == '__main__': # ---- create points pts = create_points() # ---- create graphics window win = create_a_graphics_window(801,801,axes=True, title="Drawing") # ---- draw points draw_points(win,pts) # ---- draw lines draw_lines(win,pts) # ---- wait for a mouse click in the window, then exit click = win.getMouse() win.close() sys.exit()