This commit is contained in:
Mayank Mohapatra 2025-02-22 11:06:24 +00:00
parent 703ab3300b
commit 9eaa03cb36
5 changed files with 214 additions and 1036 deletions

View file

@ -65,46 +65,6 @@ func positionObjects(objects []*d2graph.Object, radius float64) {
}
}
// func createCircularArc(edge *d2graph.Edge) {
// if edge.Src == nil || edge.Dst == nil {
// return
// }
// srcCenter := edge.Src.Center()
// dstCenter := edge.Dst.Center()
// srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
// dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
// if dstAngle < srcAngle {
// dstAngle += 2 * math.Pi
// }
// arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
// path := make([]*geo.Point, 0, ARC_STEPS+1)
// for i := 0; i <= ARC_STEPS; i++ {
// t := float64(i) / float64(ARC_STEPS)
// angle := srcAngle + t*(dstAngle-srcAngle)
// x := arcRadius * math.Cos(angle)
// y := arcRadius * math.Sin(angle)
// path = append(path, geo.NewPoint(x, y))
// }
// path[0] = srcCenter
// path[len(path)-1] = dstCenter
// // Clamp endpoints to the boundaries of the source and destination boxes.
// _, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
// _, newDst := clampPointOutsideBoxReverse(edge.Dst.Box, path, len(path)-1)
// path[0] = newSrc
// path[len(path)-1] = newDst
// // Trim redundant path points that fall inside node boundaries.
// path = trimPathPoints(path, edge.Src.Box)
// path = trimPathPoints(path, edge.Dst.Box)
// edge.Route = path
// edge.IsCurve = true
// }
func createCircularArc(edge *d2graph.Edge) {
if edge.Src == nil || edge.Dst == nil {
return
@ -113,27 +73,15 @@ func createCircularArc(edge *d2graph.Edge) {
srcCenter := edge.Src.Center()
dstCenter := edge.Dst.Center()
// Calculate angles and ensure we take the shortest arc
srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
// Calculate angular distance for both directions
clockwiseDist := dstAngle - srcAngle
// Choose shortest path
if math.Abs(clockwiseDist) > math.Pi {
if clockwiseDist > 0 {
dstAngle -= 2 * math.Pi
} else {
if dstAngle < srcAngle {
dstAngle += 2 * math.Pi
}
}
arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
path := make([]*geo.Point, 0, ARC_STEPS+1)
// Generate path with corrected direction
path := make([]*geo.Point, 0, ARC_STEPS+1)
for i := 0; i <= ARC_STEPS; i++ {
t := float64(i) / float64(ARC_STEPS)
angle := srcAngle + t*(dstAngle-srcAngle)
@ -141,41 +89,23 @@ func createCircularArc(edge *d2graph.Edge) {
y := arcRadius * math.Sin(angle)
path = append(path, geo.NewPoint(x, y))
}
path[0] = srcCenter
path[len(path)-1] = dstCenter
// [Keep existing clamping and trimming code...]
// Clamp endpoints to the boundaries of the source and destination boxes.
_, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
_, newDst := clampPointOutsideBoxReverse(edge.Dst.Box, path, len(path)-1)
path[0] = newSrc
path[len(path)-1] = newDst
// Calculate tangent direction based on arc direction
if len(path) >= 2 {
last := path[len(path)-1]
var tangentX, tangentY float64
// Determine direction using angular difference
if (dstAngle - srcAngle) > 0 {
// Counter-clockwise direction
tangentX = -last.Y
tangentY = last.X
} else {
// Clockwise direction
tangentX = last.Y
tangentY = -last.X
}
// Normalize and adjust second-to-last point
tangentLength := math.Hypot(tangentX, tangentY)
if tangentLength > 0 {
tangentDir := geo.NewPoint(tangentX/tangentLength, tangentY/tangentLength)
delta := 10.0
newSecondLast := geo.NewPoint(
last.X-delta*tangentDir.X,
last.Y-delta*tangentDir.Y,
)
path[len(path)-2] = newSecondLast
}
}
// Trim redundant path points that fall inside node boundaries.
path = trimPathPoints(path, edge.Src.Box)
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,
// then replaces the point with a precise intersection.
func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *geo.Point) {

View file

@ -540,40 +540,8 @@
"link": "",
"route": [
{
"x": 0,
"y": -200
},
{
"x": 3.1410000324249268,
"y": -199.97500610351562
},
{
"x": 6.2820000648498535,
"y": -199.9010009765625
},
{
"x": 9.420999526977539,
"y": -199.77699279785156
},
{
"x": 12.557999610900879,
"y": -199.60499572753906
},
{
"x": 15.690999984741211,
"y": -199.38299560546875
},
{
"x": 18.820999145507812,
"y": -199.11199951171875
},
{
"x": 21.945999145507812,
"y": -198.79200744628906
},
{
"x": 25.06599998474121,
"y": -198.4219970703125
"x": 26.5,
"y": -198.22999572753906
},
{
"x": 28.18000030517578,
@ -900,48 +868,8 @@
"y": -34.3849983215332
},
{
"x": 197.53700256347656,
"y": -31.285999298095703
},
{
"x": 198.00399780273438,
"y": -28.18000030517578
},
{
"x": 198.4219970703125,
"y": -25.06599998474121
},
{
"x": 198.79200744628906,
"y": -21.945999145507812
},
{
"x": 199.11199951171875,
"y": -18.820999145507812
},
{
"x": 199.38299560546875,
"y": -15.690999984741211
},
{
"x": 199.60499572753906,
"y": -12.557999610900879
},
{
"x": 199.77699279785156,
"y": -9.420999526977539
},
{
"x": 199.9010009765625,
"y": -6.2820000648498535
},
{
"x": 200,
"y": -10
},
{
"x": 200,
"y": 0
"x": 197.2519989013672,
"y": -33
}
],
"isCurve": true,
@ -976,48 +904,8 @@
"link": "",
"route": [
{
"x": 200,
"y": 0
},
{
"x": 199.97500610351562,
"y": 3.1410000324249268
},
{
"x": 199.9010009765625,
"y": 6.2820000648498535
},
{
"x": 199.77699279785156,
"y": 9.420999526977539
},
{
"x": 199.60499572753906,
"y": 12.557999610900879
},
{
"x": 199.38299560546875,
"y": 15.690999984741211
},
{
"x": 199.11199951171875,
"y": 18.820999145507812
},
{
"x": 198.79200744628906,
"y": 21.945999145507812
},
{
"x": 198.4219970703125,
"y": 25.06599998474121
},
{
"x": 198.00399780273438,
"y": 28.18000030517578
},
{
"x": 197.53700256347656,
"y": 31.285999298095703
"x": 197.2519989013672,
"y": 33
},
{
"x": 197.02099609375,
@ -1344,40 +1232,8 @@
"y": 198.00399780273438
},
{
"x": 25.06599998474121,
"y": 198.4219970703125
},
{
"x": 21.945999145507812,
"y": 198.79200744628906
},
{
"x": 18.820999145507812,
"y": 199.11199951171875
},
{
"x": 15.690999984741211,
"y": 199.38299560546875
},
{
"x": 12.557999610900879,
"y": 199.60499572753906
},
{
"x": 9.420999526977539,
"y": 199.77699279785156
},
{
"x": 6.2820000648498535,
"y": 199.9010009765625
},
{
"x": 10,
"y": 200
},
{
"x": 0,
"y": 200
"x": 26.5,
"y": 198.22999572753906
}
],
"isCurve": true,
@ -1412,40 +1268,8 @@
"link": "",
"route": [
{
"x": 0,
"y": 200
},
{
"x": -3.1410000324249268,
"y": 199.97500610351562
},
{
"x": -6.2820000648498535,
"y": 199.9010009765625
},
{
"x": -9.420999526977539,
"y": 199.77699279785156
},
{
"x": -12.557999610900879,
"y": 199.60499572753906
},
{
"x": -15.690999984741211,
"y": 199.38299560546875
},
{
"x": -18.820999145507812,
"y": 199.11199951171875
},
{
"x": -21.945999145507812,
"y": 198.79200744628906
},
{
"x": -25.06599998474121,
"y": 198.4219970703125
"x": -26.499000549316406,
"y": 198.22999572753906
},
{
"x": -28.18000030517578,
@ -1772,48 +1596,8 @@
"y": 34.3849983215332
},
{
"x": -197.53700256347656,
"y": 31.285999298095703
},
{
"x": -198.00399780273438,
"y": 28.18000030517578
},
{
"x": -198.4219970703125,
"y": 25.06599998474121
},
{
"x": -198.79200744628906,
"y": 21.945999145507812
},
{
"x": -199.11199951171875,
"y": 18.820999145507812
},
{
"x": -199.38299560546875,
"y": 15.690999984741211
},
{
"x": -199.60499572753906,
"y": 12.557999610900879
},
{
"x": -199.77699279785156,
"y": 9.420999526977539
},
{
"x": -199.9010009765625,
"y": 6.2820000648498535
},
{
"x": -200,
"y": 10
},
{
"x": -200,
"y": 0
"x": -197.2519989013672,
"y": 33
}
],
"isCurve": true,
@ -1848,32 +1632,8 @@
"link": "",
"route": [
{
"x": 513,
"y": -150
},
{
"x": 517.18798828125,
"y": -149.95599365234375
},
{
"x": 521.375,
"y": -149.82400512695312
},
{
"x": 525.5579833984375,
"y": -149.60499572753906
},
{
"x": 529.7349853515625,
"y": -149.29800415039062
},
{
"x": 533.905029296875,
"y": -148.9040069580078
},
{
"x": 538.0659790039062,
"y": -148.4219970703125
"x": 539.5,
"y": -148.2259979248047
},
{
"x": 542.2160034179688,
@ -2216,40 +1976,8 @@
"y": 115.77300262451172
},
{
"x": 700.4559936523438,
"y": 119.71399688720703
},
{
"x": 698.9550170898438,
"y": 123.6240005493164
},
{
"x": 697.3720092773438,
"y": 127.50299835205078
},
{
"x": 695.708984375,
"y": 131.3470001220703
},
{
"x": 693.9650268554688,
"y": 135.15499877929688
},
{
"x": 692.1420288085938,
"y": 138.927001953125
},
{
"x": 690.239990234375,
"y": 142.65899658203125
},
{
"x": 691.2050170898438,
"y": 141.33900451660156
},
{
"x": 686.2050170898438,
"y": 149.99899291992188
"x": 701.4329833984375,
"y": 116.9990005493164
}
],
"isCurve": true,
@ -2284,44 +2012,8 @@
"link": "",
"route": [
{
"x": 686.2050170898438,
"y": 150
},
{
"x": 684.072021484375,
"y": 153.60499572753906
},
{
"x": 681.864990234375,
"y": 157.1649932861328
},
{
"x": 679.583984375,
"y": 160.67799377441406
},
{
"x": 677.22900390625,
"y": 164.14199829101562
},
{
"x": 674.802978515625,
"y": 167.5570068359375
},
{
"x": 672.3049926757812,
"y": 170.91900634765625
},
{
"x": 669.7379760742188,
"y": 174.22900390625
},
{
"x": 667.1019897460938,
"y": 177.48399353027344
},
{
"x": 664.3989868164062,
"y": 180.6840057373047
"x": 662.3569946289062,
"y": 182.99899291992188
},
{
"x": 661.6279907226562,
@ -2648,44 +2340,8 @@
"y": 183.8260040283203
},
{
"x": 361.6000061035156,
"y": 180.6840057373047
},
{
"x": 358.8970031738281,
"y": 177.48399353027344
},
{
"x": 356.260986328125,
"y": 174.22900390625
},
{
"x": 353.6940002441406,
"y": 170.91900634765625
},
{
"x": 351.1960144042969,
"y": 167.5570068359375
},
{
"x": 348.7699890136719,
"y": 164.14199829101562
},
{
"x": 346.4150085449219,
"y": 160.67799377441406
},
{
"x": 344.1340026855469,
"y": 157.1649932861328
},
{
"x": 344.79400634765625,
"y": 158.66000366210938
},
{
"x": 339.79400634765625,
"y": 150
"x": 363.6419982910156,
"y": 183
}
],
"isCurve": true,
@ -2720,24 +2376,8 @@
"link": "",
"route": [
{
"x": 972,
"y": -200
},
{
"x": 978.281982421875,
"y": -199.9010009765625
},
{
"x": 984.5579833984375,
"y": -199.60499572753906
},
{
"x": 990.8209838867188,
"y": -199.11199951171875
},
{
"x": 997.0659790039062,
"y": -198.4219970703125
"x": 998.5,
"y": -198.21800231933594
},
{
"x": 1003.2860107421875,
@ -3104,24 +2744,8 @@
"y": 197.53700256347656
},
{
"x": 997.0659790039062,
"y": 198.4219970703125
},
{
"x": 990.8209838867188,
"y": 199.11199951171875
},
{
"x": 984.5579833984375,
"y": 199.60499572753906
},
{
"x": 982,
"y": 200
},
{
"x": 972,
"y": 200
"x": 998.5,
"y": 198.21800231933594
}
],
"isCurve": true,

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View file

@ -540,40 +540,8 @@
"link": "",
"route": [
{
"x": 12,
"y": -188
},
{
"x": 15.140999794006348,
"y": -187.97500610351562
},
{
"x": 18.281999588012695,
"y": -187.9010009765625
},
{
"x": 21.42099952697754,
"y": -187.77699279785156
},
{
"x": 24.558000564575195,
"y": -187.60499572753906
},
{
"x": 27.69099998474121,
"y": -187.38299560546875
},
{
"x": 30.820999145507812,
"y": -187.11199951171875
},
{
"x": 33.94599914550781,
"y": -186.79200744628906
},
{
"x": 37.066001892089844,
"y": -186.4219970703125
"x": 38.5,
"y": -186.22999572753906
},
{
"x": 40.18000030517578,
@ -900,48 +868,8 @@
"y": -22.385000228881836
},
{
"x": 209.53700256347656,
"y": -19.285999298095703
},
{
"x": 210.00399780273438,
"y": -16.18000030517578
},
{
"x": 210.4219970703125,
"y": -13.065999984741211
},
{
"x": 210.79200744628906,
"y": -9.946000099182129
},
{
"x": 211.11199951171875,
"y": -6.821000099182129
},
{
"x": 211.38299560546875,
"y": -3.690999984741211
},
{
"x": 211.60499572753906,
"y": -0.5580000281333923
},
{
"x": 211.77699279785156,
"y": 2.578000068664551
},
{
"x": 211.9010009765625,
"y": 5.7170000076293945
},
{
"x": 212,
"y": 2
},
{
"x": 212,
"y": 12
"x": 209.2519989013672,
"y": -21
}
],
"isCurve": true,
@ -976,48 +904,8 @@
"link": "",
"route": [
{
"x": 212,
"y": 12
},
{
"x": 211.97500610351562,
"y": 15.140999794006348
},
{
"x": 211.9010009765625,
"y": 18.281999588012695
},
{
"x": 211.77699279785156,
"y": 21.42099952697754
},
{
"x": 211.60499572753906,
"y": 24.558000564575195
},
{
"x": 211.38299560546875,
"y": 27.69099998474121
},
{
"x": 211.11199951171875,
"y": 30.820999145507812
},
{
"x": 210.79200744628906,
"y": 33.94599914550781
},
{
"x": 210.4219970703125,
"y": 37.066001892089844
},
{
"x": 210.00399780273438,
"y": 40.18000030517578
},
{
"x": 209.53700256347656,
"y": 43.2859992980957
"x": 209.2519989013672,
"y": 45
},
{
"x": 209.02099609375,
@ -1344,40 +1232,8 @@
"y": 210.00399780273438
},
{
"x": 37.066001892089844,
"y": 210.4219970703125
},
{
"x": 33.94599914550781,
"y": 210.79200744628906
},
{
"x": 30.820999145507812,
"y": 211.11199951171875
},
{
"x": 27.69099998474121,
"y": 211.38299560546875
},
{
"x": 24.558000564575195,
"y": 211.60499572753906
},
{
"x": 21.42099952697754,
"y": 211.77699279785156
},
{
"x": 18.281999588012695,
"y": 211.9010009765625
},
{
"x": 22,
"y": 212
},
{
"x": 12,
"y": 212
"x": 38.5,
"y": 210.22999572753906
}
],
"isCurve": true,
@ -1412,40 +1268,8 @@
"link": "",
"route": [
{
"x": 12,
"y": 212
},
{
"x": 8.857999801635742,
"y": 211.97500610351562
},
{
"x": 5.7170000076293945,
"y": 211.9010009765625
},
{
"x": 2.578000068664551,
"y": 211.77699279785156
},
{
"x": -0.5580000281333923,
"y": 211.60499572753906
},
{
"x": -3.690999984741211,
"y": 211.38299560546875
},
{
"x": -6.821000099182129,
"y": 211.11199951171875
},
{
"x": -9.946000099182129,
"y": 210.79200744628906
},
{
"x": -13.065999984741211,
"y": 210.4219970703125
"x": -14.49899959564209,
"y": 210.22999572753906
},
{
"x": -16.18000030517578,
@ -1772,48 +1596,8 @@
"y": 46.3849983215332
},
{
"x": -185.53700256347656,
"y": 43.2859992980957
},
{
"x": -186.00399780273438,
"y": 40.18000030517578
},
{
"x": -186.4219970703125,
"y": 37.066001892089844
},
{
"x": -186.79200744628906,
"y": 33.94599914550781
},
{
"x": -187.11199951171875,
"y": 30.820999145507812
},
{
"x": -187.38299560546875,
"y": 27.69099998474121
},
{
"x": -187.60499572753906,
"y": 24.558000564575195
},
{
"x": -187.77699279785156,
"y": 21.42099952697754
},
{
"x": -187.9010009765625,
"y": 18.281999588012695
},
{
"x": -188,
"y": 22
},
{
"x": -188,
"y": 12
"x": -185.2519989013672,
"y": 45
}
],
"isCurve": true,
@ -1848,32 +1632,8 @@
"link": "",
"route": [
{
"x": 485.5,
"y": -138
},
{
"x": 489.68798828125,
"y": -137.95599365234375
},
{
"x": 493.875,
"y": -137.82400512695312
},
{
"x": 498.0580139160156,
"y": -137.60499572753906
},
{
"x": 502.2349853515625,
"y": -137.29800415039062
},
{
"x": 506.4049987792969,
"y": -136.9040069580078
},
{
"x": 510.5660095214844,
"y": -136.4219970703125
"x": 512,
"y": -136.2259979248047
},
{
"x": 514.7160034179688,
@ -2216,40 +1976,8 @@
"y": 127.77300262451172
},
{
"x": 672.9559936523438,
"y": 131.71400451660156
},
{
"x": 671.4550170898438,
"y": 135.62399291992188
},
{
"x": 669.8720092773438,
"y": 139.5030059814453
},
{
"x": 668.208984375,
"y": 143.3470001220703
},
{
"x": 666.4650268554688,
"y": 147.15499877929688
},
{
"x": 664.6420288085938,
"y": 150.927001953125
},
{
"x": 662.739990234375,
"y": 154.65899658203125
},
{
"x": 663.7050170898438,
"y": 153.33900451660156
},
{
"x": 658.7050170898438,
"y": 161.99899291992188
"x": 673.9329833984375,
"y": 128.99899291992188
}
],
"isCurve": true,
@ -2284,44 +2012,8 @@
"link": "",
"route": [
{
"x": 658.7050170898438,
"y": 161.99899291992188
},
{
"x": 656.572021484375,
"y": 165.60499572753906
},
{
"x": 654.364990234375,
"y": 169.1649932861328
},
{
"x": 652.083984375,
"y": 172.67799377441406
},
{
"x": 649.72900390625,
"y": 176.14199829101562
},
{
"x": 647.302978515625,
"y": 179.5570068359375
},
{
"x": 644.8049926757812,
"y": 182.91900634765625
},
{
"x": 642.2379760742188,
"y": 186.22900390625
},
{
"x": 639.6019897460938,
"y": 189.48399353027344
},
{
"x": 636.8989868164062,
"y": 192.6840057373047
"x": 634.8569946289062,
"y": 194.99899291992188
},
{
"x": 634.1279907226562,
@ -2648,44 +2340,8 @@
"y": 195.8260040283203
},
{
"x": 334.1000061035156,
"y": 192.6840057373047
},
{
"x": 331.3970031738281,
"y": 189.48399353027344
},
{
"x": 328.760986328125,
"y": 186.22900390625
},
{
"x": 326.1940002441406,
"y": 182.91900634765625
},
{
"x": 323.6960144042969,
"y": 179.5570068359375
},
{
"x": 321.2699890136719,
"y": 176.14199829101562
},
{
"x": 318.9150085449219,
"y": 172.67799377441406
},
{
"x": 316.6340026855469,
"y": 169.1649932861328
},
{
"x": 317.29400634765625,
"y": 170.66000366210938
},
{
"x": 312.29400634765625,
"y": 162
"x": 336.1419982910156,
"y": 195
}
],
"isCurve": true,
@ -2720,24 +2376,8 @@
"link": "",
"route": [
{
"x": 904.9099731445312,
"y": -188
},
{
"x": 911.1920166015625,
"y": -187.9010009765625
},
{
"x": 917.468017578125,
"y": -187.60499572753906
},
{
"x": 923.7310180664062,
"y": -187.11199951171875
},
{
"x": 929.9760131835938,
"y": -186.4219970703125
"x": 931.4099731445312,
"y": -186.21800231933594
},
{
"x": 936.197021484375,
@ -3104,24 +2744,8 @@
"y": 209.53700256347656
},
{
"x": 929.9760131835938,
"y": 210.4219970703125
},
{
"x": 923.7310180664062,
"y": 211.11199951171875
},
{
"x": 917.468017578125,
"y": 211.60499572753906
},
{
"x": 914.9099731445312,
"y": 212
},
{
"x": 904.9099731445312,
"y": 212
"x": 931.4099731445312,
"y": 210.21800231933594
}
],
"isCurve": true,

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 26 KiB