Skip to content

Instantly share code, notes, and snippets.

@FractalWire
Last active February 10, 2021 00:44
Show Gist options
  • Save FractalWire/479b847f8fad5119c7efe03cc4f71dd9 to your computer and use it in GitHub Desktop.
Save FractalWire/479b847f8fad5119c7efe03cc4f71dd9 to your computer and use it in GitHub Desktop.
get the value of a wall
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment