Code Example: test_fold_01.py

#! /usr/bin/python3
# ===================================================================
# demonstrate drawing/erasing a graphics object made
# of line segments and points
# ===================================================================

from numpy import *

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

# ---- object (line segments)

segs = [ ( 200.0, 200.0, 200.0,-200.0),
         ( 200.0,-200.0,-200.0,-200.0),
         (-200.0,-200.0,-200.0,  50.0),
         (-200.0,  50.0,-200.0, 200.0),
         (-200.0, 200.0,-100.0, 200.0),
         (-100.0, 200.0, 200.0, 200.0), ]

# ---- folding line (line segment)

fln = (-200.0,50.0,-100.0,200.0)

# -------------------------------------------------------------------
# ---- draw line segment
# -------------------------------------------------------------------

def draw_line_segment(win,ul,x1,y1,x2,y2,ends=True,
                      color1='black',color2='black'):
    wx1,wy1 = cc.center_to_win_coords(x1,y1,win.width,win.height)
    wx2,wy2 = cc.center_to_win_coords(x2,y2,win.width,win.height)
    l = Line(Point(wx1,wy1),Point(wx2,wy2))
    l.setWidth(2)
    l.setFill(color1)
    l.draw(win)
    ul.append(l)
    if ends:
        draw_point(win,ul,x1,y1,color2)
        draw_point(win,ul,x2,y2,color2)

# -------------------------------------------------------------------
# ---- draw point (small circle)
# -------------------------------------------------------------------

def draw_point(win,ul,x,y,color='red'):
    wx,wy = cc.center_to_win_coords(x,y,win.width,win.height)
    p = Circle(Point(wx,wy),4)
    p.setFill(color)
    p.draw(win)
    ul.append(p)

# -------------------------------------------------------------------
# ---- draw object (made of line segments)
# ---- segs  object line segments
# ---- fln   folding line line segment
# ---- cnt   draw folding line center point
# -------------------------------------------------------------------

def draw_object(win,segs,fln,cnt=True):

    # ---- undraw list

    ul = []

    # ---- draw object

    for s in segs:
        draw_line_segment(win,ul,s[0],s[1],s[2],s[3])

    # ---- draw folding line

    if fln is not None:

        draw_line_segment(win,ul,fln[0],fln[1],fln[2],fln[3],
                          True,'red','white')

        # ---- draw folding line center point

        if cnt:
            x = (fln[0]+fln[2])/2
            y = (fln[1]+fln[3])/2
            draw_point(win,ul,x,y,'white')

    return ul

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

if __name__ == '__main__':

    win_width  = 801
    win_height = 801

    # ---- create window

    win = GraphWin("Test Fold",win_width,win_height)
    win.setBackground("white")

    # ---- draw axes

    ax.draw_xy_axes(win,True)

    # ---- draw the object, etc.

    ul = draw_object(win,segs,fln)

    # ---- undraw the object, etc.

    print()
    print('undraw the object, etc.')
    ui.pause()

    for o in ul:
        o.undraw()

    # ---- end program

    print()
    print('exit the program')
    ui.pause()

    win.close()
    print()