class Coord:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def copy(self, coord):
self.x = coord.x
self.y = coord.y
return self
def int(self):
self.x = int(self.x)
self.y = int(self.y)
def transpose(self):
[self.x, self.y] = [self.y, self.x]
return self
def max(self):
return max (self.x, self.y)
def symmetry(self, axis):
if axis == 1:
self.x = -self.x
if axis == 2:
self.y = -self.y
return self
def lowestWith(self, other):
minx = min (self.x, other.x)
miny = min (self.y, other.y)
return Coord(minx, miny)
def biggestWith(self, other):
maxx = max (self.x, other.x)
maxy = max (self.y, other.y)
return Coord(maxx, maxy)
def __eq__(self, coord):
return ( (self.x == coord.x) & (self.y == coord.y) )
def __add__(self, coord):
return Coord(self.x + coord.x, self.y + coord.y)
def __iadd__(self, coord):
self.x += coord.x
self.y += coord.y
return self
def __sub__(self, coord):
return Coord(self.x - coord.x, self.y - coord.y)
def __isub__(self, coord):
self.x -= coord.x
self.y -= coord.y
return self
def __mul__(self, _lambda):
if type(_lambda)!=type(self):
return Coord(self.x*_lambda, self.y*_lambda)
else:
raise TypeError, ArgumentError
def __rmul__(self, _lambda):
return self.__mul__(_lambda)
def __div__(self, _lambda):
if type(_lambda)!=type(self):
return Coord(self.x/_lambda, self.y/_lambda)
else:
raise TypeError, ArgumentError
def __rdiv__(self, _lambda):
return self.__div__(_lambda)
def __repr__(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"
def clone(coord):
return Coord().copy(coord)
clone = staticmethod(clone)
class Block:
# blockid = 0
def __init__(self, coord=Coord(), type="noType"):
self.coord = Coord.clone(coord) #Must be this way or risk program death !
self.type = type
# Block.blockid += 1
# self.id = Block.blockid
def copy(self, other):
self.coord.copy(other.coord)
self.type = other.type
return self
def clone(other):
return Block().copy(other)
def isEmpty(self):
return self == Block()
def x(self):
return self.coord.x
def y(self):
return self.coord.y
def moveOf(self, vector):
self.coord += vector
# def getId(self):
# return self.id
def __eq__(self, other):
return (self.type == other.type) & (self.coord == other.coord)
def __repr__(self):
return "(" + str(self.coord) + ", " + str(self.type) + ")"#", " + str(self.id) + ")"
clone = staticmethod(clone)
class BlockList:
def __init__(self, blockList=[]):
self.blockList = []
if len(blockList) > 0:
for block in blockList:
self.append(Block.clone(block))
def __iter__(self):
return self.blockList.__iter__()
def copy(self, other):
self.blockList = []
for block in other.blockList:
self.blockList.append(Block.clone(block))
def clone(other):
result = BlockList().copy(other)
def isEmpty(self):
return len(self.blockList) == 0
def moveOf(self, vector):
for block in self.blockList:
block.moveOf(vector)
def append(self, block):
self.blockList.append(block)
def __add__(self, other):
# result = BlockList()
# if other.isEmpty():
# if not self.isEmpty():
# result = self.copy(self)
# else:
# for block in self:
# result.append(Block.clone(block))
# for block in other:
# result.append(Block.clone(block))
# return result
result = BlockList()
if other.isEmpty():
if not self.isEmpty():
result = self
else:
for block in self:
result.append(block)
for block in other:
result.append(block)
return result
# def __sub__(self, other):
# difference =
# return
def remove(self, block):
if block in self.blockList:
self.blockList.remove(block)
def intersects(self, other):
if other.isEmpty() | self.isEmpty():
return False
for block1 in self.blockList:
for block2 in other:
if block1.coord == block2.coord:
return True
return False
def gravityCenter(self):
result = Coord(0, 0)
n = len(self.blockList)
for block in self.blockList:
result += block.coord
result = result/n
return result
def getLowerLeftCoord(self):
tempcoord = Coord.clone(self.blockList[0].coord)
for block in self.blockList:
tempcoord = tempcoord.lowestWith(block.coord)
return tempcoord
def getSurroundingBox(self):
tempcoord1 = Coord.clone(self.blockList[0].coord)
tempcoord2 = Coord.clone(tempcoord1)
for block in self.blockList:
tempcoord1 = tempcoord1.lowestWith(block.coord)
tempcoord2 = tempcoord2.biggestWith(block.coord)
return [tempcoord1, tempcoord2]
def __repr__(self):
return str(self.blockList)
clone = staticmethod(clone)