| 47 | |
| 48 | /** Returns the ids whose labels should render, in placement order. */ |
| 49 | export function selectLabels(boxes: LabelBox[], cap: number): string[] { |
| 50 | const ordered = [...boxes].sort( |
| 51 | (a, b) => a.priority - b.priority || b.degree - a.degree || a.id.localeCompare(b.id), |
| 52 | ); |
| 53 | const placed: LabelBox[] = []; |
| 54 | const chosen: string[] = []; |
| 55 | let capped = 0; |
| 56 | for (const box of ordered) { |
| 57 | if (box.sticky) { |
| 58 | placed.push(box); |
| 59 | chosen.push(box.id); |
| 60 | continue; |
| 61 | } |
| 62 | if (capped >= cap) continue; |
| 63 | if (placed.some((other) => overlaps(box, other))) continue; |
| 64 | placed.push(box); |
| 65 | chosen.push(box.id); |
| 66 | capped++; |
| 67 | } |
| 68 | return chosen; |
| 69 | } |