(shapes, globalRadius, tolerance = 1)
| 76 | }; |
| 77 | |
| 78 | export const computeBridges = (shapes, globalRadius, tolerance = 1) => { |
| 79 | if (!shapes || shapes.length < 2) return []; |
| 80 | |
| 81 | const bridges = []; |
| 82 | const bridgeSet = new Set(); |
| 83 | |
| 84 | const addBridge = (x, y, r, rotation) => { |
| 85 | let offsetY = 0; |
| 86 | if (rotation === 0 || rotation === 90) { |
| 87 | offsetY = -r; |
| 88 | } else if (rotation === 180 || rotation === 270) { |
| 89 | offsetY = r; |
| 90 | } |
| 91 | |
| 92 | const finalX = x; |
| 93 | const finalY = y + offsetY; |
| 94 | const key = `${Math.round(finalX)},${Math.round(finalY)},${rotation}`; |
| 95 | if (!bridgeSet.has(key)) { |
| 96 | bridgeSet.add(key); |
| 97 | bridges.push({ id: `bridge-${bridges.length}`, x: finalX, y: finalY, r, rotation }); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | for (let i = 0; i < shapes.length; i++) { |
| 102 | for (let j = 0; j < shapes.length; j++) { |
| 103 | if (i === j) continue; |
| 104 | |
| 105 | const a = shapes[i]; |
| 106 | const b = shapes[j]; |
| 107 | const r = Math.min(getRadius(a, globalRadius), getRadius(b, globalRadius)); |
| 108 | |
| 109 | const aL = a.x, |
| 110 | aR = a.x + a.w, |
| 111 | aT = a.y, |
| 112 | aB = a.y + a.h; |
| 113 | const bL = b.x, |
| 114 | bR = b.x + b.w, |
| 115 | bT = b.y, |
| 116 | bB = b.y + b.h; |
| 117 | |
| 118 | if (approxEqual(aR, bL, tolerance) && aB > bT && aT < bB) { |
| 119 | if (aT < bT - tolerance) { |
| 120 | addBridge(aR, bT, r, 0); |
| 121 | } |
| 122 | |
| 123 | if (aB > bB + tolerance) { |
| 124 | addBridge(aR, bB, r, 270); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (approxEqual(aL, bR, tolerance) && aB > bT && aT < bB) { |
| 129 | if (aT < bT - tolerance) { |
| 130 | addBridge(aL, bT, r, 90); |
| 131 | } |
| 132 | |
| 133 | if (aB > bB + tolerance) { |
| 134 | addBridge(aL, bB, r, 180); |
| 135 | } |
no test coverage detected