反比例函数 y=25/x 上取一点,向两轴作垂线围成矩形,面积恒为 25。示例用 always_redraw 每帧重建多边形,拖动 ValueTracker 时矩形实时变形。
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| from manim import * class PolygonOnAxes(Scene): def get_rectangle_corners(self, bottom_left, top_right): return [ (top_right[0], top_right[1]), (bottom_left[0], top_right[1]), (bottom_left[0], bottom_left[1]), (top_right[0], bottom_left[1]), ] def construct(self): ax = Axes( x_range=[0, 10], y_range=[0, 10], x_length=6, y_length=6, axis_config={"include_tip": False}, ) t = ValueTracker(5) k = 25 graph = ax.plot( lambda x: k / x, color=YELLOW_D, x_range=[k / 10, 10.0, 0.01], use_smoothing=False, ) def get_rectangle(): polygon = Polygon( *[ ax.c2p(*i) for i in self.get_rectangle_corners( (0, 0), (t.get_value(), k / t.get_value()) ) ] ) polygon.stroke_width = 1 polygon.set_fill(BLUE, opacity=0.5) polygon.set_stroke(YELLOW_B) return polygon polygon = always_redraw(get_rectangle) dot = Dot() dot.add_updater(lambda x: x.move_to(ax.c2p(t.get_value(), k / t.get_value()))) dot.set_z_index(10) self.play(*[FadeIn(_o) for _o in [ax, graph, dot]], run_time=1.0) self.play(Create(polygon)) self.play(t.animate.set_value(10)) self.play(t.animate.set_value(k / 10)) self.play(t.animate.set_value(5)) self.wait(0.60)
|
演示视频
下载 Python 脚本
要点
always_redraw(func) 等价于「建一次 + 每帧重建」,适合随参数变化的图形
Polygon(*points) 的顶点顺序决定填充区域,顺序错会自交
set_z_index(10) 把点提到最上层,避免被多边形盖住
代码来源:Manim Community 官方 Example Gallery 示例 PolygonOnAxes(MIT),已在 Manim CE 0.20.1 实跑验证并生成上方视频。
主观感受,仅供参考。本文采用 本文采用 CC BY-NC-SA 4.0 许可 协议,
完整转载请注明来自
数学动画引擎。