Created
April 22, 2013 19:39
-
-
Save miped/5437854 to your computer and use it in GitHub Desktop.
Simple packing algorithm for flat-ish things
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def side_by_side_simple(items): | |
if max_height(items) > MAX_HEIGHT or max_width(items) > MAX_WIDTH: | |
return False | |
if total_depth(items) <= MAX_DEPTH: | |
return True | |
pairs = permutations(items, 2) | |
stackable_pairs = [] | |
for a, b in pairs: | |
stackable = False | |
if a.height + b.height <= MAX_HEIGHT and max(a.width, b.width) <= MAX_WIDTH: | |
stackable = True | |
elif max(a.height, b.height) <= MAX_HEIGHT and a.width + b.width <= MAX_WIDTH: | |
stackable = True | |
elif max(a.height, b.height) <= MAX_WIDTH and a.width + b.width <= MAX_HEIGHT: | |
stackable = True | |
if stackable: | |
stackable_pairs.append((max(a.depth, b.depth), a, b)) | |
if stackable_pairs: | |
packed = [] | |
depth = 0 | |
for d, a, b in sorted(stackable_pairs): | |
if a in packed or b in packed: | |
break | |
depth += d | |
packed.append(a) | |
packed.append(b) | |
for i in items: | |
if i not in packed: | |
depth += i.depth | |
if depth <= MAX_DEPTH: | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment