egm/animation.py

95 lines
2.2 KiB
Python

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
ax.cla()
@switch_decorator
def pause(self):
ax = self._ax
self._ticks += 1
ticks = self._ticks
ax.set_title(f"{ticks}")
plt.pause(0.02)
self.clear()
self.init_fig()
pass