'How to stop the points from overlapping the barriers when taking corners?
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import numpy as np
import random
grid = np.array([
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
class Worker():
def __init__(self, name):
while True:
x = random.randint(0,23)
y = random.randint(0,22)
if grid[x,y] == 0:
self.row = x
self.col = y
break
self.name = name
def move(self, workers):
while True:
self.row += random.randint(-1,1)
self.col += random.randint(-1,1)
if self.row >= 26:
self.row = 25
elif self.row<0:
self.row=0
if self.col >= 24:
self.col = 23
elif self.col<0:
self.col=0
if grid[self.row,self.col] == 0:
self.row = self.row
self.col = self.col
break
def room_escape(self):
room_door = [(5,16),(13,16),(21,16),(5,8),(13,8),(21,8)]
if (self.col,self.row)==(5,16) or (self.col,self.row)==(13,16) or (self.col,self.row)==(21,16) or (self.col,self.row)==(5,8) or (self.col,self.row)==(13,8) or (self.col,self.row)==(21,8):
distances2 = []
building_door = [(0,12),(26,12)]
for i in range(len(building_door)):
distances2.append(sqrt((building_door[i][0]-self.col)**2+(building_door[i][1]-self.row)**2))
m = distances2.index(min(distances2))
if self.row < building_door[m][1]:
self.row += 1
elif self.row > building_door[m][1]:
self.row -= 1
if self.col < building_door[m][0]:
self.col += 1
elif self.col > building_door[m][0]:
self.col -= 1
elif self.row <= 8 or self.row >= 16:
distances = []
for i in range(len(room_door)):
distances.append(sqrt((room_door[i][0]-self.col)**2+(room_door[i][1]-self.row)**2))
n = distances.index(min(distances))
if self.row < room_door[n][1]:
self.row += 1
elif self.row > room_door[n][1]:
self.row -= 1
if self.col < room_door[n][0]:
self.col += 1
elif self.col > room_door[n][0]:
self.col -= 1
elif self.row >= 8 or self.row <= 16:
distances2 = []
building_door = [(0,12),(26,12)]
for i in range(len(building_door)):
distances2.append(sqrt((building_door[i][0]-self.col)**2+(building_door[i][1]-self.row)**2))
m = distances2.index(min(distances2))
if self.row < building_door[m][1]:
self.row += 1
elif self.row > building_door[m][1]:
self.row -= 1
if self.col < building_door[m][0]:
self.col += 1
elif self.col > building_door[m][0]:
self.col -= 1
from numpy import sqrt
plt.pcolormesh(grid)
#plt.scatter(2,3,c="red")
MAXROWS = 24
MAXCOLS = 26
def plot_worker_scatter(workers):
xlist = []
ylist = []
for k in range(len(workers)):
xlist.append(workers[k].col + 0.5)
ylist.append(workers[k].row + 0.5)
plt.scatter(xlist,ylist,c="red")
def main():
workerNames = ["1", "2","3","4","5","6","7","8","9","10"]
numWorkers = 10
workerList = []
for i in range(numWorkers):
workerList.append(Worker(workerNames[i]))
for t in range(1):
for i in range(numWorkers):
workerList[i].move(workerList)
print("Just a normal day")
plt.pcolormesh(grid)
plot_worker_scatter(workerList)
plt.scatter(0+0.5,12+0.5,c="blue")
plt.title("Normal Activity")
plt.xlabel("Columns")
plt.ylabel("Rows")
plt.xlim(0,MAXCOLS)
plt.ylim(0,MAXROWS)
plt.show()
print(grid[0,10])
for t in range(1):
for i in range(numWorkers):
workerList[i].room_escape()
print("Run to Emergency exit! You have", 10 - t, "seconds")
plt.pcolormesh(grid)
plot_worker_scatter(workerList)
plt.title("Building under Fire")
plt.xlabel("Columns")
plt.ylabel("Rows")
plt.xlim(0,MAXCOLS)
plt.ylim(0,MAXROWS)
plt.show()
if __name__ == "__main__":
main()
This is a simulation of a fire evacuation where the workers run towards the excits when the building is under fire. They have to move around barriers if walls ti reach the exits. The problem is whenever these points take corners they overlap the walls eventhough I have cleartly stated a condition to stop that. I can't figure out why. The condition is if the coordinates of the point in the grid equals 1 the loop is excecuted again. Can someone please help.
There is also an error that comes from time to time when I run the code. It says: IndexError: index 24 is out of bounds for axis 0 with size 24
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

![[1]: https://i.stack.imgur.com/g6m3j.png](https://i.stack.imgur.com/h7GTI.png)