Initial commit
This commit is contained in:
74
src/visualization/web_renderer.py
Normal file
74
src/visualization/web_renderer.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""Web rendering for mazes (JSON format for Canvas)."""
|
||||
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
from ..core.maze import Maze
|
||||
|
||||
|
||||
class WebRenderer:
|
||||
"""Renders mazes as JSON data for web canvas visualization."""
|
||||
|
||||
@staticmethod
|
||||
def to_json_format(
|
||||
maze: Maze,
|
||||
solution_path: Optional[List[Tuple[int, int]]] = None,
|
||||
visited_cells: Optional[List[Tuple[int, int]]] = None
|
||||
) -> Dict:
|
||||
"""Convert maze to JSON format for web rendering.
|
||||
|
||||
Args:
|
||||
maze: Maze to render
|
||||
solution_path: Optional list of (row, col) tuples for solution
|
||||
visited_cells: Optional list of (row, col) tuples for visited cells
|
||||
|
||||
Returns:
|
||||
Dictionary with maze data for web rendering
|
||||
"""
|
||||
# Build walls array
|
||||
walls = []
|
||||
for row in maze.grid:
|
||||
row_walls = []
|
||||
for cell in row:
|
||||
cell_walls = {
|
||||
'north': cell.has_wall('north'),
|
||||
'south': cell.has_wall('south'),
|
||||
'east': cell.has_wall('east'),
|
||||
'west': cell.has_wall('west')
|
||||
}
|
||||
row_walls.append(cell_walls)
|
||||
walls.append(row_walls)
|
||||
|
||||
# Convert paths to sets for quick lookup
|
||||
solution_set = set(solution_path) if solution_path else set()
|
||||
visited_set = set(visited_cells) if visited_cells else set()
|
||||
|
||||
# Build cell states
|
||||
cell_states = []
|
||||
for row_idx in range(maze.rows):
|
||||
row_states = []
|
||||
for col_idx in range(maze.cols):
|
||||
pos = (row_idx, col_idx)
|
||||
state = 'normal'
|
||||
|
||||
if pos == maze.start:
|
||||
state = 'start'
|
||||
elif pos == maze.end:
|
||||
state = 'end'
|
||||
elif pos in solution_set:
|
||||
state = 'solution'
|
||||
elif pos in visited_set:
|
||||
state = 'visited'
|
||||
|
||||
row_states.append(state)
|
||||
cell_states.append(row_states)
|
||||
|
||||
return {
|
||||
'rows': maze.rows,
|
||||
'cols': maze.cols,
|
||||
'walls': walls,
|
||||
'cellStates': cell_states,
|
||||
'start': list(maze.start),
|
||||
'end': list(maze.end),
|
||||
'algorithm': maze.algorithm_used,
|
||||
'generationTime': maze.generation_time_ms
|
||||
}
|
||||
Reference in New Issue
Block a user