diff options
| author | Tom Harley | 2019-07-19 00:07:52 +0100 |
|---|---|---|
| committer | Tom Harley | 2019-07-19 00:07:52 +0100 |
| commit | 4e4b0bfa9e237d2ca8b6889a7979c3d141daae5d (patch) | |
| tree | fa88601dc841b59e2bd99f83d971a7c3656613f2 | |
| download | binpacker-4e4b0bfa9e237d2ca8b6889a7979c3d141daae5d.tar.gz binpacker-4e4b0bfa9e237d2ca8b6889a7979c3d141daae5d.zip | |
Initial commit
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | main.c | 51 |
3 files changed, 55 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +main diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ada7ff3 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +default: all + +all: main @@ -0,0 +1,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]); + } +} |
