Skip to content

Commit

Permalink
uses array for hex grid property layer fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sahil-Chhoker committed Jan 28, 2025
1 parent efed03e commit 4e0dec9
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions mesa/visualization/mpl_space_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import contextlib
import itertools
import warnings
from collections.abc import Callable, Iterator
from collections.abc import Callable
from functools import lru_cache
from itertools import pairwise
from typing import Any
Expand Down Expand Up @@ -163,7 +163,7 @@ def draw_space(
@lru_cache(maxsize=1024, typed=True)
def _get_hexmesh(
width: int, height: int, size: float = 1.0
) -> Iterator[list[tuple[float, float]]]:
) -> list[tuple[float, float]]:
"""Generate hexagon vertices for the mesh. Yields list of vertex coordinates for each hexagon."""

# Helper function for getting the vertices of a hexagon given the center and size
Expand All @@ -183,12 +183,15 @@ def _get_hex_vertices(

x_spacing = np.sqrt(3) * size
y_spacing = 1.5 * size
hexagons = []

for row, col in itertools.product(range(height), range(width)):
# Calculate center position with offset for even rows
x = col * x_spacing + (row % 2 == 0) * (x_spacing / 2)
y = row * y_spacing
yield _get_hex_vertices(x, y, size)
hexagons.append(_get_hex_vertices(x, y, size))

return hexagons


def draw_property_layers(
Expand Down Expand Up @@ -411,7 +414,8 @@ def setup_hexmesh(width, height):
edges = set()

# Generate edges for each hexagon
for vertices in _get_hexmesh(width, height):
hexagons = _get_hexmesh(width, height)
for vertices in hexagons:
# Edge logic, connecting each vertex to the next
for v1, v2 in pairwise([*vertices, vertices[0]]):
# Sort vertices to ensure consistent edge representation and avoid duplicates.
Expand Down

0 comments on commit 4e0dec9

Please sign in to comment.