set shaped areas in a grid

This commit is contained in:
Stefan Harmuth 2022-08-10 23:34:58 +02:00
parent f5d59cd74f
commit f83709758c

View File

@ -1,5 +1,5 @@
from __future__ import annotations from __future__ import annotations
from .coordinate import Coordinate, DistanceAlgorithm from .coordinate import Coordinate, DistanceAlgorithm, Shape
from .types import Numeric from .types import Numeric
from enum import Enum from enum import Enum
from heapq import heappop, heappush from heapq import heappop, heappush
@ -111,6 +111,17 @@ class Grid:
def div(self, pos: Coordinate, value: Numeric = 1) -> Numeric: def div(self, pos: Coordinate, value: Numeric = 1) -> Numeric:
return self.set(pos, self.get(pos) / value) return self.set(pos, self.get(pos) / value)
def add_shape(self, shape: Shape, value: Numeric = 1) -> None:
for x in range(shape.top_left.x, shape.bottom_right.x + 1):
for y in range(shape.top_left.y, shape.bottom_right.y + 1):
if not shape.mode_3d:
pos = Coordinate(x, y)
self.set(pos, self.get(pos) + value)
else:
for z in range(shape.top_left.z, shape.bottom_right.z + 1):
pos = Coordinate(x, y, z)
self.set(pos, self.get(pos) + value)
def get(self, pos: Coordinate) -> Any: def get(self, pos: Coordinate) -> Any:
return self.__grid.get(pos, self.__default) return self.__grid.get(pos, self.__default)
@ -158,6 +169,9 @@ class Grid:
else: else:
return list(self.__grid.keys()) return list(self.__grid.keys())
def values(self):
return self.__grid.values()
def getSum(self, includeNegative: bool = True) -> Numeric: def getSum(self, includeNegative: bool = True) -> Numeric:
if not self.mode3D: if not self.mode3D:
return sum( return sum(