list comp is faster than building lists yourself

This commit is contained in:
Stefan Harmuth 2022-01-14 12:29:54 +01:00
parent 3590c2550c
commit 1e1f5ef126
2 changed files with 11 additions and 14 deletions

View File

@ -80,11 +80,11 @@ class Coordinate:
else:
nb_list = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for dx, dy in nb_list:
tx = self.x + dx
ty = self.y + dy
if minX <= tx <= maxX and minY <= ty <= maxY:
neighbourList.append(Coordinate(tx, ty))
return [
Coordinate(self.x + dx, self.y + dy)
for dx, dy in nb_list
if minX <= self.x + dx <= maxX and minY <= self.y + dy <= maxY
]
else:
if includeDiagonal:
nb_list = [(x, y, z) for x in [-1, 0, 1] for y in [-1, 0, 1] for z in [-1, 0, 1]]
@ -92,14 +92,11 @@ class Coordinate:
else:
nb_list = [(-1, 0, 0), (0, -1, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, -1)]
for dx, dy, dz in nb_list:
tx = self.x + dx
ty = self.y + dy
tz = self.z + dz
if minX <= tx <= maxX and minY <= ty <= maxY and minZ <= tz <= maxZ:
neighbourList.append(Coordinate(tx, ty, tz))
return neighbourList
return [
Coordinate(self.x + dx, self.y + dy, self.z + dz)
for dx, dy, dz in nb_list
if minX <= self.x + dx <= maxX and minY <= self.y + dy <= maxY and minZ <= self.z + dz <= maxZ
]
def getAngleTo(self, target: Coordinate, normalized: bool = False) -> float:
"""normalized returns an angle going clockwise with 0 starting in the 'north'"""

View File

@ -259,7 +259,7 @@ class Grid:
if currentCoord == pos_to:
break
for neighbour in self.getNeighboursOf(currentCoord, True, includeDiagonal):
for neighbour in self.getNeighboursOf(currentCoord, includeDefault=True, includeDiagonal=includeDiagonal):
if self.get(neighbour) in walls or neighbour in closedNodes:
continue