daily pastebin goal
29%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from math import cos, sin, pi
  2. from enum import IntFlag
  3. import numpy as np
  4.  
  5. # OUTPUT :
  6. #
  7. # Walkable space :
  8. # [[ True  True  True  True  True]
  9. #  [ True False False False  True]
  10. #  [ True False False False  True]
  11. #  [ True False False False  True]
  12. #  [ True  True  True  True  True]]
  13. #
  14. # Corners:
  15. # (1, 1) Wall.SOUTH|EAST
  16. # (1, 3) Wall.NORTH|EAST
  17. # (3, 1) Wall.WEST|SOUTH
  18. # (3, 3) Wall.NORTH|WEST
  19. #
  20. # Wall space :
  21. # [[ 0  0  0  0  0]
  22. #  [ 0  3  7  6  0]
  23. #  [ 0 11 15 14  0]
  24. #  [ 0  9 13 12  0]
  25. #  [ 0  0  0  0  0]]
  26.  
  27.  
  28. class Wall(IntFlag):
  29.     EAST = 2**0
  30.     SOUTH = 2**1
  31.     WEST = 2**2
  32.     NORTH = 2**3
  33.  
  34.  
  35. def fetch_wall(pos, walkable_space):
  36.     x, y = pos
  37.     height, width = walkable_space.shape
  38.     if walkable_space[y, x]:
  39.         return 0
  40.  
  41.     wall = 0
  42.     for i in range(4):
  43.         dx, dy = round(cos(i*pi/2)), round(sin(i*pi/2))
  44.         if 0 <= y+dy < height and 0 <= x+dx < width:
  45.             wall += (not walkable_space[y+dy, x+dx]) * 2**i
  46.     return wall
  47.  
  48.  
  49. def main():
  50.     ws = np.ones((5, 5), dtype=bool)
  51.     height, width = ws.shape
  52.     ws[1:4, 1:4] = False
  53.  
  54.     print("Walkable space :")
  55.     print(ws)
  56.     print()
  57.  
  58.     positions = [(x, y) for x in range(1, 4, 2) for y in range(1, 4, 2)]
  59.     print("Corners:")
  60.     for pos in positions:
  61.         print(pos, Wall(fetch_wall(pos, ws)))
  62.     print()
  63.  
  64.     wall_space = np.zeros(ws.shape, np.int8)
  65.     for j in range(height):
  66.         for i in range(width):
  67.             wall_space[j, i] = fetch_wall((i, j), ws)
  68.     print("Wall space :")
  69.     print(wall_space)
  70.  
  71.  
  72. if __name__ == "__main__":
  73.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top