Coordinate.getNeighbours() and Grid.getNeighboursOf() can be generators; no need to create extra lists every time
This commit is contained in:
parent
5df82d2359
commit
00de38a277
@ -113,11 +113,9 @@ class Coordinate:
|
|||||||
else:
|
else:
|
||||||
nb_list = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
nb_list = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
||||||
|
|
||||||
return [
|
for dx, dy in nb_list:
|
||||||
Coordinate(self.x + dx, self.y + dy)
|
if minX <= self.x + dx <= maxX and minY <= self.y + dy <= maxY:
|
||||||
for dx, dy in nb_list
|
yield self.__class__(self.x + dx, self.y + dy)
|
||||||
if minX <= self.x + dx <= maxX and minY <= self.y + dy <= maxY
|
|
||||||
]
|
|
||||||
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]]
|
||||||
@ -125,11 +123,9 @@ 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)]
|
||||||
|
|
||||||
return [
|
for dx, dy, dz in nb_list:
|
||||||
Coordinate(self.x + dx, self.y + dy, self.z + dz)
|
if minX <= self.x + dx <= maxX and minY <= self.y + dy <= maxY and minZ <= self.z + dz <= maxZ:
|
||||||
for dx, dy, dz in nb_list
|
yield self.__class__(self.x + dx, self.y + dy, self.z + dz)
|
||||||
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:
|
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'"""
|
||||||
|
|||||||
@ -224,10 +224,9 @@ class Grid:
|
|||||||
minX=self.minX, minY=self.minY, minZ=self.minZ,
|
minX=self.minX, minY=self.minY, minZ=self.minZ,
|
||||||
maxX=self.maxX, maxY=self.maxY, maxZ=self.maxZ
|
maxX=self.maxX, maxY=self.maxY, maxZ=self.maxZ
|
||||||
)
|
)
|
||||||
if includeDefault:
|
for x in neighbours:
|
||||||
return neighbours
|
if includeDefault or x in self.__grid:
|
||||||
else:
|
yield x
|
||||||
return [x for x in neighbours if x in self.__grid]
|
|
||||||
|
|
||||||
def getNeighbourSum(self, pos: Coordinate, includeNegative: bool = True, includeDiagonal: bool = True) -> Numeric:
|
def getNeighbourSum(self, pos: Coordinate, includeNegative: bool = True, includeDiagonal: bool = True) -> Numeric:
|
||||||
neighbour_sum = 0
|
neighbour_sum = 0
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user