From 13e25832edc9029b42d2ed6adc17c4a8caeaa9ed Mon Sep 17 00:00:00 2001 From: n3040 Date: Wed, 3 Jan 2024 16:47:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=9E=E7=8E=B0=E4=BA=86?= =?UTF-8?q?=E5=8A=A8=E7=94=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- animation.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 animation.py diff --git a/animation.py b/animation.py new file mode 100644 index 0000000..af63352 --- /dev/null +++ b/animation.py @@ -0,0 +1,91 @@ +import matplotlib.pyplot as plt +from functools import wraps +import numpy as np + + +class Animation: + def __init__(self) -> None: + fig, ax = plt.subplots() + self._fig = fig + self._ax = ax + self._ticks = 0 + self._disable = False + self.init_fig() + pass + + @staticmethod + def switch_decorator(func): + @wraps(func) + def not_run(cls, *args, **kwargs): + # print("not run") + pass + + @wraps(func) + def wrapTheFunction(cls, *args, **kwargs): + if not cls._disable: + # print("desc") + return func(cls, *args, **kwargs) + return not_run(cls,*args, **kwargs) + + return wrapTheFunction + + def disable(self, _disable): + self._disable = _disable + @switch_decorator + def init_fig(self): + ax = self._ax + ax.set_aspect(1) + ax.set_xlim([-500, 500]) + ax.set_ylim([-500, 500]) + @switch_decorator + def show(self): + self._fig.show() + + @switch_decorator + def add_rg_line(self, line_func): + ax = self._ax + x = np.linspace(0, 300) + y = line_func(x) + ax.plot(x, y) + @switch_decorator + def add_rs(self, rs, rs_x, rs_y): + ax = self._ax + ax.add_artist(plt.Circle((rs_x, rs_y), rs, fill=False)) + @switch_decorator + def add_rc(self, rc, rc_x, rc_y): + ax = self._ax + ax.add_artist(plt.Circle((rc_x, rc_y), rc, fill=False)) + + # 增加暴露弧范围 + @switch_decorator + def add_expose_area( + self, + rc_x, + rc_y, + intersection_x1, + intersection_y1, + intersection_x2, + intersection_y2, + ): + ax = self._ax + ax.plot([rc_x, intersection_x1], [rc_y, intersection_y1], color="red") + ax.plot([rc_x, intersection_x2], [rc_y, intersection_y2], color="red") + pass + @switch_decorator + def clear(self): + ax = self._ax + # fig = self._fig + ax.cla() + @switch_decorator + def pause(self): + # fig=self._fig + # print('tick') + ax = self._ax + self._ticks += 1 + ticks = self._ticks + ax.set_title(f"{ticks}") + plt.pause(0.02) + self.clear() + self.init_fig() + + pass