#! /usr/bin/python3 # =================================================================== # demo combining multiple transformation matrices # into a single matrix # =================================================================== import transformation_matrix as tm import coordinate_conversion as cc import user_interface as ui from graphics import * import numpy as np import os, sys win_width = 801 win_height = 801 # ---- blob (polygon) window coordinates # ---- blob center is 100,100 blob_w_coords = [ [110,70], [140,70], [110,110], [110,140], [80,110], [50,110], [70,100], [50,90], [90,90], [110,70] ] # ---- blob (polygon) center (Cartesian) coordinates # ---- blob is center around the origin (0.0,0.0) blob_c_coords = [ [10,-30], [40,-30], [10,10], [10,40], [-20,10], [-50,10], [-30,0], [-50,-10], [-10,-10], [10,-30] ] # ------------------------------------------------------------------- # ---- function: draw X,Y axes # ------------------------------------------------------------------- def draw_xy_axes(win,line_width=1,line_color="black"): xl = Line(Point(0,win.height/2),Point(win.width-1,win.height/2)) xl.setWidth(line_width) xl.setFill(line_color) xl.draw(win) yl = Line(Point(win.width/2,0),Point(win.width/2,win.height-1)) yl.setWidth(line_width) yl.setFill(line_color) yl.draw(win) # ------------------------------------------------------------------- # ---- function: create a polygon points list in window coordinates # ------------------------------------------------------------------- def create_points_list(tm,coords,winwidth,winheight): points = [] for c in coords: xy1 = [c[0],c[1],1] p = tm @ xy1 xy = cc.center_to_win_coords(p[0],p[1],winwidth,winheight) points.append(Point(xy[0],xy[1])) return points # ------------------------------------------------------------------- # ---- main # ------------------------------------------------------------------- # ---- do nothing transformation tmi = tm.get_identity_matrix_2d() xx_points = create_points_list(tmi,blob_c_coords,win_width,win_height) # ---- combine transformations into one # ---- rotate/translate transformation # ---- rotate blob 45 deg then translate 100,100 tm1 = tm.get_translation_matrix_2d(100.0,100.0) tm2 = tm.get_z_rotation_matrix_2d(45.0) tm3 = tm1 @ tm2 # order of operations is important # hint: read operations right to left rt_points = create_points_list(tm3,blob_c_coords,win_width,win_height) # ---- create window win = GraphWin("Rotate/Translate Demo", win_width, win_height) win.setBackground("white") # ---- draw center X,Y axes (Cartesian coordinate axes) draw_xy_axes(win) # ---- draw blob(s) # ---- first draw "do nothing" transformation # ---- second draw rotate/translate transformation blob = Polygon(xx_points) blob.setWidth(3) blob.setOutline("black") blob.setFill("red") blob.draw(win) blob = Polygon(rt_points) blob.setWidth(3) blob.setOutline("black") blob.setFill("red") blob.draw(win) # ---- pause and end program print() ui.pause()