Skip to content

Instantly share code, notes, and snippets.

@s4nyam
Last active June 26, 2023 07:45
Show Gist options
  • Save s4nyam/86fbabee958532ab75f08b08b958c198 to your computer and use it in GitHub Desktop.
Save s4nyam/86fbabee958532ab75f08b08b958c198 to your computer and use it in GitHub Desktop.
Simulate HexCA inspired from Hugo Cisneros Thesis, Plots and save the neighborhood used in HexCA
import pygame
import numpy as np
# Setting parameters
width = 100
height = 100
cell_size = 15
spacing = 2
live_color = (0, 150, 0)
dead_color = (0, 0, 0)
population_density = 0.001
# Replicator Rule
survivelist = [1,3,5,7]
bornlist = [1,3,5,7]
# # GoL rule
# survivelist = [2,3]
# bornlist = [3]
num_live_cells = int(width * height * population_density)
grid = np.zeros((height, width), dtype=int)
random_indices = np.random.choice(width * height, num_live_cells, replace=False)
grid.flat[random_indices] = 1
pygame.init()
window_width = width * (cell_size + spacing) + spacing
window_height = height * (cell_size + spacing) + spacing
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Hexagonal Cellular Automaton")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
new_grid = np.copy(grid)
for row in range(height):
for col in range(width):
center_x = (col * (cell_size + spacing) + cell_size / 2) + spacing
center_y = (row * (cell_size + spacing) + cell_size / 2) + spacing
points = []
for i in range(6):
angle_deg = 60 * i
angle_rad = np.radians(angle_deg)
point_x = center_x + cell_size / 2 * np.cos(angle_rad)
point_y = center_y + cell_size / 2 * np.sin(angle_rad)
points.append((point_x, point_y))
pygame.draw.polygon(window, live_color if grid[row, col] == 1 else dead_color, points)
live_neighbors = 0
for dx, dy in [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]:
new_row = row + dy
new_col = col + dx
if new_row < 0:
new_row += height
elif new_row >= height:
new_row -= height
if new_col < 0:
new_col += width
elif new_col >= width:
new_col -= width
live_neighbors += grid[new_row, new_col]
if grid[row, col] == 1 and live_neighbors not in survivelist:
new_grid[row, col] = 0
elif grid[row, col] == 0 and live_neighbors in bornlist:
new_grid[row, col] = 1
grid = np.copy(new_grid)
pygame.display.update()
pygame.quit()
import numpy as np
import matplotlib.pyplot as plt
neighborhood_coords = [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]
fig = plt.figure(figsize=(5, 5), dpi=100)
plt.scatter(*zip(*neighborhood_coords), color='green', s=100)
for coord in neighborhood_coords:
plt.annotate(f'({coord[0]}, {coord[1]})', xy=coord, xytext=(coord[0] + 0.2, coord[1] + 0.2),
arrowprops=dict(facecolor='black', arrowstyle='->'))
origin = (0, 0)
for coord in neighborhood_coords:
plt.plot([origin[0], coord[0]], [origin[1], coord[1]], color='black')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Hexagonal Neighborhood')
plt.savefig('neighborhood.png')
plt.close(fig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment