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

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()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值