Penlect / rectangle-packer

Rectangle packing program
MIT License
62 stars 16 forks source link

x, y switched when using max_width/max_height #13

Closed rbreu closed 3 years ago

rbreu commented 3 years ago

When you specify max_width and max_height and the values are small enough that the algorithm can't build it's preferred shape, the (x, y) values of the resulting positions are switched. If max_width and max_height are big enough so that the algo can build it's preferred shape, all is fine.

import rpack
sizes = [(2736, 3648), (2736, 3648), (3648, 2736), (2736, 3648), (2736, 3648)]

# Max width/height big enough that the algorithm can build its preferred shape:
max_wh = 70650
positions = rpack.pack(sizes, max_width=max_wh, max_height=max_wh)
print(positions)  # [(0, 0), (2736, 0), (10944, 0), (5472, 0), (8208, 0)]
print(rpack.overlapping(sizes, positions))  # None

# Max width/height smaller so that the algorithm need to deviate from its preferred shape:

max_wh = 14130
positions = rpack.pack(sizes, max_width=max_wh, max_height=max_wh)
print(positions)  # [(0, 0), (0, 2736), (3648, 0), (0, 5472), (0, 8208)]
print(rpack.overlapping(sizes, positions))  # (0, 1)
print(rpack.overlapping(sizes, [(p[1], p[0]) for p in positions]))  # None

max_wh = 8478
positions = rpack.pack(sizes, max_width=max_wh, max_height=max_wh)
print(positions)  # [(0, 0), (0, 2736), (3648, 2736), (0, 5472), (3648, 0)]
print(rpack.overlapping(sizes, positions))  # (0, 1)
print(rpack.overlapping(sizes, [(p[1], p[0]) for p in positions]))  # None

There's an easy workaround, if anyone needs this, just apply this to your resulting positions:

if rpack.overlapping(sizes, positions):
    positions = [(p[1], p[0]) for p in positions]

With this, all works like a charm.

Penlect commented 3 years ago

Thank you @rbreu for reporting this bug. I have released a bugfix release, 2.0.1, and deployed new wheels to PyPI.

rbreu commented 3 years ago

It seems to work now! Thank you very much. image