From fc0178288e34051a0621e60059fd25dd150bc3aa Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Mon, 29 Nov 2021 16:40:01 +0100 Subject: [PATCH] Coordinate.generate(): get list of coordinates from x1,y1 to x2,y2 Grid(): expose boundaries Grid.isWithinBoundaries(): check if Coordinate is within boundaries Grid.getActiveCells(): get all Coordinates within Grid() with a value --- coordinate.py | 8 +++++++- grid.py | 42 ++++++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/coordinate.py b/coordinate.py index a5bf583..34140b5 100644 --- a/coordinate.py +++ b/coordinate.py @@ -1,7 +1,7 @@ from __future__ import annotations from dataclasses import dataclass from math import sqrt, inf -from typing import Union +from typing import Union, List @dataclass(frozen=True, order=True) @@ -60,3 +60,9 @@ class Coordinate: neighbourList.append(Coordinate(tx, ty)) return neighbourList + + @staticmethod + def generate(from_x: int, to_x: int, from_y: int, to_y: int) -> List[Coordinate]: + return [Coordinate(x, y) for x in range(from_x, to_x + 1) for y in range(from_y, to_y + 1)] + + diff --git a/grid.py b/grid.py index e217e76..7e9820c 100644 --- a/grid.py +++ b/grid.py @@ -11,23 +11,23 @@ class Grid: def __init__(self, default=False): self.__default = default self.__grid = {} - self.__minX = 0 - self.__minY = 0 - self.__maxX = 0 - self.__maxY = 0 + self.minX = 0 + self.minY = 0 + self.maxX = 0 + self.maxY = 0 def __trackBoundaries(self, pos: Coordinate): - if pos.x < self.__minX: - self.__minX = pos.x + if pos.x < self.minX: + self.minX = pos.x - if pos.y < self.__minY: - self.__minY = pos.y + if pos.y < self.minY: + self.minY = pos.y - if pos.x > self.__maxX: - self.__maxX = pos.x + if pos.x > self.maxX: + self.maxX = pos.x - if pos.y > self.__maxY: - self.__maxY = pos.y + if pos.y > self.maxY: + self.maxY = pos.y def toggle(self, pos: Coordinate): if pos in self.__grid: @@ -63,12 +63,15 @@ class Grid: def isCorner(self, pos: Coordinate) -> bool: return pos in [ - Coordinate(self.__minX, self.__minY), - Coordinate(self.__minX, self.__maxY), - Coordinate(self.__maxX, self.__minY), - Coordinate(self.__maxX, self.__maxY), + Coordinate(self.minX, self.minY), + Coordinate(self.minX, self.maxY), + Coordinate(self.maxX, self.minY), + Coordinate(self.maxX, self.maxY), ] + def isWithinBoundaries(self, pos: Coordinate) -> bool: + return self.minX <= pos.x <= self.maxX and self.minY <= pos.y <= self.maxY + def add(self, pos: Coordinate, value: Union[float, int] = 1): if pos in self.__grid: self.__grid[pos] += value @@ -83,6 +86,9 @@ class Grid: self.__trackBoundaries(pos) self.__grid[pos] = self.__default - value + def getActiveCells(self): + return [i for i in self.__grid.keys()] + def getSum(self, includeNegative: bool = True): grid_sum = 0 for value in self.__grid.values(): @@ -96,8 +102,8 @@ class Grid: neighbour_sum = 0 for neighbour in pos.getNeighbours( includeDiagonal=includeDiagonal, - minX=self.__minX, minY=self.__minY, - maxX=self.__maxX, maxY=self.__maxY): + minX=self.minX, minY=self.minY, + maxX=self.maxX, maxY=self.maxY): if neighbour in self.__grid: if includeNegative or self.__grid[neighbour] > 0: neighbour_sum += self.__grid[neighbour]