MCPcopy Create free account
hub / github.com/DavidHDev/react-bits / computeCornerRadii

Function computeCornerRadii

src/tools/shape-magic/computeBridges.js:7–76  ·  view source on GitHub ↗
(shapes, globalRadius, tolerance = 1)

Source from the content-addressed store, hash-verified

5const approxEqual = (a, b, tol = 1) => Math.abs(a - b) <= tol;
6
7export const computeCornerRadii = (shapes, globalRadius, tolerance = 1) => {
8 const result = {};
9
10 for (const shape of shapes) {
11 const r = getRadius(shape, globalRadius);
12 result[shape.id] = { tl: r, tr: r, br: r, bl: r };
13 }
14
15 for (let i = 0; i < shapes.length; i++) {
16 for (let j = i + 1; j < shapes.length; j++) {
17 const a = shapes[i];
18 const b = shapes[j];
19
20 const aL = a.x,
21 aR = a.x + a.w,
22 aT = a.y,
23 aB = a.y + a.h;
24 const bL = b.x,
25 bR = b.x + b.w,
26 bT = b.y,
27 bB = b.y + b.h;
28
29 if (approxEqual(aR, bL, tolerance)) {
30 const overlapT = Math.max(aT, bT);
31 const overlapB = Math.min(aB, bB);
32 if (overlapB > overlapT) {
33 if (aT >= overlapT - tolerance && aT <= overlapB + tolerance) result[a.id].tr = 0;
34 if (aB >= overlapT - tolerance && aB <= overlapB + tolerance) result[a.id].br = 0;
35 if (bT >= overlapT - tolerance && bT <= overlapB + tolerance) result[b.id].tl = 0;
36 if (bB >= overlapT - tolerance && bB <= overlapB + tolerance) result[b.id].bl = 0;
37 }
38 }
39
40 if (approxEqual(aL, bR, tolerance)) {
41 const overlapT = Math.max(aT, bT);
42 const overlapB = Math.min(aB, bB);
43 if (overlapB > overlapT) {
44 if (aT >= overlapT - tolerance && aT <= overlapB + tolerance) result[a.id].tl = 0;
45 if (aB >= overlapT - tolerance && aB <= overlapB + tolerance) result[a.id].bl = 0;
46 if (bT >= overlapT - tolerance && bT <= overlapB + tolerance) result[b.id].tr = 0;
47 if (bB >= overlapT - tolerance && bB <= overlapB + tolerance) result[b.id].br = 0;
48 }
49 }
50
51 if (approxEqual(aB, bT, tolerance)) {
52 const overlapL = Math.max(aL, bL);
53 const overlapR = Math.min(aR, bR);
54 if (overlapR > overlapL) {
55 if (aL >= overlapL - tolerance && aL <= overlapR + tolerance) result[a.id].bl = 0;
56 if (aR >= overlapL - tolerance && aR <= overlapR + tolerance) result[a.id].br = 0;
57 if (bL >= overlapL - tolerance && bL <= overlapR + tolerance) result[b.id].tl = 0;
58 if (bR >= overlapL - tolerance && bR <= overlapR + tolerance) result[b.id].tr = 0;
59 }
60 }
61
62 if (approxEqual(aT, bB, tolerance)) {
63 const overlapL = Math.max(aL, bL);
64 const overlapR = Math.min(aR, bR);

Callers 2

ShapeMagicFunction · 0.90
PresetTileFunction · 0.90

Calls 2

getRadiusFunction · 0.85
approxEqualFunction · 0.85

Tested by

no test coverage detected