list comp is faster than building lists yourself
This commit is contained in:
parent
3590c2550c
commit
1e1f5ef126
@ -80,11 +80,11 @@ class Coordinate:
|
|||||||
else:
|
else:
|
||||||
nb_list = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
nb_list = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
||||||
|
|
||||||
for dx, dy in nb_list:
|
return [
|
||||||
tx = self.x + dx
|
Coordinate(self.x + dx, self.y + dy)
|
||||||
ty = self.y + dy
|
for dx, dy in nb_list
|
||||||
if minX <= tx <= maxX and minY <= ty <= maxY:
|
if minX <= self.x + dx <= maxX and minY <= self.y + dy <= maxY
|
||||||
neighbourList.append(Coordinate(tx, ty))
|
]
|
||||||
else:
|
else:
|
||||||
if includeDiagonal:
|
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]]
|
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:
|
else:
|
||||||
nb_list = [(-1, 0, 0), (0, -1, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, -1)]
|
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:
|
return [
|
||||||
tx = self.x + dx
|
Coordinate(self.x + dx, self.y + dy, self.z + dz)
|
||||||
ty = self.y + dy
|
for dx, dy, dz in nb_list
|
||||||
tz = self.z + dz
|
if minX <= self.x + dx <= maxX and minY <= self.y + dy <= maxY and minZ <= self.z + dz <= maxZ
|
||||||
if minX <= tx <= maxX and minY <= ty <= maxY and minZ <= tz <= maxZ:
|
]
|
||||||
neighbourList.append(Coordinate(tx, ty, tz))
|
|
||||||
|
|
||||||
return neighbourList
|
|
||||||
|
|
||||||
def getAngleTo(self, target: Coordinate, normalized: bool = False) -> float:
|
def getAngleTo(self, target: Coordinate, normalized: bool = False) -> float:
|
||||||
"""normalized returns an angle going clockwise with 0 starting in the 'north'"""
|
"""normalized returns an angle going clockwise with 0 starting in the 'north'"""
|
||||||
|
|||||||
@ -259,7 +259,7 @@ class Grid:
|
|||||||
if currentCoord == pos_to:
|
if currentCoord == pos_to:
|
||||||
break
|
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:
|
if self.get(neighbour) in walls or neighbour in closedNodes:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user