Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses array for hex grid property layer fix #2651

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading