# -*- coding: utf-8 -*-
import numpy as np
from manim import *


def ruler(sc: Scene, p1, p2, color=GREEN, angle=PI, axis=OUT):
    """圆规动画：p1 是针（不动），p2 是笔芯（绕 p1 转动画弧）"""
    d1 = Dot(point=p1, color=RED)
    d2 = Dot(point=p2, color=color)
    dl = DashedLine(d1.get_center(), d2.get_center())

    r = np.linalg.norm(p2 - p1)
    arc = ArcBetweenPoints(p2, p2, stroke_width=2)

    dl.add_updater(lambda z: z.become(DashedLine(d1.get_center(), d2.get_center())))
    if np.array_equal(axis, OUT):
        arc.add_updater(
            lambda z: z.become(
                ArcBetweenPoints(
                    p2,
                    d2.get_center(),
                    radius=r,
                    stroke_color=color,
                    stroke_width=2,
                )
            )
        )
    if np.array_equal(axis, IN):
        arc.add_updater(
            lambda z: z.become(
                ArcBetweenPoints(
                    d2.get_center(),
                    p2,
                    radius=r,
                    stroke_color=color,
                    stroke_width=2,
                )
            )
        )

    sc.add(d1, d2, dl, arc)
    sc.play(
        Rotate(
            d2, about_point=d1.get_center(), axis=axis, angle=angle, rate_func=linear
        )
    )

    arc.clear_updaters()
    dl.clear_updaters()
    sc.remove(d1, d2, dl)
    return arc


class RulerDemo(Scene):
    def construct(self):
        ruler(self, np.array([-1, 0, 0]), np.array([-1, 1, 0]))
        ruler(self, np.array([1, 0, 0]), np.array([1, 1, 0]), axis=IN)
        self.wait()
