From 9287e2de10dd592b94ae9bb9213fbda128d36ccf Mon Sep 17 00:00:00 2001 From: timofej Date: Thu, 17 Aug 2023 13:05:15 +0200 Subject: [PATCH] Fix and clean display --- neuropercolation/display.py | 66 +++++++++++++++---------------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/neuropercolation/display.py b/neuropercolation/display.py index 6619826..5e1e775 100644 --- a/neuropercolation/display.py +++ b/neuropercolation/display.py @@ -33,7 +33,7 @@ class PygameEngine: By initializing pygame lazy the dependency can be dropped. """ - def __init__(self, window_size, top, *args, **kwargs): + def __init__(self, window_size, *args, **kwargs): super().__init__(*args, **kwargs) import pygame self._pygame = pygame @@ -43,7 +43,7 @@ class PygameEngine: self.__font = pygame.font.SysFont("monospace", 15) self._width = window_size[0] - self._height = window_size[1] + top + self._height = window_size[1] def write_text(self, pos, text, color=(0, 255, 0)): label = self.__font.render(text, True, color) @@ -65,10 +65,10 @@ class PygameEngine: class Simulate2Layers: def __init__(self, - cellular_automaton: Neuropercolation, - window_size=(1000, 800), - stretch_cells=False, - draw_engine=None, + dim, + eps, + res=10, + draw='pygame', state_to_color_cb=None, *args, **kwargs): """ @@ -81,10 +81,13 @@ class Simulate2Layers: :param state_to_color_cb: A callback to define the draw color of CA states (default: red for states != 0) """ super().__init__(*args, **kwargs) - self._cellular_automaton = cellular_automaton - self.__rect = _Rect(left=0, top=30, width=window_size[0], height=window_size[1]) - self.__calculate_cell_display_size(stretch_cells) - self.__draw_engine = PygameEngine(window_size, self.__rect.top) if draw_engine is None else draw_engine + self._cellular_automaton = Neuropercolation(dim,eps) + self.__cell_size = [res,res] + self.__gridside = dim*res + self.__samegap = 10 + self.__othergap = 20 + self.__rect = _Rect(left=0, top=30, width=dim*res, height=2*dim*res+self.__samegap) + self.__draw_engine = PygameEngine((self.__rect.width,self.__rect.height+self.__rect.top)) if draw == 'pygame' else draw self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb def run(self, @@ -123,15 +126,6 @@ class Simulate2Layers: def _not_at_the_end(self, last_evolution_step): return (self._cellular_automaton.evolution_step < last_evolution_step or last_evolution_step <= 0) - def __calculate_cell_display_size(self, stretch_cells): # pragma: no cover - grid_dimension = self._cellular_automaton.dimension - if stretch_cells: - self.__cell_size = [(self.__rect.width) / (grid_dimension[0]), - (self.__rect.height) / (grid_dimension[1]*2)] - else: - self.__cell_size = [int((self.__rect.width) / (grid_dimension[0])), - int((self.__rect.height) / (grid_dimension[1]*2))] - def _redraw_dirty_cells(self): self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells())) @@ -158,7 +152,7 @@ class Simulate2Layers: map(operator.mul, self.__cell_size, coord[:2]), - [0,(self.__rect.height/2+4)*(coord[2])])) + [0,(self.__gridside+self.__samegap)*(coord[2])])) def __calculate_cell_position_on_screen(self, cell_pos): return [self.__rect.left + cell_pos[0], self.__rect.top + cell_pos[1]] @@ -177,10 +171,10 @@ class Simulate2Layers: class Simulate4Layers: def __init__(self, - cellular_automaton: NeuropercolationCoupled, - window_size=(1000, 800), - stretch_cells=False, - draw_engine=None, + dim, + eps, + res=10, + draw='pygame', state_to_color_cb=None, *args, **kwargs): """ @@ -193,10 +187,13 @@ class Simulate4Layers: :param state_to_color_cb: A callback to define the draw color of CA states (default: red for states != 0) """ super().__init__(*args, **kwargs) - self._cellular_automaton = cellular_automaton - self.__rect = _Rect(left=0, top=30, width=window_size[0], height=window_size[1]) - self.__calculate_cell_display_size(stretch_cells) - self.__draw_engine = PygameEngine(window_size, self.__rect.top) if draw_engine is None else draw_engine + self._cellular_automaton = NeuropercolationCoupled(dim,eps) + self.__cell_size = [res,res] + self.__gridside = dim*res + self.__samegap = 10 + self.__othergap = 20 + self.__rect = _Rect(left=0, top=30, width=2*dim*res+self.__othergap, height=2*dim*res+self.__samegap) + self.__draw_engine = PygameEngine((self.__rect.width,self.__rect.height+self.__rect.top)) if draw == 'pygame' else draw self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb def run(self, @@ -235,15 +232,6 @@ class Simulate4Layers: def _not_at_the_end(self, last_evolution_step): return (self._cellular_automaton.evolution_step < last_evolution_step or last_evolution_step <= 0) - def __calculate_cell_display_size(self, stretch_cells): # pragma: no cover - grid_dimension = self._cellular_automaton.dimension - if stretch_cells: - self.__cell_size = [(self.__rect.width) / (grid_dimension[0]*2), - (self.__rect.height) / (grid_dimension[1]*2)] - else: - self.__cell_size = [int((self.__rect.width) / (grid_dimension[0]*2)), - int((self.__rect.height) / (grid_dimension[1]*2))] - def _redraw_dirty_cells(self): self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells())) @@ -270,8 +258,8 @@ class Simulate4Layers: map(operator.mul, self.__cell_size, coord[:2]), - [(self.__rect.width/2)*(coord[3]), - (self.__rect.height/2)*(coord[2])])) + [(self.__gridside+self.__othergap)*(coord[3]), + (self.__gridside+self.__samegap)*(coord[2])])) def __calculate_cell_position_on_screen(self, cell_pos): return [self.__rect.left + cell_pos[0], self.__rect.top + cell_pos[1]]