blob: a779c74e42506e0b8dc4adad2f11494cc2ea29f9 (
plain) (
blame)
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
|
#include <stdio.h>
typedef struct { int top; int left; int width; int height; } rect;
typedef struct node { struct node *left; struct node *right; rect rc; int id;
} node;
node *insert(node *n, rect r, node **alloc) {
if (!n->id) {
if (n->rc.width - r.width < 0 || n->rc.height - r.height < 0) return 0;
n->left = (*alloc)++;
n->right = (*alloc)++;
n->left->rc.top = n->rc.top;
n->left->rc.left = n->rc.left + r.width;
n->left->rc.width = n->rc.width - r.width;
n->left->rc.height = r.height;
n->right->rc.top = n->rc.top + r.height;
n->right->rc.left = n->rc.left;
n->right->rc.width = n->rc.width;
n->right->rc.height = n->rc.height - r.height;
return n;
} else {
node *new_node = insert(n->left, r, alloc);
if (new_node) return new_node;
else return insert(n->right, r, alloc);
}
}
void print_tree(node *n) {
if (!n->id) return;
printf("%i: (%i, %i)\n", n->id, n->rc.left, n->rc.top);
if (n->left) print_tree(n->left);
if (n->right) print_tree(n->right);
}
int main() {
node alloc[100] = {0};
node *np = &alloc[1];
alloc[0].rc.top = 0;
alloc[0].rc.left = 0;
alloc[0].rc.width = 1000;
alloc[0].rc.height = 1000;
rect r;
r.width = 201;
r.height = 100;
for (int i = 0; i < 21; i++) {
insert(&alloc[0], r, &np)->id = i+1;
printf("\n");
print_tree(&alloc[0]);
}
}
|