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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| from manim import * import numpy as np from numpy import array run_time = 1 data = [[1, 2], [3, 4]] def dot(pos, **kwargs): return Dot(pos, **kwargs) import sympy as sp x, y = sp.symbols('x y', real=True) a, b, c = 3, 5, 4
eq1 = (x - 0)**2 + (y - 0)**2 - b**2 eq2 = (x - c)**2 + (y - 0)**2 - a**2 solutions = sp.solve([eq1, eq2], (x, y))
import sympy as sp x, y = sp.symbols('x y', real=True) b, c, theta = 4, 5, sp.rad(60)
eq1 = x**2 + y**2 - b**2
bc_sq = c**2 + b**2 - 2*c*b*sp.cos(theta) eq2 = (x - c)**2 + y**2 - bc_sq solutions = sp.solve([eq1, eq2], (x, y))
valid_solution = [sol for sol in solutions if sol[1] >= 0][0] import sympy as sp import numpy as np class SSSCongruenceDemo(Scene): def construct(self): A = np.array([-1, 0, 0]) B = np.array([1, 0, 0]) a_tracker = ValueTracker(2) b_tracker = ValueTracker(2) triangles = always_redraw( lambda: self.get_triangles( A, B, a_tracker.get_value(), b_tracker.get_value() ) ) self.play(*[FadeIn(_o) for _o in [triangles]], run_time=1.0) labels = always_redraw( lambda: self.get_labels(A, B, a_tracker.get_value(), b_tracker.get_value()) ) self.play(*[FadeIn(_o) for _o in [labels]], run_time=1.0) side_labels = always_redraw( lambda: self.get_side_labels( A, B, a_tracker.get_value(), b_tracker.get_value() ) ) self.play(*[FadeIn(_o) for _o in [side_labels]], run_time=1.0) - **改变 BC 的长度**:C 点沿一条弧线移动,三角形的形状随之变化,但始终满足给定的三边长度约束。你能直观看到「改变一边,三角形形状唯一确定」。 self.play(a_tracker.animate.set_value(3), run_time=2) self.play(b_tracker.animate.set_value(1), run_time=2) self.play( a_tracker.animate.set_value(2), b_tracker.animate.set_value(2), run_time=2 ) self.wait() def solve_vertex_C(self, A, B, a, b): """用 SymPy 求解顶点 C,返回两个镜像点的 np.array 坐标列表""" x, y = sp.symbols("x y", real=True) c = np.linalg.norm(B - A) eq1 = (x - 0) ** 2 + (y - 0) ** 2 - b**2 eq2 = (x - c) ** 2 + (y - 0) ** 2 - a**2 solutions = sp.solve([eq1, eq2], (x, y), dict=True) if not solutions: return [] AB_vec = B - A x_axis = AB_vec / c y_axis = np.array([-x_axis[1], x_axis[0], 0]) C_points = [] for sol in solutions: sol_x = float(sp.N(sol[x])) sol_y = float(sp.N(sol[y])) C = A + sol_x * x_axis + sol_y * y_axis C_points.append(C) return C_points def get_triangles(self, A, B, a, b): C_points = self.solve_vertex_C(A, B, a, b) if not C_points: return VGroup() triangles = VGroup() colors = [BLUE, GREEN] for i, C in enumerate(C_points): triangle = Polygon( A, B, C, color=colors[i % 2], fill_opacity=0.3, stroke_width=2 ) triangles.add(triangle) return triangles def get_labels(self, A, B, a, b): C_points = self.solve_vertex_C(A, B, a, b) if not C_points: return VGroup() label_A = MathTex("A", color=WHITE, font_size=28).next_to(A, DL, buff=0.15) label_B = MathTex("B", color=WHITE, font_size=28).next_to(B, DR, buff=0.15) labels = VGroup(label_A, label_B) for i, C in enumerate(C_points): direction = UP if C[1] >= A[1] else DOWN label_C = MathTex(f"C_{i+1}", color=WHITE, font_size=28).next_to( C, direction, buff=0.15 ) labels.add(label_C) return labels def get_side_labels(self, A, B, a, b): C_points = self.solve_vertex_C(A, B, a, b) if not C_points: return VGroup() c = np.linalg.norm(B - A) labels = VGroup() label_AB = MathTex(f"{c:.1f}", font_size=24, color=YELLOW).move_to( (A + B) / 2 + DOWN * 0.3 ) labels.add(label_AB) for i, C in enumerate(C_points): direction = LEFT if C[0] < (A[0] + B[0]) / 2 else RIGHT label_AC = MathTex(f"{b:.1f}", font_size=24, color=RED).move_to( (A + C) / 2 + direction * 0.3 ) label_BC = MathTex(f"{a:.1f}", font_size=24, color=RED).move_to( (B + C) / 2 + (-direction) * 0.3 ) labels.add(label_AC, label_BC) return labels
|