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

155
.gitignore vendored Normal file
View File

@@ -0,0 +1,155 @@
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
manimlib.egg-info/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
pytestdebug.log
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
doc/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
pyrightconfig.json
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pythonenv*
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# profiling data
.prof
# End of https://www.toptal.com/developers/gitignore/api/python
# Custom exclusions:
.DS_Store
# For manim
/videos
/custom_config.yml
media/

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

7
manim.cfg Normal file
View File

@@ -0,0 +1,7 @@
[CLI]
frame_rate = 30
pixel_height = 540
pixel_width = 960
background_color = BLACK
background_opacity = 1
scene_names = Default