首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试使用分支为生长和收缩的细丝设置动画

尝试使用分支为生长和收缩的细丝设置动画
EN

Stack Overflow用户
提问于 2019-04-30 07:50:05
回答 1查看 84关注 0票数 0

基本上,母丝从空间中的初始点开始增长和收缩。当它达到阈值长度时(在我提供的代码中,它对应于坐标= (X = 8,X =8))。此时,分支开始从(8,8)开始增长。分支的增长和收缩与父分支类似,不同之处在于它的起点是(8,8),而父分支的起点是(1,1)。(虽然我从(1,1)开始分支,但这只是为了让动画的列表长度相等)。

更大的问题是,我的代码中有几个这样的父细丝以不同的角度生长,当它们中的每一个都超过阈值长度时,分支以随机的角度出现。此外,如果父对象收缩回小于阈值的长度,则分支将消失(或者以一种简单的方式,分支的坐标与父对象的坐标相同。)

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

X = [1,2,3,4,5,6,7,8,9,10,11,12,11,10,11,12,13,14,15,14,13,12,13] #parent x coord
Y = [1,2,3,4,5,6,7,8,9,10,11,12,11,10,11,12,13,14,15,14,13,12,13] #parent y coord

X1 = [1,2,3,4,5,6,7,8,7,6,5,4,4,4,5,6,7,8,7,6,5,4,3] #branch x coord
Y1 = [1,2,3,4,5,6,7,8,9,10,11,12,12,12,11,10,9,8,9,10,11,12,13] #branch y coord

fig, ax = plt.subplots(1,1)
ax.set_xlim([0, 20])
ax.set_ylim([-1.1, 20])

graph1, = ax.plot([], [], color = 'green')
graph2, = ax.plot([], [], color = 'green')
dot1, = ax.plot([], [], 'o', color='red', markersize = 5)

dot2, = ax.plot([], [], 'o', color='green', markersize = 5)
def oj(i):

    graph1.set_data([X[0],X[i]],[Y[0],Y[i]]) ## for the parent this works as I want it grow and shrink without any trace

    graph2.set_data(X1[:i+1],Y1[:i+1]) # for the branch I can't use the same code as that for graph1 as I want it to be attached at (8,8) and grow from or shrink to that poin
    dot1.set_data(X[i],Y[i]) # this shows how the tip of the parent filament animates

    dot2.set_data(X1[i],Y1[i]) #this shows the tip of the branch which I face a difficulty in animating as it traces back instead of showing a shrink 
anim = animation.FuncAnimation(fig, oj, frames=len(X), interval=1000, repeat = False)
plt.show()
EN

回答 1

Stack Overflow用户

发布于 2019-04-30 20:44:02

实际上,我将保留您对主分支使用的相同策略,但引入一个参数来指示分支应该在哪个步骤发生并从那里开始工作:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

X = [1,2,3,4,5,6,7,8,9,10,11,12,11,10,11,12,13,14,15,14,13,12,13] #parent x coord
Y = [1,2,3,4,5,6,7,8,9,10,11,12,11,10,11,12,13,14,15,14,13,12,13] #parent y coord

X1 = [7,6,5,4,4,4,5,6,7,8,7,6,5,4,3] #branch x coord
Y1 = [9,10,11,12,12,12,11,10,9,8,9,10,11,12,13] #branch y coord

fig, ax = plt.subplots(1,1)
ax.set_xlim([0, 20])
ax.set_ylim([-1.1, 20])

graph1, = ax.plot([], [], color = 'green')
graph2, = ax.plot([], [], color = 'green')
dot1, = ax.plot([], [], 'o', color='red', markersize = 5)
dot2, = ax.plot([], [], 'o', color='green', markersize = 5)
ttl = ax.set_title('')

def oj(i, branch_point):
    ttl.set_text('i={:d}'.format(i))
    graph1.set_data([X[0],X[i]],[Y[0],Y[i]]) ## for the parent this works as I want it grow and shrink without any trace
    dot1.set_data(X[i],Y[i]) # this shows how the tip of the parent filament animates
    if i>branch_point:
        graph2.set_data([X[branch_point],X1[i-branch_point-1]],[Y[branch_point],Y1[i-branch_point-1]]) # for the branch I can't use the same code as that for graph1 as I want it to be attached at (8,8) and grow from or shrink to that poin
        dot2.set_data(X1[i-branch_point-1],Y1[i-branch_point-1]) #this shows the tip of the branch which I face a difficulty in animating as it traces back instead of showing a shrink 
anim = animation.FuncAnimation(fig, oj, frames=len(X), interval=1000, repeat=False, fargs=(7,))
plt.show()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55912164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档