python-3.x - 使用 manim 制作带有轻微移动节点的类似网络的图
问题描述
试图通过使用valuetrackers重新创建它
这是我的代码,错误也在那个pastebin里面谢谢你的帮助,因为我还不能创建一个动画,我想通过将点和线都链接到一些valuetrackers列表然后最后存储更新来制作动画列表中的这些价值跟踪器,最后播放它们是我的目标
class Network3(Scene):
def construct(self):
screen_grid = ScreenGrid()
self.add(screen_grid)
#function to just random the x and y coordinates
def randomize_xy():
for i in range(0,no_of_dots):
x_coord[i]=random.randint(-7,+7)
y_coord[i]=random.randint(-4,+4)
print(x_coord)
print(y_coord)
no_of_dots=20
#make some dots
dots=[]
for i in range(0,no_of_dots):
dots.append(Dot())
#make initial list of coordinates
x_coord=[]
y_coord=[]
for i in range(0,no_of_dots):
x_coord.append(random.randint(-7,7))
y_coord.append(random.randint(-4,4))
# #make sure all the lengths are ok
# print(f"length of dots lenght={len(dots)} of x_coord={len(x_coord)}") error of int base 10 is coming from here
print("length of dots=",end="")
print(str(len(dots))) #print(len(dots)) still causes an error !!!
#add all dots at one point
self.add(*dots)
#make anim list to move everything to their position from origin
animlist=[]
for i in range(0,no_of_dots):
animlist.extend([dots[i].move_to,[x_coord[i],y_coord[i],0]])
#play
self.play(*animlist)
#make a list of value trackers
x_trackers=[]
y_trackers=[]
for i in range(0,no_of_dots):
x_tracker=ValueTracker(x_coord[i])
y_tracker=ValueTracker(y_coord[i])
x_trackers.append(x_tracker)
y_trackers.append(y_tracker)
#making lines
lines=[]
for i in range(0,no_of_dots-1):
t1=[x_coord[i],y_coord[i],0]
t2=[x_coord[i+1],y_coord[i+1],0]
line=Line(t1,t2)
lines.append(line)
def update_func1(obj,i):
temp1=np.array([x_trackers[i],y_trackers[i],0])
temp2=np.array([x_trackers[i+1],y_trackers[i+1],0])
line=Line(start=temp1,end=temp2)
obj.become(line)
#making a link between each line and value tracker
for i in range(0,no_of_dots-1):
lines[i].add_updater(lambda m:update_func1(m,i))
#adding again
self.add(*lines)
#making a link between each dot and value tracker
for i in range(0,no_of_dots):
dots[i].add_updater(lambda m:m.move_to([x_trackers[i],y_trackers[i],0]))
#now somehow have to change all the value tracker at once
animlist2=[]
for i in range(0,no_of_dots):
animlist2.extend([x_trackers[i].set_value,random.randint(-7,+7)])
animlist2.extend([y_trackers[i].set_value,random.randint(-4,+4)])
#now play
self.play(*animlist2)
#end
self.wait(3)
我得到的当前错误是:
Traceback (most recent call last):
File "C:\manim\manimlib\extract_scene.py", line 155, in main
scene = SceneClass(**scene_kwargs)
File "C:\manim\manimlib\scene\scene.py", line 53, in __init__
self.construct()
File "network_graphic.py", line 584, in construct
lines[i].add_updater(lambda m:update_func1(m,i))
File "C:\manim\manimlib\mobject\mobject.py", line 192, in add_updater
self.update(0)
File "C:\manim\manimlib\mobject\mobject.py", line 159, in update
updater(self)
File "network_graphic.py", line 584, in <lambda>
lines[i].add_updater(lambda m:update_func1(m,i))
File "network_graphic.py", line 578, in update_func1
line=Line(start=temp1,end=temp2)
File "C:\manim\manimlib\mobject\geometry.py", line 431, in __init__
self.set_start_and_end_attrs(start, end)
File "C:\manim\manimlib\mobject\geometry.py", line 471, in set_start_and_end_attrs
vect = normalize(rough_end - rough_start)
TypeError: unsupported operand type(s) for -: 'ValueTracker' and 'ValueTracker'
解决方案
我认为您最好的选择是使用带有“dt”的功能,如果您有任何疑问,请观看我的视频。这是格兰特在他的一个非常古老的视频中制作的基本示例,但我认为您可以将其用于您的想法。
class RandomMove(Scene):
CONFIG = {
"amplitude": 0.4,
"jiggles_per_second": 1,
}
def construct(self):
points = VGroup(*[
Dot(radius=0.2) for _ in range(9)
])
points.arrange_in_grid(buff=1)
for submob in points:
submob.jiggling_direction = rotate_vector(
RIGHT, np.random.random() * TAU *1.5,
)
submob.jiggling_phase = np.random.random() * TAU *1.5
def update_mob(mob, dt):
for submob in mob:
submob.jiggling_phase += dt * self.jiggles_per_second * TAU
submob.shift(
self.amplitude *
submob.jiggling_direction *
np.sin(submob.jiggling_phase) * dt
)
points.add_updater(update_mob)
self.add(points)
self.wait(10)
结果在这里
推荐阅读
- auth0 - 有没有办法通过 state 参数传递 redirect_uri 并在回调端点中访问所述参数?
- python - `signal.signal` 和 asyncio 的 `EventLoop.add_signal_handler` 之间的区别
- javascript - 在 Workbox 中重新验证策略时使用 Stale 进行预缓存
- matlab - 求解 6 个 ode 的系统 - 可变参数 -CLOSED
- javascript - jquery加载方法不加载站点脚本和css为什么?
- regex - 为什么我的正则表达式在在线正则表达式测试仪上运行时无法选择正确的元素
- laravel - 如何检查身份验证登录 3 表 laravel?
- r - 使用以 R 中的第一个条件为条件的条件进行过滤
- uwp - 从 Dynamics 门户分发 Windows 10 应用程序
- c# - 如何在 .NET 核心上生成 RSA 密钥对?