draw_shape_repeatedly.py

#!/usr/bin/python3
# ===================================================================
# draw a repeated shape (try graphics.py draw polygon method?)
# ===================================================================

import coordinate_conversion as cc
import transformation_matrix as tm
import user_interface as ui
import draw_xy_axes as ax
from graphics import *


winw = 801                     # window width
winh = 801                     # window height

line_color = ['black','red','green','blue','yellow','cyan']

testpts = [[0,0],[200,10],[220,0],[200,-10],[0,0]] 


#--------------------------------------------------------------------
# ---- draw shape
# ---- win   - window object
# ---- pts   - list of end points to draw conecting lines
# ---- angle - shape rotation angle
# ---- color - shape line color
# -------------------------------------------------------------------

def draw_shape(win,pts,angle=10,color=0):

    # --- transformation matrix (rotation around orgin)

    mx = tm.get_z_rotation_matrix_2d(angle)

    # ---- connect the points with lines

    for i in range(len(pts)-1):

        # ---- rotate (transform) line points (start and end)

        cxy0 = mx @ [pts[i][0],pts[i][1],1]
        cxy1 = mx @ [pts[i+1][0],pts[i+1][1],1]

        # ---- convert center (caresian) coordinates to
        # ---- window coordinates

        wxy0 = cc.center_to_win_coords(cxy0[0],cxy0[1],winw,winh)
        wxy1 = cc.center_to_win_coords(cxy1[0],cxy1[1],winw,winh)

        # ---- draw a line

        l = Line(Point(wxy0[0],wxy0[1]),Point(wxy1[0],wxy1[1]))
        l.setWidth(2)
        l.setOutline(line_color[color])
        l.draw(win)


# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

# ---- create window

win = GraphWin("Repeat Drawing Shape",winw,winh)
win.setBackground("white")

# ---- draw X,Y coordinate axes

##ax.draw_xy_axes(win,True)

# ---- rotate shape then draw

color  = 0                     # color list index
angle  = 0                     # starting angle
delta  = 15                    # angle delta
lstlen = len(testpts)          # point list length

while angle <= 180:

    # ---- draw shape using a rotation angle (deg)

    draw_shape(win,testpts,angle,color)

    angle += delta             # next angle

    if color < lstlen:         # next color
        color += 1
    else:
        color = 0

# ---- end program

ui.pause()

win.close()
print()