This commit is contained in:
Mayank Mohapatra 2025-02-21 19:18:03 +00:00
parent 79d013d05e
commit 7bc2b2a3d8

View file

@ -78,6 +78,7 @@ func createCircularArc(edge *d2graph.Edge) {
dstAngle += 2 * math.Pi dstAngle += 2 * math.Pi
} }
// Use the source center's distance for the arc radius.
arcRadius := math.Hypot(srcCenter.X, srcCenter.Y) arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
path := make([]*geo.Point, 0, ARC_STEPS+1) path := make([]*geo.Point, 0, ARC_STEPS+1)
@ -113,15 +114,11 @@ func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *
if boxContains(box, path[i]) { if boxContains(box, path[i]) {
continue continue
} }
seg := geo.NewSegment(path[i-1], path[i]) // Refine the intersection between the last inside and the first outside point.
inters := boxIntersections(box, *seg) refined := refineIntersection(box, path[i-1], path[i])
if len(inters) > 0 { return i, refined
return i, inters[0]
} }
return i, path[i] return len(path) - 1, path[len(path)-1]
}
last := len(path) - 1
return last, path[last]
} }
func clampPointOutsideBoxReverse(box *geo.Box, path []*geo.Point, endIdx int) (int, *geo.Point) { func clampPointOutsideBoxReverse(box *geo.Box, path []*geo.Point, endIdx int) (int, *geo.Point) {
@ -136,16 +133,27 @@ func clampPointOutsideBoxReverse(box *geo.Box, path []*geo.Point, endIdx int) (i
if boxContains(box, path[j]) { if boxContains(box, path[j]) {
continue continue
} }
seg := geo.NewSegment(path[j], path[j+1]) refined := refineIntersection(box, path[j], path[j+1])
inters := boxIntersections(box, *seg) return j, refined
if len(inters) > 0 {
return j, inters[0]
}
return j, path[j]
} }
return 0, path[0] return 0, path[0]
} }
func refineIntersection(box *geo.Box, pInside, pOutside *geo.Point) *geo.Point {
const epsilon = 1e-6
a := pInside
b := pOutside
for math.Hypot(b.X-a.X, b.Y-a.Y) > epsilon {
mid := geo.NewPoint((a.X+b.X)/2, (a.Y+b.Y)/2)
if boxContains(box, mid) {
a = mid
} else {
b = mid
}
}
return geo.NewPoint((a.X+b.X)/2, (a.Y+b.Y)/2)
}
func boxContains(b *geo.Box, p *geo.Point) bool { func boxContains(b *geo.Box, p *geo.Point) bool {
return p.X >= b.TopLeft.X && return p.X >= b.TopLeft.X &&
p.X <= b.TopLeft.X+b.Width && p.X <= b.TopLeft.X+b.Width &&