grid transformations
This commit is contained in:
parent
37211190dd
commit
0c0a9a7eb1
59
grid.py
59
grid.py
@ -1,12 +1,23 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from coordinate import Coordinate
|
from coordinate import Coordinate
|
||||||
from typing import Union
|
from enum import Enum
|
||||||
|
from typing import Union, Any
|
||||||
|
|
||||||
OFF_STATES = [False, 0, None]
|
OFF_STATES = [False, 0, None]
|
||||||
OFF = False
|
OFF = False
|
||||||
ON = True
|
ON = True
|
||||||
|
|
||||||
|
|
||||||
|
class GridTransformation(Enum):
|
||||||
|
FLIP_HORIZONTALLY = 1
|
||||||
|
FLIP_VERTICALLY = 2
|
||||||
|
FLIP_DIAGONALLY = 3
|
||||||
|
FLIP_DIAGONALLY_REV = 4
|
||||||
|
ROTATE_RIGHT = 5
|
||||||
|
ROTATE_LEFT = 6
|
||||||
|
ROTATE_TWICE = 7
|
||||||
|
|
||||||
|
|
||||||
class Grid:
|
class Grid:
|
||||||
def __init__(self, default=False):
|
def __init__(self, default=False):
|
||||||
self.__default = default
|
self.__default = default
|
||||||
@ -36,13 +47,19 @@ class Grid:
|
|||||||
self.__trackBoundaries(pos)
|
self.__trackBoundaries(pos)
|
||||||
self.__grid[pos] = not self.__default
|
self.__grid[pos] = not self.__default
|
||||||
|
|
||||||
def set(self, pos: Coordinate, value: any):
|
def set(self, pos: Coordinate, value: Any):
|
||||||
if value in OFF_STATES and pos in self.__grid:
|
if value in OFF_STATES and pos in self.__grid:
|
||||||
del self.__grid[pos]
|
del self.__grid[pos]
|
||||||
elif value not in OFF_STATES:
|
elif value not in OFF_STATES:
|
||||||
self.__trackBoundaries(pos)
|
self.__trackBoundaries(pos)
|
||||||
self.__grid[pos] = value
|
self.__grid[pos] = value
|
||||||
|
|
||||||
|
def get(self, pos: Coordinate) -> Any:
|
||||||
|
if pos in self.__grid:
|
||||||
|
return self.__grid[pos]
|
||||||
|
else:
|
||||||
|
return self.__default
|
||||||
|
|
||||||
def getState(self, pos: Coordinate) -> bool:
|
def getState(self, pos: Coordinate) -> bool:
|
||||||
if pos not in self.__grid:
|
if pos not in self.__grid:
|
||||||
return False
|
return False
|
||||||
@ -109,3 +126,41 @@ class Grid:
|
|||||||
neighbour_sum += self.__grid[neighbour]
|
neighbour_sum += self.__grid[neighbour]
|
||||||
|
|
||||||
return neighbour_sum
|
return neighbour_sum
|
||||||
|
|
||||||
|
def flip(self, c1: Coordinate, c2: Coordinate):
|
||||||
|
buf = self.get(c1)
|
||||||
|
self.set(c1, self.get(c2))
|
||||||
|
self.set(c2, buf)
|
||||||
|
|
||||||
|
def transform(self, mode: GridTransformation):
|
||||||
|
if mode == GridTransformation.FLIP_HORIZONTALLY:
|
||||||
|
for x in range(self.minX, (self.maxX - self.minX) // 2 + 1):
|
||||||
|
for y in range(self.minY, self.maxY + 1):
|
||||||
|
self.flip(Coordinate(x, y), Coordinate(self.maxX - x, y))
|
||||||
|
elif mode == GridTransformation.FLIP_VERTICALLY:
|
||||||
|
for y in range(self.minY, (self.maxY - self.minY) // 2 + 1):
|
||||||
|
for x in range(self.minX, self.maxX + 1):
|
||||||
|
self.flip(Coordinate(x, y), Coordinate(x, self.maxY - y))
|
||||||
|
elif mode == GridTransformation.FLIP_DIAGONALLY:
|
||||||
|
self.transform(GridTransformation.ROTATE_LEFT)
|
||||||
|
self.transform(GridTransformation.FLIP_HORIZONTALLY)
|
||||||
|
elif mode == GridTransformation.FLIP_DIAGONALLY_REV:
|
||||||
|
self.transform(GridTransformation.ROTATE_RIGHT)
|
||||||
|
self.transform(GridTransformation.FLIP_HORIZONTALLY)
|
||||||
|
elif mode == GridTransformation.ROTATE_LEFT:
|
||||||
|
newGrid = Grid()
|
||||||
|
for x in range(self.maxX, self.minX - 1, -1):
|
||||||
|
for y in range(self.minY, self.maxY + 1):
|
||||||
|
newGrid.set(Coordinate(y, self.maxX - x), self.get(Coordinate(x, y)))
|
||||||
|
|
||||||
|
self.__dict__.update(newGrid.__dict__)
|
||||||
|
elif mode == GridTransformation.ROTATE_RIGHT:
|
||||||
|
newGrid = Grid()
|
||||||
|
for x in range(self.minX, self.maxX + 1):
|
||||||
|
for y in range(self.maxY, self.minY - 1, -1):
|
||||||
|
newGrid.set(Coordinate(self.maxY - y, x), self.get(Coordinate(x, y)))
|
||||||
|
|
||||||
|
self.__dict__.update(newGrid.__dict__)
|
||||||
|
elif mode == GridTransformation.ROTATE_TWICE:
|
||||||
|
self.transform(GridTransformation.ROTATE_RIGHT)
|
||||||
|
self.transform(GridTransformation.ROTATE_RIGHT)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user