[go: up one dir, main page]

Menu

[r3]: / trunk / Model.py  Maximize  Restore  History

Download this file

218 lines (169 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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)