@@ -39,10 +39,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) e
39
39
return nil
40
40
}
41
41
42
- // calculateRadius now guards against a division-by-zero error when there is only one object.
42
+ // calculateRadius computes the required radius such that the chord between
43
+ // adjacent objects is at least (maxSize + 2*PADDING). We multiply the resulting
44
+ // radius by 1.1 (10% extra) to provide additional separation and avoid overlapping.
43
45
func calculateRadius (objects []* d2graph.Object ) float64 {
44
46
if len (objects ) < 2 {
45
- // When there is a single object, we can simply use MIN_RADIUS.
46
47
return MIN_RADIUS
47
48
}
48
49
numObjects := float64 (len (objects ))
@@ -51,16 +52,21 @@ func calculateRadius(objects []*d2graph.Object) float64 {
51
52
size := math .Max (obj .Box .Width , obj .Box .Height )
52
53
maxSize = math .Max (maxSize , size )
53
54
}
55
+ // The chord between adjacent centers is 2*radius*sin(π/n). To ensure that
56
+ // chord >= maxSize + 2*PADDING, we require:
57
+ // radius >= (maxSize/2 + PADDING) / sin(π/n)
54
58
minRadius := (maxSize / 2.0 + PADDING ) / math .Sin (math .Pi / numObjects )
55
- return math .Max (minRadius , MIN_RADIUS )
59
+ // Use MIN_RADIUS as a lower bound and then add a 10% extra margin.
60
+ radius := math .Max (minRadius , MIN_RADIUS )
61
+ return radius * 1.1
56
62
}
57
63
58
64
func positionObjects (objects []* d2graph.Object , radius float64 ) {
59
65
numObjects := float64 (len (objects ))
60
66
angleOffset := - math .Pi / 2
61
67
62
68
for i , obj := range objects {
63
- angle := angleOffset + (2 * math .Pi * float64 (i ) / numObjects )
69
+ angle := angleOffset + (2 * math .Pi * float64 (i )/ numObjects )
64
70
x := radius * math .Cos (angle )
65
71
y := radius * math .Sin (angle )
66
72
obj .TopLeft = geo .NewPoint (
@@ -167,7 +173,7 @@ func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *
167
173
}
168
174
return i , path [i ]
169
175
}
170
- return len (path ) - 1 , path [len (path )- 1 ]
176
+ return len (path )- 1 , path [len (path )- 1 ]
171
177
}
172
178
173
179
// clampPointOutsideBoxReverse works similarly but in reverse order.
0 commit comments