Add second layer to renamed Simulate2Layers visualiser
This commit is contained in:
parent
0561c62467
commit
34794ffb7a
@ -18,4 +18,4 @@ limitations under the License.
|
|||||||
from .neighborhood import Neighborhood, MooreNeighborhood, RadialNeighborhood, VonNeumannNeighborhood, \
|
from .neighborhood import Neighborhood, MooreNeighborhood, RadialNeighborhood, VonNeumannNeighborhood, \
|
||||||
HexagonalNeighborhood, EdgeRule
|
HexagonalNeighborhood, EdgeRule
|
||||||
from .automaton import Neuropercolation, NeuropercolationCoupled
|
from .automaton import Neuropercolation, NeuropercolationCoupled
|
||||||
from .display import CAWindow
|
from .display import Simulate2Layers
|
||||||
|
@ -33,7 +33,7 @@ class PygameEngine:
|
|||||||
By initializing pygame lazy the dependency can be dropped.
|
By initializing pygame lazy the dependency can be dropped.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, window_size, *args, **kwargs):
|
def __init__(self, window_size, top, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
import pygame
|
import pygame
|
||||||
self._pygame = pygame
|
self._pygame = pygame
|
||||||
@ -43,7 +43,7 @@ class PygameEngine:
|
|||||||
self.__font = pygame.font.SysFont("monospace", 15)
|
self.__font = pygame.font.SysFont("monospace", 15)
|
||||||
|
|
||||||
self._width = window_size[0]
|
self._width = window_size[0]
|
||||||
self._height = window_size[1]
|
self._height = window_size[1] + top
|
||||||
|
|
||||||
def write_text(self, pos, text, color=(0, 255, 0)):
|
def write_text(self, pos, text, color=(0, 255, 0)):
|
||||||
label = self.__font.render(text, True, color)
|
label = self.__font.render(text, True, color)
|
||||||
@ -63,7 +63,7 @@ class PygameEngine:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class CAWindow:
|
class Simulate2Layers:
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
cellular_automaton: Neuropercolation,
|
cellular_automaton: Neuropercolation,
|
||||||
window_size=(1000, 800),
|
window_size=(1000, 800),
|
||||||
@ -82,9 +82,9 @@ class CAWindow:
|
|||||||
"""
|
"""
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self._cellular_automaton = cellular_automaton
|
self._cellular_automaton = cellular_automaton
|
||||||
self.__rect = _Rect(left=0, top=30, width=window_size[0], height=window_size[1] - 30)
|
self.__rect = _Rect(left=0, top=30, width=window_size[0], height=window_size[1])
|
||||||
self.__calculate_cell_display_size(stretch_cells)
|
self.__calculate_cell_display_size(stretch_cells)
|
||||||
self.__draw_engine = PygameEngine(window_size) if draw_engine is None else draw_engine
|
self.__draw_engine = PygameEngine(window_size, self.__rect.top) if draw_engine is None else draw_engine
|
||||||
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
|
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
|
||||||
|
|
||||||
def run(self,
|
def run(self,
|
||||||
@ -125,11 +125,11 @@ class CAWindow:
|
|||||||
def __calculate_cell_display_size(self, stretch_cells): # pragma: no cover
|
def __calculate_cell_display_size(self, stretch_cells): # pragma: no cover
|
||||||
grid_dimension = self._cellular_automaton.dimension
|
grid_dimension = self._cellular_automaton.dimension
|
||||||
if stretch_cells:
|
if stretch_cells:
|
||||||
self.__cell_size = [self.__rect.width / grid_dimension[0],
|
self.__cell_size = [(self.__rect.width) / (grid_dimension[0]),
|
||||||
self.__rect.height / grid_dimension[1]]
|
(self.__rect.height) / (grid_dimension[1]*2)]
|
||||||
else:
|
else:
|
||||||
self.__cell_size = [int(self.__rect.width / grid_dimension[0]),
|
self.__cell_size = [int((self.__rect.width) / (grid_dimension[0])),
|
||||||
int(self.__rect.height / grid_dimension[1])]
|
int((self.__rect.height) / (grid_dimension[1]*2))]
|
||||||
|
|
||||||
def _redraw_dirty_cells(self):
|
def _redraw_dirty_cells(self):
|
||||||
self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells()))
|
self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells()))
|
||||||
@ -141,21 +141,27 @@ class CAWindow:
|
|||||||
|
|
||||||
def __redraw_cell(self, cell, coordinate):
|
def __redraw_cell(self, cell, coordinate):
|
||||||
cell_color = self.__state_to_color(cell.state)
|
cell_color = self.__state_to_color(cell.state)
|
||||||
|
if coordinate[2]==1:
|
||||||
|
cell_color = cell_color[::-1]
|
||||||
cell_pos = self.__calculate_cell_position_in_the_grid(coordinate)
|
cell_pos = self.__calculate_cell_position_in_the_grid(coordinate)
|
||||||
surface_pos = self.__calculate_cell_position_on_screen(cell_pos)
|
surface_pos = self.__calculate_cell_position_on_screen(cell_pos)
|
||||||
cell.is_dirty = False
|
cell.is_dirty = False
|
||||||
return self.__draw_cell_surface(surface_pos, cell_color)
|
return self.__draw_cell_surface(surface_pos, cell_color)
|
||||||
|
|
||||||
def _get_cell_color(self, current_state: Sequence) -> Sequence:
|
def _get_cell_color(self, current_state: Sequence) -> Sequence:
|
||||||
""" Returns the color of the cell depending on its current state """
|
""" Returns the color of the cell depending on its current state """
|
||||||
return 255 if current_state[0] else 0, 0, 0
|
return 255 if current_state[0] else 0, 0, 0
|
||||||
|
|
||||||
def __calculate_cell_position_in_the_grid(self, coordinate):
|
def __calculate_cell_position_in_the_grid(self, coord):
|
||||||
return list(map(operator.mul, self.__cell_size, coordinate))
|
return list(map(operator.add,
|
||||||
|
map(operator.mul,
|
||||||
|
self.__cell_size,
|
||||||
|
coord[:2]),
|
||||||
|
[0,(self.__rect.height/2+4)*(coord[2])]))
|
||||||
|
|
||||||
def __calculate_cell_position_on_screen(self, cell_pos):
|
def __calculate_cell_position_on_screen(self, cell_pos):
|
||||||
return [self.__rect.left + cell_pos[0], self.__rect.top + cell_pos[1]]
|
return [self.__rect.left + cell_pos[0], self.__rect.top + cell_pos[1]]
|
||||||
|
|
||||||
def __draw_cell_surface(self, surface_pos, cell_color):
|
def __draw_cell_surface(self, surface_pos, cell_color):
|
||||||
return self.__draw_engine.fill_surface_with_color((surface_pos, self.__cell_size), cell_color)
|
return self.__draw_engine.fill_surface_with_color((surface_pos, self.__cell_size), cell_color)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user