Skip to content

Commit dfc4e4b

Browse files
try
1 parent 95e0aae commit dfc4e4b

File tree

5 files changed

+5237
-4919
lines changed

5 files changed

+5237
-4919
lines changed

d2layouts/d2cycle/layout.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) e
3939
return nil
4040
}
4141

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.
4345
func calculateRadius(objects []*d2graph.Object) float64 {
4446
if len(objects) < 2 {
45-
// When there is a single object, we can simply use MIN_RADIUS.
4647
return MIN_RADIUS
4748
}
4849
numObjects := float64(len(objects))
@@ -51,16 +52,21 @@ func calculateRadius(objects []*d2graph.Object) float64 {
5152
size := math.Max(obj.Box.Width, obj.Box.Height)
5253
maxSize = math.Max(maxSize, size)
5354
}
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)
5458
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
5662
}
5763

5864
func positionObjects(objects []*d2graph.Object, radius float64) {
5965
numObjects := float64(len(objects))
6066
angleOffset := -math.Pi / 2
6167

6268
for i, obj := range objects {
63-
angle := angleOffset + (2 * math.Pi * float64(i) / numObjects)
69+
angle := angleOffset + (2*math.Pi*float64(i)/numObjects)
6470
x := radius * math.Cos(angle)
6571
y := radius * math.Sin(angle)
6672
obj.TopLeft = geo.NewPoint(
@@ -167,7 +173,7 @@ func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *
167173
}
168174
return i, path[i]
169175
}
170-
return len(path) - 1, path[len(path)-1]
176+
return len(path)-1, path[len(path)-1]
171177
}
172178

173179
// clampPointOutsideBoxReverse works similarly but in reverse order.

0 commit comments

Comments
 (0)