90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
from manim import *
|
||
import manim
|
||
|
||
|
||
class CoordinateSystem(Scene):
|
||
def construct(self):
|
||
# ===== 左侧坐标系 =====
|
||
# 使用Axes替代NumberPlane以获得更多功能
|
||
left_axes = Axes(
|
||
x_range=[-4, 4, 1],
|
||
y_range=[0, 20, 5],
|
||
x_length=4,
|
||
y_length=4,
|
||
axis_config={"color": BLUE}
|
||
).add_coordinates()
|
||
left_axes.shift(LEFT * 3 + DOWN * 1.5)
|
||
|
||
# 修正1:正确的函数表达式和参数
|
||
left_graph = left_axes.plot(
|
||
lambda x: x ** 2, # 修正指数函数
|
||
x_range=[-4, 4],
|
||
color=GREEN
|
||
)
|
||
|
||
# 修正2:使用正确的黎曼矩形方法
|
||
riemann_area = left_axes.get_riemann_rectangles(
|
||
left_graph,
|
||
x_range=[-2, 2],
|
||
dx=0.2, # 调整密度
|
||
color=[BLUE, GREEN],
|
||
input_sample_type="right"
|
||
)
|
||
|
||
# ===== 右侧坐标系 =====
|
||
right_axes = Axes(
|
||
x_range=[-4, 4, 1],
|
||
y_range=[-20, 20, 5],
|
||
x_length=4,
|
||
y_length=4,
|
||
axis_config={"color": RED}
|
||
).add_coordinates()
|
||
right_axes.shift(RIGHT * 3 + DOWN * 1.5)
|
||
|
||
right_graph = right_axes.plot(
|
||
lambda x: 2 * x,
|
||
x_range=[-4, 4],
|
||
color=YELLOW
|
||
)
|
||
|
||
# 修正3:正确的垂线方法
|
||
v_lines = right_axes.get_vertical_lines_to_graph(
|
||
right_graph,
|
||
x_range=[-3, 3],
|
||
num_lines=12,
|
||
color=WHITE
|
||
)
|
||
|
||
# ===== 动画序列 =====
|
||
# 修正4:分步动画展示
|
||
self.play(
|
||
LaggedStart(
|
||
Create(left_axes),
|
||
Create(right_axes),
|
||
lag_ratio=0.5
|
||
),
|
||
run_time=2
|
||
)
|
||
self.play(
|
||
Create(left_graph),
|
||
Create(right_graph),
|
||
run_time=3
|
||
)
|
||
self.play(
|
||
FadeIn(riemann_area),
|
||
Create(v_lines),
|
||
run_time=2
|
||
)
|
||
self.wait()
|
||
|
||
|
||
if __name__ == '__main__':
|
||
config = {
|
||
"quality": "low_quality",
|
||
"preview": True,
|
||
"media_dir": "./output"
|
||
}
|
||
|
||
with manim.tempconfig(config):
|
||
scene = CoordinateSystem()
|
||
scene.render() |