Skip to content

Commit 8d4cbfd

Browse files
ntindleCopilot
andauthored
Update src/utils/gridfinityUtils.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Nicholas Tindle <nicktindle@outlook.com>
1 parent 7782b91 commit 8d4cbfd

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/utils/gridfinityUtils.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,20 @@ export const getColor = (type: LayoutItem['type'], index: number): string => {
300300
};
301301

302302
/**
303-
* Find all ways to divide a length into pieces no larger than maxSize
304-
* Returns array of piece sizes that sum to length
303+
* Finds all optimal ways to divide a given length into pieces, where each piece is no larger than `maxSize`
304+
* and no smaller than `minSize`. The function attempts to avoid small leftover pieces by redistributing sizes
305+
* when possible.
306+
*
307+
* @param {number} length - The total length to be divided.
308+
* @param {number} maxSize - The maximum allowed size for any single piece.
309+
* @param {number} [minSize=2] - The minimum allowed size for any single piece (default is 2).
310+
* @returns {number[][]} An array of arrays, where each inner array represents a possible way to divide the length
311+
* into pieces that sum to `length`, with each piece between `minSize` and `maxSize` (inclusive).
312+
*
313+
* The algorithm tries all possible base sizes from `maxSize` down to `minSize`, and for each, it:
314+
* - Adds divisions with only full-size pieces if possible.
315+
* - Adds divisions with a remainder piece if the remainder is at least `minSize`.
316+
* - Attempts to redistribute sizes to avoid small remainders (e.g., instead of 6+6+1, tries 5+5+3).
305317
*/
306318
const findDivisions = (length: number, maxSize: number, minSize: number = 2): number[][] => {
307319
const divisions: number[][] = [];

0 commit comments

Comments
 (0)