|
1 | | - |
| 1 | +/** |
| 2 | + * @typedef {Object} PotpackBox |
| 3 | + * @property {number} w Box width. |
| 4 | + * @property {number} h Box height. |
| 5 | + * @property {number} [x] X coordinate in the resulting container. |
| 6 | + * @property {number} [y] Y coordinate in the resulting container. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @typedef {Object} PotpackStats |
| 11 | + * @property {number} w Width of the resulting container. |
| 12 | + * @property {number} h Height of the resulting container. |
| 13 | + * @property {number} fill The space utilization value (0 to 1). Higher is better. |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * Packs 2D rectangles into a near-square container. |
| 18 | + * |
| 19 | + * Mutates the {@link boxes} array: it's sorted (by height/width), |
| 20 | + * and box objects are augmented with `x`, `y` coordinates. |
| 21 | + * |
| 22 | + * @param {PotpackBox[]} boxes |
| 23 | + * @return {PotpackStats} |
| 24 | + */ |
2 | 25 | export default function potpack(boxes) { |
3 | 26 |
|
4 | 27 | // calculate total box area and maximum box width |
@@ -46,7 +69,7 @@ export default function potpack(boxes) { |
46 | 69 | if (box.w === space.w && box.h === space.h) { |
47 | 70 | // space matches the box exactly; remove it |
48 | 71 | const last = spaces.pop(); |
49 | | - if (i < spaces.length) spaces[i] = last; |
| 72 | + if (last && i < spaces.length) spaces[i] = last; |
50 | 73 |
|
51 | 74 | } else if (box.h === space.h) { |
52 | 75 | // space matches the box height; update it accordingly |
|
0 commit comments