allow getActiveCells() to return only one row/column

annotation/import fixed
This commit is contained in:
Stefan Harmuth 2021-12-13 11:10:38 +01:00
parent e16c21b3bb
commit a507b004f9

View File

@ -2,8 +2,7 @@ from __future__ import annotations
from .coordinate import Coordinate, DistanceAlgorithm
from dataclasses import dataclass
from enum import Enum
from math import inf
from typing import Any, List, Union
from typing import Any, Dict, List, Optional, Union
OFF = False
ON = True
@ -121,8 +120,13 @@ class Grid:
else:
return self.minX <= pos.x <= self.maxX and self.minY <= pos.y <= self.maxY
def getActiveCells(self) -> List[Coordinate]:
return list(self.__grid.keys())
def getActiveCells(self, x: int = None, y: int = None) -> List[Coordinate]:
if x:
return [c for c in self.__grid.keys() if c.x == x]
elif y:
return [c for c in self.__grid.keys() if c.y == y]
else:
return list(self.__grid.keys())
def getSum(self, includeNegative: bool = True) -> Numeric:
grid_sum = 0
@ -199,18 +203,18 @@ class Grid:
self.transform(GridTransformation.ROTATE_RIGHT)
def getPath(self, pos_from: Coordinate, pos_to: Coordinate, includeDiagonal: bool, walls: List[Any] = None)\
-> List[Coordinate]:
-> Union[None, List[Coordinate]]:
@dataclass(frozen=True)
class Node:
f_cost: int
h_cost: int
parent: Coordinate
parent: Optional[Coordinate]
if walls in None:
walls = [self.__default]
openNodes: Dict[Coordinate, Node] = {}
closedNodes: Dict[Coordinate, Node] = {} # Dict[Coordinate, Node]
closedNodes: Dict[Coordinate, Node] = {}
openNodes[pos_from] = Node(
pos_from.getDistanceTo(pos_to, DistanceAlgorithm.MANHATTAN, includeDiagonal),