/
moldpieces.py
207 lines (182 loc) · 5.11 KB
/
moldpieces.py
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
from tkinter import *
import math
class Rectangle:
def __init__(self, width, height, color):
self.width = width
self.height = height
self.color = color
def color(self, color):
self.color = color
def getBox(self):
return Box(0, self.width, 0, self.height)
class Line:
def __init__(self, length, degrees):
self.length = length
self.degrees = degrees
def getEndPoint(self):
x = self.length * math.cos(math.radians(self.degrees))
y = self.length * math.sin(math.radians(self.degrees))
return Point(x,y)
def getBox(self):
endPoint = self.getEndPoint()
if (endPoint.x >= 0 and endPoint.y >= 0):
return Box(0, 0, endPoint.x, endPoint.y)
if (endPoint.x >= 0 and endPoint.y <= 0):
return Box(0, endPoint.y, endPoint.x, 0)
if (endPoint.x <= 0 and endPoint.y < 0):
return Box(endPoint.x, endPoint.y, 0, 0)
if (endPoint.x <= 0 and endPoint.y >= 0):
return Box(endPoint.x, 0, 0, endPoint.y)
class Hollow:
def __init_(self, width, height, image):
self.width = width;
self.height = height;
class Carve():
def __init__(self, point, piece):
self.point = point
self.piece = piece
self.color = 'white'
def color(self, color):
self.color = color
class MoldableLine:
def __init__(self, length, degrees):
self.line = Line(length, degrees)
self.length = length
self.degrees = degrees
self.endM = None
def end(self, mLine):
self.endM = mLine
def weld(self):
if (self.line == None):
return Mold([])
pos = Point(0,0)
mTotal = Mold([Piece(pos, self.line, "")])
if (self.endM):
eMold, point = self.endM.weld()
mTotal = mTotal.combine(eMold, self.getEndPoint(), point)
return (mTotal, pos)
def getEndPoint(self):
x = self.length * math.cos(math.radians(self.degrees))
y = self.length * math.sin(math.radians(self.degrees))
return Point(x,y)
class MoldableRectangle:
def __init__(self, width, height, color, name):
self.rect = Rectangle(width, height, color)
self.color = color
self.name = name
self.lEnd = None
self.rEnd = None
self.tEnd = None
self.bEnd = None
def left(self, mRect):
self.lEnd = mRect
return mRect
def right(self, mRect):
self.rEnd = mRect
return mRect
def top(self, mRect):
self.tEnd = mRect
return mRect
def bot(self, mRect):
self.bEnd = mRect;
return mRect
def asBox(self):
return
def weld(self):
if (self.rect == None):
return Mold([])
pos = Point(0,0)
mTotal = Mold([Piece(pos, self.rect, self.name)])
if (self.lEnd):
lMold, lPos = self.lEnd.weld()
newPos = Point(lPos.x + self.lEnd.rect.width, lPos.y)
mTotal = lMold.combine(mTotal, pos, newPos);
pos = newPos
if (self.tEnd):
tMold, tPos = self.tEnd.weld()
newPos = Point(tPos.x, tPos.y + self.tEnd.rect.height)
mTotal = tMold.combine(mTotal, pos, newPos);
pos = newPos
if (self.rEnd):
rMold, rPos = self.rEnd.weld()
mTotal = mTotal.combine(rMold, rPos, Point(pos.x + self.rect.width, pos.y));
if (self.bEnd):
bMold, bPos = self.bEnd.weld()
mTotal = mTotal.combine(bMold, bPos, Point(pos.x, pos.y + self.rect.height))
return (mTotal, pos)
class Piece:
def __init__(self, point, obj, name):
self.pos = point;
self.name = name
self.obj = obj
def getBox(self):
box = self.obj.getBox()
box.topY += self.pos.y
box.botY + self.pos.y
box.topX += self.pos.x
box.botX + self.pos.x
return box
class Mold:
def __init__(self, pieces):
self.pieces = pieces
def getPiece(self, name):
for piece in self.pieces:
if piece.name == name:
return piece
def combine(self, mold, pos1, pos2):
xDiff = pos1.x - pos2.x
yDiff = pos1.y - pos2.y
for piece in mold.pieces:
piece.pos.x += xDiff
piece.pos.y += yDiff
return Mold(self.pieces + mold.pieces)
def getBox(self):
topY = None
topX = None
botY = None
botX = None
for piece in self.pieces:
pieceBox = piece.getBox()
if (topY is None or pieceBox.topY < topY):
topY = pieceBox.topY
if (topX is None or pieceBox.topX < topX):
topX = pieceBox.topX
if (botX is None or pieceBox.botX > botX):
botX = pieceBox.botX
if (botY is None or pieceBox.botY > botY):
botY = pieceBox.botY
return Box(topX, topY, botX, botY)
def Empty():
return Mold([])
def fitToOrigin(self):
box = self.getBox()
yOff = 0
xOff = 0
if (box.topY < 0):
yOff = box.topY
if (box.topX < 0):
xOff = box.topX
for piece in self.pieces:
piece.pos.x -= xOff
piece.pos.y -= yOff
class Box:
def __init__(self, topX, topY, botX, botY):
self.topX = topX
self.topY = topY
self.botX = botX
self.botY = botY
def combine(self, box2):
topY = self.topY if self.topY > box2.topY else bot2.topY
topX = self.topX if self.topX > box2.topX else bot2.topX
botY = self.botY if self.botY < box2.botY else bot2.botY
botX = self.botX if self.topY < box2.botX else bot2.botX
def pointInside(self, point):
return point.x > self.topX and point.x < botX and point.y < self.topY and point.y > self.botY
def contains(self, box):
return not (self.pointInside(Point(box.topX, box.topY)) or self.pointInside(Point(box.botX, box.botY)))
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, o):
return Point(self.x + o.x, self.y + o.y)