barnes hut

This commit is contained in:
2025-02-03 22:37:18 +01:00
commit 433c9cb988
3 changed files with 309 additions and 0 deletions

147
main.py Normal file
View File

@@ -0,0 +1,147 @@
from manim import *
class DefaultTemplate(ThreeDScene):
def construct(self):
shift_pos = (-3,0,0)
graph_pos = (4.5,3,0)
start_size=7
top = Square(color=BLUE, side_length=start_size)
top.shift(shift_pos)
def entity(point,color, runtime=1):
dot = Dot(color=color)
lable = Text(f"{point[:2]}",font_size=25,color=color)
group = VGroup(dot,lable)
group.shift(shift_pos)
group[1].shift(tuple(x+0.35 for x in point))
if runtime>0:
self.play(Create(group[0]), run_time=runtime)
self.play(group[0].animate.shift(point), run_time=runtime)
else:
group[0].shift(point)
self.add(group[0][0])
return group
self.play(Create(top))
#self.wait(1)
p1 = entity((1,2,1), BLUE_C)
self.play(Write(p1[1]))
#self.wait(1)
self.play(FadeOut(p1[1]))
t = Text("5kg", font_size=6)
t.move_to(p1[0])
p1_graph = VGroup(p1[0].copy(), t)
self.add(p1_graph)
p1.color = GRAY
#self.wait(10)
self.play(p1_graph.animate.scale(4).move_to(graph_pos))
p2 = entity((-2,1,1), BLUE_C,0.5)
self.play(Write(p2[1]))
#self.wait(1)
self.play(FadeOut(p2[1]))
l1 = Square(color=GREEN, side_length=start_size, grid_xstep=start_size/2, grid_ystep=start_size/2)
l1.set_z_index(-1)
l1.shift(shift_pos)
self.play(Create(l1))
numbs_grid = VGroup()
root_1 = p1_graph.copy()
root_1[0].color = GREEN
slots = VGroup()
for i in range(-2,2):
slot_text = VGroup()
slot = root_1[0].copy()
slot.color = GRAY_D
slot.shift((i+0.5,-1,0))
slot_text.add(slot)
slot_text.add(Text(str(i+3), color=GRAY_C,font_size=20).move_to(slot))
slots.add(slot_text)
root_1_1 = p1_graph.copy()
self.play(Transform(root_1_1,root_1), p1_graph.animate.shift((-1.5,0,0)))
root_1 = root_1_1
i = 1
square_numbers = VGroup()
for y in [1,-1]:
for x in [-1, 1]:
text = Text(text=str(i))
text.move_to(l1)
text.shift((x*start_size/4,y*start_size/4,0))
arrow = Arrow(start=root_1.get_center(), end=slots[i-1].get_center(), color=BLUE)
self.play(Write(text),Transform(root_1.copy(),VGroup(slots[i-1],arrow)))
square_numbers.add(text)
i+=1
p1_graph.set_z_index(2)
self.play(p1_graph.animate.move_to(slots[1]))
t = Text("1kg", font_size=6)
t.move_to(p2[0])
p2_graph = VGroup(p2[0].copy(), t)
self.add(p2_graph)
p2.color = GRAY
self.play(p2_graph.animate.scale(4).move_to(graph_pos).shift((-1.5,0,0)))
plus = Text("+")
plus.move_to(graph_pos).shift((-0.75,0,0))
t = Text("6kg", font_size=20)
t.move_to(root_1)
t.set_z_index(3)
self.play(Write(plus))
self.play(Transform(root_1[1], t))
self.play(p2_graph.animate.move_to(slots[0]), Unwrite(plus), Unwrite(square_numbers))
p3 = entity((1,1,1), BLUE_C,0)
self.play(Write(p3[1]))
#self.wait(1)
self.play(FadeOut(p3[1]))
l2 = Square(color=ORANGE, side_length=start_size/2, grid_xstep=start_size/4, grid_ystep=start_size/4)
l2.shift((start_size/4,start_size/4,0))
l2.set_z_index(-2)
l2.shift(shift_pos)
self.play(Create(l2))
self.wait(3)
t = Text("8kg", font_size=6)
t.move_to(p3[0])
p3_graph = VGroup(p3[0].copy(), t)
p3.color = GRAY
self.play(p3_graph.animate.scale(4).move_to(graph_pos).shift((-1.5,0,0)))
t = Text("14kg", font_size=20)
t.move_to(root_1)
t.set_z_index(3)
self.play(Write(plus))
self.play(Transform(root_1[1], t))
self.play(p3_graph.animate.move_to(p2_graph).shift((-1.5,0,0)))
plus_2 = Text("+")
plus_2.move_to(p2_graph).shift((-0.75,0,0))
self.play(Write(plus_2))
root_2 = p1_graph.copy()
root_2[0].color = ORANGE
self.play(Transform(VGroup(plus_2,p1_graph), root_2))