Animation Example #2

#! /usr/bin/python3
# ===================================================================
# animation example #2
# ===================================================================

import user_interface as ui
import coordinate_conversion as cc
import draw_xy_axes as ax
import numpy as np
from graphics import *
import time


# ---- window dimensions

win_width  = 801
win_height = 801


# ------------------------------------------------------------------
# ---- display location data
# ------------------------------------------------------------------

def display_location(cx,cy,wx,wy,wwx,wwy,dx,dy):

    print('--------------------------------------------------')
    print(f'cx  = {cx}')
    print(f'cy  = {cx}')
    print(f'wx  = {wx}')
    print(f'wx  = {wx}')
    print(f'wwx = {wwx}')
    print(f'wwy = {wwx}')
    print(f'dx  = {dx}')
    print(f'dx  = {dx}')


# ------------------------------------------------------------------
# ---- animation
# ---- move a circle around the origin (center coordinates)
# ----
# ---- win  window object
# ---- dst  starting distance from origin (center coordinates)
# ---- deg  starting angle for animation loop
# ---- inc  increment in degrees for each step of the loop
# ---- cnt  loop step count
# ---- sec  pause seconds between drawing frames
# -------------------------------------------------------------------

def animation(win, dst, deg, inc=5.0 ,cnt=100, sec=0.2):

    if not sec > 0.0:
        print(f'Bad seconds value ({sec})')
        return

    rad = np.deg2rad(deg % 360.0)

    cx = np.sin(rad) * dst
    cy = np.cos(rad) * dst

    (wx,wy) = cc.center_to_win_coords(cx,cy,win.width,win.height)

    c = Circle(Point(wx,wy),5)
    c.setOutline('black')
    c.setWidth(2)
    c.setFill('red')
    c.draw(win)

    for _ in range(int(cnt)):

        time.sleep(sec)

        deg += inc

        rad = np.deg2rad(deg % 360.0)

        cx = np.sin(rad) * dst
        cy = np.cos(rad) * dst

        (wwx,wwy) = cc.center_to_win_coords(cx,cy,win.width,win.height)

        dx = wwx - wx
        dy = wwy - wy

        c.move(dx,dy)

        ##display_location(cx,cy,wx,wy,wwx,wwy,dx,dy)

        wx = wwx
        wy = wwy


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

# ---- create window

win = GraphWin("Animation 02", win_width, win_height)
win.setBackground("white")

# ---- draw X,Y coordinate axes

ax.draw_xy_axes(win,True)

# ---- animation

animation(win,150.0,0.0)

# ---- end program

ui.pause()

win.close()
print()