hlfw.ca

binpack

Download patch

ref: b6af1e9a2161df2ca656471983274ac632539095
parent: 314d055abecf2596c8f45e33c2ab4ebf193184da
author: Halfwit <michaelmisch1985@gmail.com>
date: Mon Nov 7 04:38:45 PST 2016

Some formatting - still does not work

--- a/binpack.c
+++ b/binpack.c
@@ -88,6 +88,7 @@
 {
 	struct input temp;
 
+	/* Cumulative area of all bins */
 	for (size_t i = 1; i < length; i++) {
 		for (size_t j = 0; j < length - i; j++) {
 			if ((r[j + 1].max_w * r[j + 1].min_h) > (r[j].max_w * r[j].min_h)) {
@@ -191,15 +192,15 @@
 
 		/* if binpack succeeds, we can grow --> set min as current, else set max as current */
 		if(pack_bin(length, out, mb, r) && r[i].max_w - r[i].min_w > 1) {
-			r[i].max_w = (r[i].min_w + r[i].max_w) / 2;
-		} else {
 			r[i].min_w = (r[i].min_w + r[i].max_w) / 2;
+		} else {
+			r[i].max_w = (r[i].min_w + r[i].max_w) / 2;
 		}
 
 		if(pack_bin(length, out, mb, r) && r[i].max_h - r[i].min_h > 1) {
-			r[i].max_h = (r[i].min_h + r[i].max_h) / 2;
-		} else{
 			r[i].min_h = (r[i].min_h + r[i].max_h) / 2;
+		} else {
+			r[i].max_h = (r[i].min_h + r[i].max_h) / 2;
 		}
 	}
 
@@ -218,23 +219,19 @@
 	while ((c = getopt(argc, argv, "hg:x:y:")) != -1) {
 		switch (c) {
 		/* read in, all vars unsigned int */
-		case 'g': {
+		case 'g':
 			sscanf(optarg, "%u", &mb.gaps);
 			break;
-		}
-		case 'x': {
+		case 'x':
 			sscanf(optarg, "%u", &mb.x);
 			break;
-		}
-		case 'y': {
+		case 'y':
 			sscanf(optarg, "%u", &mb.y);
 			break;
-		}
-		case 'h': {
+		case 'h':
 			fprintf(stderr, "Usage: %s -x screen_width -y screen_height -g gaps\n", argv[0]);
 			return EXIT_SUCCESS;
 		}
-		}
 	}
 
 	struct input r[100];
@@ -247,6 +244,7 @@
 		return EXIT_SUCCESS;
 	}
 
+	/* If bin area is more than available area, exit */
 	sort_input(r, length);
 
 	/* Initial pack to establish out bins */
@@ -253,6 +251,15 @@
 	pack_bin(length, out, mb, r);
 
 	while(resize(length, out, mb, r));
+
+	unsigned area = 0;
+	for (size_t i = 0; i < length; i++) {
+		area+=out[i].w * out[i].h;
+	}
+
+	/* If we have more windows than space */
+	if (area > (mb.x - mb.gaps) * (mb.y - mb.gaps))
+		return EXIT_SUCCESS;
 
 	center(length, out, mb);