Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import cos, sin, pi
- from enum import IntFlag
- import numpy as np
- # OUTPUT :
- #
- # Walkable space :
- # [[ True True True True True]
- # [ True False False False True]
- # [ True False False False True]
- # [ True False False False True]
- # [ True True True True True]]
- #
- # Corners:
- # (1, 1) Wall.SOUTH|EAST
- # (1, 3) Wall.NORTH|EAST
- # (3, 1) Wall.WEST|SOUTH
- # (3, 3) Wall.NORTH|WEST
- #
- # Wall space :
- # [[ 0 0 0 0 0]
- # [ 0 3 7 6 0]
- # [ 0 11 15 14 0]
- # [ 0 9 13 12 0]
- # [ 0 0 0 0 0]]
- class Wall(IntFlag):
- EAST = 2**0
- SOUTH = 2**1
- WEST = 2**2
- NORTH = 2**3
- def fetch_wall(pos, walkable_space):
- x, y = pos
- height, width = walkable_space.shape
- if walkable_space[y, x]:
- return 0
- wall = 0
- for i in range(4):
- dx, dy = round(cos(i*pi/2)), round(sin(i*pi/2))
- if 0 <= y+dy < height and 0 <= x+dx < width:
- wall += (not walkable_space[y+dy, x+dx]) * 2**i
- return wall
- def main():
- ws = np.ones((5, 5), dtype=bool)
- height, width = ws.shape
- ws[1:4, 1:4] = False
- print("Walkable space :")
- print(ws)
- print()
- positions = [(x, y) for x in range(1, 4, 2) for y in range(1, 4, 2)]
- print("Corners:")
- for pos in positions:
- print(pos, Wall(fetch_wall(pos, ws)))
- print()
- wall_space = np.zeros(ws.shape, np.int8)
- for j in range(height):
- for i in range(width):
- wall_space[j, i] = fetch_wall((i, j), ws)
- print("Wall space :")
- print(wall_space)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement