欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python3.6 绕圈圈打印矩形动画效果

程序员文章站 2022-04-27 13:18:01
...
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

class Stack:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[-1]

    def size(self):
        return len(self.items)

def main():
    ax.spines['right'].set_color('none') #隐藏上,右坐标轴
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')#移动刻度线
    ax.spines['bottom'].set_position(('data', 0))#移动下,左坐标轴
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data', 0))

    # 画外圆
    theta = np.linspace(0, 2 * np.pi, 1000)
    x1, y1 = np.cos(theta) * 150, np.sin(theta) * 150
    plt.plot(x1, y1, color='yellow', linewidth=2.0)
    #画內圆
    x2,y2 = np.cos(theta)*30, np.sin(theta)*30
    plt.plot(x2, y2, color='red', linewidth=2.0)


def output(x,y):
    # 打印坐标(索引)信息
    SIZE = x.shape[0]  # 适用于其他值
    orient = 3  # 控制方向 0:向上,1:向下,2:向左,3:向右
    H, L = 0, 0  # H 控制行号,L 控制列号

    for i in range(1, SIZE * SIZE + 1):
        if((x[H][L]**2 + y[H][L]**2 >= 900) & (x[H][L]**2 + y[H][L]**2 <= 22500)):
            s.push(y[H][L])
            s.push(x[H][L])
        if H + L == SIZE - 1:
            if L > H:
                orient = 1
            else:
                orient = 0
        elif H == L and H >= SIZE / 2:
            orient = 2
        elif L + 1 == H and H < SIZE / 2:
            orient = 3

        if orient == 0:
            H -= 1
        elif orient == 1:
            H += 1
        elif orient == 2:
            L -= 1
        elif orient == 3:
            L += 1



def init():
    ax.set_xlim(-180, 180)
    ax.set_ylim(-180, 180)
    return ln,

def update(frame):

    xdata.append(s.pop())
    ydata.append(s.pop())

    ln.set_data(xdata, ydata)

    # text_pt.set_text("x=%.3f,\n y=%.3f" % (np.cos(frame) * 150, np.sin(frame) * 150))  # num 代表第几个索引,一定是整数。
    # text_pt.set_position((np.cos(frame) * 150, np.sin(frame) * 150))  # 设置文本位置。
    # return ln,text_pt,
    return ln,



if __name__ == '__main__':
    fig, ax = plt.subplots()
    xdata, ydata = [], []
    s = Stack()

    ln, = plt.plot([], [], 'r.-')

    main()
    x, y = np.mgrid[-150: 150: 40j, -150: 150:40j]  # 返回多维结构
    output(x,y)
    ani = FuncAnimation(fig, update, frames=np.arange(0, len(s.items)/2),
                        init_func=init, blit=True, repeat=False,interval=10)
    # ani.save('circle.gif', writer='imagemagick', fps=10)
    plt.show()

python3.6 绕圈圈打印矩形动画效果

相关标签: python animation