This commit is contained in:
Mayank Mohapatra 2025-02-22 10:36:51 +00:00
parent 3c1be1ee2a
commit d00529c6f0

View file

@ -18,6 +18,7 @@ const (
EPSILON = 1e-10 // Small value for floating point comparisons
)
// Layout computes node positions and generates curved edge routes.
func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) error {
objects := g.Root.ChildrenArray
if len(objects) == 0 {
@ -65,6 +66,7 @@ func positionObjects(objects []*d2graph.Object, radius float64) {
}
}
// createPreciseCircularArc computes a curved edge path that touches the node boundaries exactly.
func createPreciseCircularArc(edge *d2graph.Edge) {
if edge.Src == nil || edge.Dst == nil {
return
@ -73,7 +75,7 @@ func createPreciseCircularArc(edge *d2graph.Edge) {
srcCenter := edge.Src.Center()
dstCenter := edge.Dst.Center()
// Calculate angles in the circular layout
// Compute angles for the circular arc.
srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
if dstAngle < srcAngle {
@ -82,7 +84,7 @@ func createPreciseCircularArc(edge *d2graph.Edge) {
arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
// Generate initial path points
// Generate the initial arc path.
path := make([]*geo.Point, 0, ARC_STEPS+1)
for i := 0; i <= ARC_STEPS; i++ {
t := float64(i) / float64(ARC_STEPS)
@ -92,24 +94,28 @@ func createPreciseCircularArc(edge *d2graph.Edge) {
path = append(path, geo.NewPoint(x, y))
}
// Find precise intersection points
// Compute precise intersection points so the arrow touches the node boundaries.
// For the source, the segment goes from the center (inside) to the next point (outside).
srcIntersection := findPreciseBoxIntersection(edge.Src.Box, path[0], path[1])
// For the destination, the segment goes from the center to the previous point (outside).
dstIntersection := findPreciseBoxIntersection(edge.Dst.Box, path[len(path)-1], path[len(path)-2])
// Update path endpoints with precise intersections
// Update the endpoints with the snapped intersection points.
path[0] = srcIntersection
path[len(path)-1] = dstIntersection
// Remove any points that might be inside the boxes
// Trim intermediate points that still fall inside the boxes.
startIdx := 0
endIdx := len(path) - 1
for i := 1; i < len(path)-1; i++ {
if boxContains(edge.Src.Box, path[i]) {
startIdx = i
for i := 1; i < len(path); i++ {
if !boxContains(edge.Src.Box, path[i]) {
startIdx = i - 1
break
}
if boxContains(edge.Dst.Box, path[i]) {
endIdx = i
}
for i := len(path) - 2; i >= 0; i-- {
if !boxContains(edge.Dst.Box, path[i]) {
endIdx = i + 1
break
}
}
@ -118,44 +124,38 @@ func createPreciseCircularArc(edge *d2graph.Edge) {
edge.IsCurve = true
}
// findPreciseBoxIntersection returns the intersection point of the line (from p1 to p2) with the box boundary,
// snapped exactly to the nearest edge.
func findPreciseBoxIntersection(box *geo.Box, p1, p2 *geo.Point) *geo.Point {
// Define box edges as line segments
// Define the four box edges.
edges := []geo.Segment{
// Top edge
*geo.NewSegment(
geo.NewPoint(box.TopLeft.X, box.TopLeft.Y),
geo.NewPoint(box.TopLeft.X+box.Width, box.TopLeft.Y),
),
// Right edge
), // Top
*geo.NewSegment(
geo.NewPoint(box.TopLeft.X+box.Width, box.TopLeft.Y),
geo.NewPoint(box.TopLeft.X+box.Width, box.TopLeft.Y+box.Height),
),
// Bottom edge
), // Right
*geo.NewSegment(
geo.NewPoint(box.TopLeft.X, box.TopLeft.Y+box.Height),
geo.NewPoint(box.TopLeft.X+box.Width, box.TopLeft.Y+box.Height),
),
// Left edge
), // Bottom
*geo.NewSegment(
geo.NewPoint(box.TopLeft.X, box.TopLeft.Y),
geo.NewPoint(box.TopLeft.X, box.TopLeft.Y+box.Height),
),
), // Left
}
// Line segment from p1 to p2
// Construct the line from p1 (inside) to p2 (outside).
line := *geo.NewSegment(p1, p2)
// Find the intersection point closest to p1
var closestIntersection *geo.Point
minDist := math.MaxFloat64
for _, edge := range edges {
if intersection := findSegmentIntersection(line, edge); intersection != nil {
dist := math.Hypot(
intersection.X-p1.X,
intersection.Y-p1.Y,
)
// Find the intersection among the four edges that is closest to p1.
for _, seg := range edges {
if intersection := findSegmentIntersection(line, seg); intersection != nil {
dist := math.Hypot(intersection.X-p1.X, intersection.Y-p1.Y)
if dist < minDist {
minDist = dist
closestIntersection = intersection
@ -164,35 +164,58 @@ func findPreciseBoxIntersection(box *geo.Box, p1, p2 *geo.Point) *geo.Point {
}
if closestIntersection != nil {
return closestIntersection
return snapToBoundary(box, closestIntersection)
}
return p1
}
// findSegmentIntersection computes the intersection between two line segments s1 and s2 using their parametric form.
func findSegmentIntersection(s1, s2 geo.Segment) *geo.Point {
// Calculate the intersection of two line segments using parametric equations
x1, y1 := s1.Start.X, s1.Start.Y
x2, y2 := s1.End.X, s1.End.Y
x3, y3 := s2.Start.X, s2.Start.Y
x4, y4 := s2.End.X, s2.End.Y
denominator := (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4)
if math.Abs(denominator) < EPSILON {
denom := (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4)
if math.Abs(denom) < EPSILON {
return nil
}
t := ((x1-x3)*(y3-y4) - (y1-y3)*(x3-x4)) / denominator
u := -((x1-x2)*(y1-y3) - (y1-y2)*(x1-x3)) / denominator
t := ((x1-x3)*(y3-y4) - (y1-y3)*(x3-x4)) / denom
u := -((x1-x2)*(y1-y3) - (y1-y2)*(x1-x3)) / denom
if t >= 0 && t <= 1 && u >= 0 && u <= 1 {
x := x1 + t*(x2-x1)
y := y1 + t*(y2-y1)
return geo.NewPoint(x, y)
}
return nil
}
// snapToBoundary adjusts point p so that it lies exactly on the nearest boundary of box.
func snapToBoundary(box *geo.Box, p *geo.Point) *geo.Point {
left := box.TopLeft.X
right := box.TopLeft.X + box.Width
top := box.TopLeft.Y
bottom := box.TopLeft.Y + box.Height
dLeft := math.Abs(p.X - left)
dRight := math.Abs(p.X - right)
dTop := math.Abs(p.Y - top)
dBottom := math.Abs(p.Y - bottom)
if dLeft < dRight && dLeft < dTop && dLeft < dBottom {
return geo.NewPoint(left, p.Y)
} else if dRight < dLeft && dRight < dTop && dRight < dBottom {
return geo.NewPoint(right, p.Y)
} else if dTop < dBottom {
return geo.NewPoint(p.X, top)
} else {
return geo.NewPoint(p.X, bottom)
}
}
// boxContains returns true if point p is inside the box (using EPSILON for floating point tolerance).
func boxContains(b *geo.Box, p *geo.Point) bool {
return p.X >= b.TopLeft.X-EPSILON &&
p.X <= b.TopLeft.X+b.Width+EPSILON &&