neighbours.py 872 B

123456789101112131415161718192021222324252627
  1. '''
  2. Created on 19 nov. 2016
  3. @author: olinox
  4. '''
  5. def neighbours_of(geometry, x, y):
  6. if geometry == 4:
  7. return squ_neighbours_of(x, y)
  8. elif geometry == 5:
  9. return hex_neighbours_of(x, y)
  10. else:
  11. raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
  12. def hex_neighbours_of(x, y):
  13. """ returns the list of coords of the neighbours of a cell on an hexagonal grid"""
  14. if x%2 == 0:
  15. return [(x, y-1), (x+1, y-1), (x+1, y), (x, y+1), (x-1, y), (x-1, y-1)]
  16. else:
  17. return [(x, y-1), (x+1, y), (x+1, y+1), (x, y+1), (x-1, y+1), (x-1, y)]
  18. def squ_neighbours_of(x, y):
  19. """ returns the list of coords of the neighbours of a cell on an square grid"""
  20. return [(x-1, y-1), (x, y-1), (x+1, y-1), \
  21. (x-1, y) , (x, y-1), (x+1, y) , \
  22. (x-1, y+1), (x, y+1),(x+1, y+1)]