import numpy as np
from matplotlib import pyplot as plt

from m4opt.utils.optimization import pack_boxes

rng = np.random.RandomState(seed=42)
for n in range(1, 5):
    wh = rng.uniform(size=(n, 2))
    xy, (total_width, total_height) = pack_boxes(wh)
    fig, ax = plt.subplots(subplot_kw=dict(aspect=1))
    ax.set_xlim(0, total_width)
    ax.set_ylim(0, total_height)
    for xy_, wh_ in zip(xy, wh):
        ax.add_patch(plt.Rectangle(xy_, *wh_, edgecolor="black"))