First version of usable Displayer class done

This commit is contained in:
Andreas Tsouchlos 2022-04-21 16:47:25 +02:00
parent aa430277f5
commit 2e92185afd
2 changed files with 40 additions and 14 deletions

View File

@ -4,32 +4,53 @@ import numpy as np
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
import functools
from time import sleep
import itertools
#
# Displayer class
#
class Displayer:
def __init__(self, width, height):
self._fig, self._ax = plt.subplots()
self._fig.set_figwidth(width)
self._fig.set_figheight(height)
self._xdata, self._ydata = [], []
self._obj, = plt.plot([], [], 'ro')
self._circle = plt.Circle((5, 5), 1, color='r', fill=False)
self._objects = [self._obj, self._circle]
self._object_map = {}
self._circle_map = {}
self._objects = []
def register_object(self, obj_name, obj_color='r'):
self._object_map[obj_name], = plt.plot([], [], color=obj_color, marker='o')
self._circle_map[obj_name] = plt.Circle((0, 0), 1, color=obj_color, fill=False)
self._objects.append(self._object_map[obj_name])
self._objects.append(self._circle_map[obj_name])
def move_object(self, obj_name, x, y):
self._object_map[obj_name].set_data([x, y])
self._circle_map[obj_name].center = (x, y)
def __init_animation(self):
self._ax.set_xlim(0, 20)
self._ax.set_ylim(0, 20)
self._ax.add_patch(self._circle)
for key in self._circle_map:
self._ax.add_patch(self._circle_map[key])
return self._objects
def __update_animation(self, t):
self._obj.set_data([t, t])
self._circle.center = (t, t)
self._circle.radius = t*0.1
return self._objects
def animate(self, n_steps):
anim = FuncAnimation(self._fig, self.__update_animation, frames=np.linspace(0, n_steps-1, n_steps),
def animate(self, n_steps, anim_callback):
def anim_callback_wrapper(t):
anim_callback(self, t)
return self._objects
anim = FuncAnimation(self._fig, anim_callback_wrapper, frames=np.linspace(0, n_steps-1, n_steps),
init_func=self.__init_animation, blit=True)
plt.show()

View File

@ -6,8 +6,13 @@ from matplotlib.animation import FuncAnimation
from display.dislpay_2d import Displayer
def update(disp, t):
disp.move_object("test", t, t)
disp.move_object("test2", 20-t, 20-t)
def run():
disp = Displayer(width=6, height=6)
# disp.show_object([1, 1], 4, "green")
disp.animate(n_steps = 100)
#disp.show_object([1, 1], 4, "green")
disp.register_object("test")
disp.register_object("test2", 'g')
disp.animate(n_steps = 100, anim_callback=update)