From b1cb08027e3ec33936552dbfc55db63ee0fbd8f6 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 14 Jul 2023 13:24:00 -0700 Subject: [PATCH] update tracing to label box --- d2graph/layout.go | 11 +++++++++++ lib/geo/box.go | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/d2graph/layout.go b/d2graph/layout.go index 05ef65fb0..358a95934 100644 --- a/d2graph/layout.go +++ b/d2graph/layout.go @@ -336,6 +336,12 @@ func (edge *Edge) TraceToShape(points []*geo.Point, startIndex, endIndex int) (n // add left/right padding to box labelBox.TopLeft.X -= label.PADDING labelBox.Width += 2 * label.PADDING + + for labelBox.Contains(startingSegment.End) && startIndex+1 > endIndex { + startingSegment.Start = startingSegment.End + startingSegment.End = points[startIndex+2] + startIndex++ + } if intersections := labelBox.Intersections(startingSegment); len(intersections) > 0 { overlapsOutsideLabel = true p := intersections[0] @@ -381,6 +387,11 @@ func (edge *Edge) TraceToShape(points []*geo.Point, startIndex, endIndex int) (n // add left/right padding to box labelBox.TopLeft.X -= label.PADDING labelBox.Width += 2 * label.PADDING + for labelBox.Contains(endingSegment.Start) && endIndex-1 > startIndex { + endingSegment.End = endingSegment.Start + endingSegment.Start = points[endIndex-2] + endIndex-- + } if intersections := labelBox.Intersections(endingSegment); len(intersections) > 0 { overlapsOutsideLabel = true p := intersections[0] diff --git a/lib/geo/box.go b/lib/geo/box.go index 3fe0eb7c2..de9b36cff 100644 --- a/lib/geo/box.go +++ b/lib/geo/box.go @@ -78,3 +78,8 @@ func (b *Box) ToString() string { } return fmt.Sprintf("{TopLeft: %s, Width: %.0f, Height: %.0f}", b.TopLeft.ToString(), b.Width, b.Height) } + +func (b *Box) Contains(p *Point) bool { + return !(p.X < b.TopLeft.X || b.TopLeft.X+b.Width < p.X || + p.Y < b.TopLeft.Y || b.TopLeft.Y+b.Height < p.Y) +}