Skip to content
This repository was archived by the owner on Jan 25, 2025. It is now read-only.

Commit 19bdb26

Browse files
committed
use global vars, fns
1 parent 82a501f commit 19bdb26

1 file changed

Lines changed: 64 additions & 39 deletions

File tree

ma/physics.py

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
import math
44
from typing import Callable
55

6-
MAX_U = 3 * PI
7-
SKIP_SECTIONS = False
6+
MAX_U: float = 3 * PI
7+
SKIP_SECTIONS: bool = True
8+
9+
u_label = None
10+
axes_3d = None
11+
u = 1.2
12+
hl_pos_dot = None
813

914

1015
class Physics(ThreeDScene):
1116
def construct(self) -> None:
17+
global axes_3d, u_label, u, hl_pos_dot
1218
# inital scene
1319
self.next_section(skip_animations=SKIP_SECTIONS)
1420
self.set_camera_orientation(phi=60 * DEGREES, theta=-60 * DEGREES)
@@ -19,27 +25,27 @@ def construct(self) -> None:
1925
xl = xr[1] - xr[0]
2026
yl = yr[1] - yr[0]
2127
zl = zr[1] - zr[0]
22-
self.axes_3d = ThreeDAxes(
28+
axes_3d = ThreeDAxes(
2329
x_range=xr, y_range=yr, z_range=zr, x_length=xl, y_length=yl, z_length=zl
2430
).move_to(np.array([0, 0, -1]))
2531

2632
constant = Text("Constant").to_corner(UR)
2733
self.add_fixed_in_frame_mobjects(constant)
2834
varies = Text("Varies").to_edge(RIGHT)
2935
self.add_fixed_in_frame_mobjects(varies)
30-
labels = self.axes_3d.get_axis_labels()
36+
labels = axes_3d.get_axis_labels()
3137
self.add(labels)
32-
self.add(self.axes_3d)
38+
self.add(axes_3d)
3339

3440
# show hl curve
3541
self.next_section(skip_animations=SKIP_SECTIONS)
3642

37-
curve = ParametricFunction(f(self.axes_3d), t_range=np.array([0, MAX_U]))
43+
curve = ParametricFunction(r, t_range=np.array([0, MAX_U]))
3844
self.add(curve)
3945
curve_label = (
4046
MathTex(r"\vec{r}")
4147
.add_background_rectangle(color=BLACK)
42-
.move_to(f(self.axes_3d)(MAX_U) + np.array([0, 0, 0]))
48+
.move_to(r(MAX_U) + np.array([0, 0, 0]))
4349
)
4450
self.add_fixed_orientation_mobjects(curve_label)
4551
vec_r = MathTex(r"\vec{r}").next_to(constant, DOWN, aligned_edge=LEFT)
@@ -51,39 +57,58 @@ def construct(self) -> None:
5157
# parameterize by u
5258
self.next_section()
5359

54-
self.u: float = 1.2
55-
self.u_label = MathTex(r"\vec{r}(u)")
56-
self.add_fixed_orientation_mobjects(self.u_label)
57-
self.hl_pos_dot = Dot3D().move_to(f(self.axes_3d)(self.u))
58-
self.add(self.hl_pos_dot)
59-
self.play(Create(self.hl_pos_dot))
60-
self.hl_pos_dot.add_updater(self.update_hl_pos)
61-
self.u_label.add_updater(
62-
self.update_u_label
60+
u_label = MathTex(r"\vec{r}(u)")
61+
self.add_fixed_orientation_mobjects(u_label)
62+
hl_pos_dot = Dot3D().move_to(r(u))
63+
self.add(hl_pos_dot)
64+
self.play(Create(hl_pos_dot))
65+
hl_pos_dot.add_updater(update_hl_pos)
66+
u_label.add_updater(
67+
update_u_label
6368
)
64-
self.add_updater(self.increase_u)
69+
self.add_updater(increase_u)
70+
71+
self.wait(1, frozen_frame=False)
72+
self.play(u_label.animate.set_opacity(0.3))
73+
self.wait()
74+
75+
# com pos
76+
self.next_section()
6577

66-
self.wait(5, frozen_frame=False)
67-
#self.u_label.a
68-
#self.play(self.u_label.animate.opacity(0.2))
78+
up = Vector([])
6979

70-
def increase_u(self, dt: float):
71-
self.u += dt
7280

73-
def update_u_label(self, m: Mobject) -> None:
74-
if isinstance(m, MathTex):
75-
m.next_to(self.hl_pos_dot, UP)
76-
else:
77-
raise TypeError
78-
79-
def update_hl_pos(self, m: Mobject) -> None:
80-
#print(self.u)
81-
#self.u += dt
82-
m.move_to(f(self.axes_3d)(self.u))
83-
84-
85-
def f(axes: ThreeDAxes) -> Callable[[float], np.ndarray]:
86-
def f_(t: float) -> np.ndarray:
87-
p = np.array([t, 0, 2 - t / 3 + math.sin(t / 3)])
88-
return axes.c2p(*p)
89-
return f_
81+
def update_u_label(m: Mobject) -> None:
82+
if isinstance(m, MathTex):
83+
if hl_pos_dot is None:
84+
raise ValueError
85+
m.next_to(hl_pos_dot, UP)
86+
else:
87+
raise TypeError
88+
89+
90+
def increase_u(dt: float):
91+
global u
92+
u += dt
93+
94+
95+
def update_hl_pos(m: Mobject) -> None:
96+
#print(self.u)
97+
#self.u += dt
98+
if axes_3d is None:
99+
raise ValueError
100+
m.move_to(r(u))
101+
102+
103+
def r(t: float) -> np.ndarray:
104+
p = np.array([t, 0, 2 - t / 3 + math.sin(t / 3)])
105+
if axes_3d is None:
106+
raise ValueError
107+
return axes_3d.c2p(*p)
108+
109+
110+
def dr(t: float) -> np.ndarray:
111+
d = np.array([1, 0, -1 / 3 + 1 / 3 * math.cos(t / 3)])
112+
if axes_3d is None:
113+
raise ValueError
114+
return axes_3d.c2p(*d)

0 commit comments

Comments
 (0)