This commit is contained in:
Mayank Mohapatra 2025-03-08 11:05:04 +00:00
parent 95e0aaedd9
commit dfc4e4bc3d
5 changed files with 5237 additions and 4919 deletions

View file

@ -39,10 +39,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) e
return nil return nil
} }
// calculateRadius now guards against a division-by-zero error when there is only one object. // calculateRadius computes the required radius such that the chord between
// adjacent objects is at least (maxSize + 2*PADDING). We multiply the resulting
// radius by 1.1 (10% extra) to provide additional separation and avoid overlapping.
func calculateRadius(objects []*d2graph.Object) float64 { func calculateRadius(objects []*d2graph.Object) float64 {
if len(objects) < 2 { if len(objects) < 2 {
// When there is a single object, we can simply use MIN_RADIUS.
return MIN_RADIUS return MIN_RADIUS
} }
numObjects := float64(len(objects)) numObjects := float64(len(objects))
@ -51,8 +52,13 @@ func calculateRadius(objects []*d2graph.Object) float64 {
size := math.Max(obj.Box.Width, obj.Box.Height) size := math.Max(obj.Box.Width, obj.Box.Height)
maxSize = math.Max(maxSize, size) maxSize = math.Max(maxSize, size)
} }
// The chord between adjacent centers is 2*radius*sin(π/n). To ensure that
// chord >= maxSize + 2*PADDING, we require:
// radius >= (maxSize/2 + PADDING) / sin(π/n)
minRadius := (maxSize/2.0 + PADDING) / math.Sin(math.Pi/numObjects) minRadius := (maxSize/2.0 + PADDING) / math.Sin(math.Pi/numObjects)
return math.Max(minRadius, MIN_RADIUS) // Use MIN_RADIUS as a lower bound and then add a 10% extra margin.
radius := math.Max(minRadius, MIN_RADIUS)
return radius * 1.1
} }
func positionObjects(objects []*d2graph.Object, radius float64) { func positionObjects(objects []*d2graph.Object, radius float64) {

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 47 KiB

After

Width:  |  Height:  |  Size: 48 KiB

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 47 KiB

After

Width:  |  Height:  |  Size: 48 KiB