This commit is contained in:
Mayank Mohapatra 2025-02-22 10:50:31 +00:00
parent be5ba61297
commit 85c1ea10f4

View file

@ -66,64 +66,44 @@ func positionObjects(objects []*d2graph.Object, radius float64) {
} }
func createCircularArc(edge *d2graph.Edge) { func createCircularArc(edge *d2graph.Edge) {
if edge.Src == nil || edge.Dst == nil { if edge.Src == nil || edge.Dst == nil {
return return
} }
srcCenter := edge.Src.Center() srcCenter := edge.Src.Center()
dstCenter := edge.Dst.Center() dstCenter := edge.Dst.Center()
// Calculate the radius based on the distance from origin to the centers srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
srcRadius := math.Hypot(srcCenter.X, srcCenter.Y) dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
dstRadius := math.Hypot(dstCenter.X, dstCenter.Y) if dstAngle < srcAngle {
dstAngle += 2 * math.Pi
// Use average radius for more consistent arcs }
// Calculate angles but preserve relative positioning arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
// Ensure we take the shorter path around the circle path := make([]*geo.Point, 0, ARC_STEPS+1)
if dstAngle < srcAngle { for i := 0; i <= ARC_STEPS; i++ {
if srcAngle - dstAngle > math.Pi { t := float64(i) / float64(ARC_STEPS)
dstAngle += 2 * math.Pi angle := srcAngle + t*(dstAngle-srcAngle)
} x := arcRadius * math.Cos(angle)
} else { y := arcRadius * math.Sin(angle)
if dstAngle - srcAngle > math.Pi { path = append(path, geo.NewPoint(x, y))
srcAngle += 2 * math.Pi }
} path[0] = srcCenter
} path[len(path)-1] = dstCenter
path := make([]*geo.Point, 0, ARC_STEPS+1) // Clamp endpoints to the boundaries of the source and destination boxes.
for i := 0; i <= ARC_STEPS; i++ { _, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
t := float64(i) / float64(ARC_STEPS) _, newDst := clampPointOutsideBoxReverse(edge.Dst.Box, path, len(path)-1)
angle := srcAngle + t*(dstAngle-srcAngle) path[0] = newSrc
path[len(path)-1] = newDst
// Use interpolated radius for smoother transition
radius := srcRadius + t*(dstRadius-srcRadius)
x := radius * math.Cos(angle)
y := radius * math.Sin(angle)
path = append(path, geo.NewPoint(x, y))
}
// Ensure exact endpoints
path[0] = srcCenter
path[len(path)-1] = dstCenter
// Clamp endpoints to the boundaries of the source and destination boxes // Trim redundant path points that fall inside node boundaries.
_, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0) path = trimPathPoints(path, edge.Src.Box)
_, newDst := clampPointOutsideBoxReverse(edge.Dst.Box, path, len(path)-1) path = trimPathPoints(path, edge.Dst.Box)
path[0] = newSrc
path[len(path)-1] = newDst
// Trim redundant path points that fall inside node boundaries edge.Route = path
path = trimPathPoints(path, edge.Src.Box) edge.IsCurve = true
path = trimPathPoints(path, edge.Dst.Box)
edge.Route = path
edge.IsCurve = true
} }
// clampPointOutsideBox walks forward along the path until it finds a point outside the box, // clampPointOutsideBox walks forward along the path until it finds a point outside the box,