-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrectangle_sheet.go
More file actions
212 lines (157 loc) · 5.31 KB
/
rectangle_sheet.go
File metadata and controls
212 lines (157 loc) · 5.31 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
package gosprite
import(
"time"
"fmt"
)
type MyError struct {
When time.Time
What string
}
func (e MyError) Error() string {
return fmt.Sprintf("%v: %v", e.When, e.What)
}
type RectangleSheet struct{
width int
height int
data TwoDimensional
}
func (r *RectangleSheet) init(width, height int){
r.width = width
r.height = height
r.data = TwoDimensional{}
r.data.init(width, height)
}
func (r *RectangleSheet) canAddRectangleAt(x, y, width, height int) (requiredHorisontalCount, requiredVerticalCount, leftOverWidth, leftOverHeight int, err error){
requiredHorisontalCount = 0;
requiredVerticalCount = 0;
leftOverWidth = 0;
leftOverHeight = 0;
foundWidth := 0;
foundHeight := 0;
trialX := x;
trialY := y;
err = nil
// Check all cells that need to be unoccupied for there to be room for the rectangle.
//console.log("Occupied " + this.data.item(trialX, trialY).occupied + " at " + trialX + " " + trialY);
for foundHeight < height {
trialX = x;
foundWidth = 0;
for foundWidth < width {
if r.data.item(trialX, trialY) == true {
err = MyError{
time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
"Cannot place Rectangle",
}
return ;
}
foundWidth += r.data.colWidth(trialX);
trialX++;
}
foundHeight += r.data.rowHeight(trialY);
trialY++;
}
// Visited all cells that we'll need to place the rectangle,
// and none were occupied. So the space is available here.
requiredHorisontalCount = trialX - x;
requiredVerticalCount = trialY - y;
leftOverWidth = (foundWidth - width);
leftOverHeight = (foundHeight - height);
return
};
func (r *RectangleSheet) addRectangle(height, width int) (offsetX, offsetY int) {
requiredHeight := height;
requiredWidth := width;
x := 0;
y := 0;
offsetX = 0;
offsetY = 0;
rowCount := r.data.rowCount;
for {
// First move upwards until we find an unoccupied cell.
// If we're already at an unoccupied cell, no need to do anything.
// Important to clear all occupied cells to get
// the lowest free height deficit. This must be taken from the top of the highest
// occupied cell.
for (y < rowCount) && (r.data.item(x, y) == true) {
offsetY += r.data.rowHeight(y)
y += 1;
}
// If we found an unoccupied cell, than see if we can place a rectangle there.
// If not, than y popped out of the top of the canvas.
if ((y < rowCount) && (r.freeHeightDeficit(r.height, offsetY, requiredHeight) <= 0)) {
requiredHorisontalCount, requiredVerticalCount, leftOverWidth, leftOverHeight, err := r.canAddRectangleAt(x, y, requiredWidth, requiredHeight);
if err == nil {
r.placeRectangle(x, y, requiredWidth,requiredHeight,
requiredHorisontalCount, requiredVerticalCount,
leftOverWidth, leftOverHeight);
return;
}
// Go to the next cell
offsetY += r.data.rowHeight(y)
y += 1;
}
// If we've come so close to the top of the canvas that there is no space for the
// rectangle, go to the next column. r automatically also checks whether we've popped out of the top
// of the canvas (in that case, _canvasHeight == offsetY).
var freeHeightDeficit = r.freeHeightDeficit(r.height, offsetY, requiredHeight);
if freeHeightDeficit > 0 {
offsetY = 0;
y = 0;
offsetX += r.data.colWidth(x);
x += 1;
}
// If we've come so close to the right edge of the canvas that there is no space for
// the rectangle, return false now.
if (r.width - offsetX) < requiredWidth {
return -1, -1;
}
}
return
}
func (r *RectangleSheet) freeHeightDeficit(canvasHeight, offsetY, requiredHeight int) int {
spaceLeftVertically := canvasHeight - offsetY
freeHeightDeficit := requiredHeight - spaceLeftVertically
return freeHeightDeficit
}
func (r *RectangleSheet) placeRectangle(x, y, height, width, requiredHorisontalCount, requiredVerticalCount, leftOverWidth, leftOverHeight int) {
if leftOverWidth > 0 {
xFarRightColumn := x + requiredHorisontalCount - 1
r.data.insertColumn(xFarRightColumn, leftOverWidth)
}
if leftOverHeight > 0 {
yFarBottomColumn := y + requiredVerticalCount - 1
r.data.insertRow(yFarBottomColumn, leftOverHeight)
}
for i := x + requiredHorisontalCount - 1; i >= x; i-- {
for j := y + requiredVerticalCount - 1; j >= y; j-- {
r.data.setItem(i, j, true)
}
}
}
func (r *RectangleSheet) calculate(images []*Image) {
totalWidth := 0
maxHeight := images[0].height + (images[0].height/2)
for _, v := range images {
totalWidth += v.width
}
r.init(totalWidth, maxHeight)
success_count := 0;
for _, image := range images{
offsetX, offsetY := r.addRectangle(image.height, image.width);
if offsetX >= 0 && offsetY >= 0 {
image.x = offsetX;
image.y = offsetY;
success_count++;
}
}
last_col_occupied := false;
for y := 0; y < r.data.rowCount; y++ {
if r.data.item(r.data.colCount - 1, y) == true{
last_col_occupied = true;
}
}
if last_col_occupied == false{
r.width -= r.data.colWidth(r.data.colCount - 1);
}
return
};