Đường Cycloid
SÁNG TẠO VIDEO VỚI MANIM https://www.facebook.com/groups/manimceunknown
python
4 years ago
4.5 kB
107
Indexable
from typing_extensions import runtime
from manim import *
import math
class Move0(Scene):
def construct(self):
# Định nghĩa phương trình tham số đường Cycloid
def cycloid(R,start,end):
func = lambda t: np.array([R*(t-math.sin(t)),R*(1-math.cos(t)), 0])
graph = ParametricFunction(
func,
t_range = np.array([start,end]),
color=PURPLE)
return graph
# tạo đường Cycloid
cyc = cycloid(1,-2*PI,2*PI)
# Dịch chuyển phía dưới 1 đơn vị
cyc.shift(DOWN)
# text
text=Text('Đường Cycloid',gradient=(BLUE, GREEN),font='Helvetica Neue',weight='ULTRALIGHT')
# scale text
text.scale(0.85)
# Đưa text lên cạnh trên màn hình
text.to_edge(UP,buff=1)
# Xuất text ra màn hình
self.play(FadeIn(text,scale=3))
# Đợi 1 giây
self.wait()
# tạo biến hệ số scale để tùy chỉnh
num_scale=1
# tạo đường tròn
cir=Circle().scale(num_scale)
# gán màu cho đường tròn
cir.set_color(BLUE)
# Tạo biến chạy theta
theta = ValueTracker(-2 * PI)
# Tạo đường nối tâm và bán kính
rad=Line(ORIGIN,num_scale*DOWN).rotate_about_origin(-theta.get_value())
# Tạo Dot trên đường tròn
dot=Dot().move_to(rad.get_end())
# Đặt nhóm cho cir,dot,rad
rolling_circle = VGroup(cir,dot,rad)
# Thêm một text khác
text1=Text('Cho một đường tròn và một điểm cố định nằm trên nó',gradient=(BLUE, GREEN),font='Helvetica Neue',weight='ULTRALIGHT')
# Xuất đường tròn ra màn hình
self.play(Create(cir))
text1.scale(0.85)
text1.to_edge(DOWN,buff=1)
# Xuất text1 và dot ra màn hình
self.play(FadeIn(text1,shift=UP))
self.play(FadeIn(dot,scale=2))
self.wait(2)
# Xóa text1 khỏi màn hình
self.play(FadeOut(text1,shift=DOWN))
# thêm 1 text khác
text1=Text('Đặt đường tròn trên một trục nằm ngang',gradient=(BLUE, GREEN),font='Helvetica Neue',weight='ULTRALIGHT')
text1.scale(0.85)
text1.to_edge(DOWN,buff=1)
self.play(FadeIn(text1,shift=UP))
# Tạo 1 đường thẳng nối từ mép trái đến mép phải màn hình
line=Line(config.frame_width*LEFT/2,config.frame_width*RIGHT/2).shift(num_scale*DOWN)
self.wait(2)
# Di chuyển rolling_circle sang bên phải theta.get_value() * RIGHT đơn vị
self.play(rolling_circle.animate.shift(theta.get_value() * RIGHT))
# Đổi hướng đường thẳng từ phải sang trái (để làm hiệu ứng!)
self.play(Create(line.reverse_direction()),runtime=2)
self.wait()
self.play(FadeOut(text1,shift=DOWN))
# text1 kiểu VGroup cho đẹp!
text1 = VGroup(
Text('Cho đường tròn lăn trên trục',gradient=(BLUE, GREEN),font='Helvetica Neue',weight='ULTRALIGHT'),
Text('Quỹ tích điểm đã cho ban đầu là đường Cycloid',gradient=(BLUE, GREEN),font='Helvetica Neue',weight='ULTRALIGHT')
).scale(0.85).arrange(direction = DOWN, buff = 0.125)
text1.to_edge(DOWN,buff=1)
self.play(FadeIn(text1,shift=UP))
self.wait(2)
# Thao tác nâng cao, tạo phương thức add_update
def cupd(obj):
ncirc = Circle().scale(num_scale).set_color(BLUE)
nline = Line(ORIGIN,num_scale*DOWN).rotate_about_origin(-theta.get_value())
ndot = Dot().move_to(nline.get_end())
n_grp = VGroup(ncirc, nline, ndot)
n_grp.shift(theta.get_value()* RIGHT)
obj.become(n_grp)
# Gán phương thức updater cho rolling_circle
rolling_circle.add_updater(cupd)
self.add(rolling_circle)
self.play(
AnimationGroup(
theta.animate(run_time=10).increment_value(4 * PI),
Create(cyc,run_time=10),
lag_ratio=0
)
)
self.wait(2)
# remove phương thức updater
rolling_circle.remove_updater(cupd)
# Tạo hiệu ứng xóa toàn bộ màn hình
picture= Group(*self.mobjects)
self.play(FadeOut(picture,shift=3*LEFT,scale=3),run_time=1)
Editor is loading...