From 6736a70389506a98b08423606cddcd9c3bec4820 Mon Sep 17 00:00:00 2001 From: Mayank Mohapatra <125661248+Mayank77maruti@users.noreply.github.com> Date: Sat, 22 Feb 2025 11:00:58 +0000 Subject: [PATCH] try --- d2layouts/d2cycle/layout.go | 61 +++++++ .../txtar/cycle-diagram/dagre/board.exp.json | 24 +-- .../txtar/cycle-diagram/dagre/sketch.exp.svg | 152 +++++++++--------- .../txtar/cycle-diagram/elk/board.exp.json | 24 +-- .../txtar/cycle-diagram/elk/sketch.exp.svg | 152 +++++++++--------- 5 files changed, 237 insertions(+), 176 deletions(-) diff --git a/d2layouts/d2cycle/layout.go b/d2layouts/d2cycle/layout.go index 0e9036f74..f0786f400 100644 --- a/d2layouts/d2cycle/layout.go +++ b/d2layouts/d2cycle/layout.go @@ -65,6 +65,47 @@ 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 +// } +// createCircularArc creates a circular arc path for the edge and adjusts the last segment to align with the tangent direction. func createCircularArc(edge *d2graph.Edge) { if edge.Src == nil || edge.Dst == nil { return @@ -102,6 +143,26 @@ func createCircularArc(edge *d2graph.Edge) { path = trimPathPoints(path, edge.Src.Box) path = trimPathPoints(path, edge.Dst.Box) + // Adjust the last segment to align with the tangent direction at the destination + if len(path) >= 2 { + last := path[len(path)-1] + // Calculate tangent direction (perpendicular to radius vector) + tangentX := -last.Y + tangentY := last.X + tangentLength := math.Hypot(tangentX, tangentY) + if tangentLength > 0 { + // Normalize tangent direction + tangentDir := geo.NewPoint(tangentX/tangentLength, tangentY/tangentLength) + // Move second-to-last point along the tangent direction + delta := 10.0 + newSecondLast := geo.NewPoint( + last.X-delta*tangentDir.X, + last.Y-delta*tangentDir.Y, + ) + path[len(path)-2] = newSecondLast + } + } + edge.Route = path edge.IsCurve = true } diff --git a/e2etests/testdata/txtar/cycle-diagram/dagre/board.exp.json b/e2etests/testdata/txtar/cycle-diagram/dagre/board.exp.json index 78ed2dffe..f260b0886 100644 --- a/e2etests/testdata/txtar/cycle-diagram/dagre/board.exp.json +++ b/e2etests/testdata/txtar/cycle-diagram/dagre/board.exp.json @@ -864,8 +864,8 @@ "y": -37.47600173950195 }, { - "x": 197.02099609375, - "y": -34.3849983215332 + "x": 195.6020050048828, + "y": -42.86199951171875 }, { "x": 197.2519989013672, @@ -1228,8 +1228,8 @@ "y": 197.53700256347656 }, { - "x": 28.18000030517578, - "y": 198.00399780273438 + "x": 36.4109992980957, + "y": 196.90499877929688 }, { "x": 26.5, @@ -1592,8 +1592,8 @@ "y": 37.47600173950195 }, { - "x": -197.02099609375, - "y": 34.3849983215332 + "x": -195.6020050048828, + "y": 42.86199951171875 }, { "x": -197.2519989013672, @@ -1972,8 +1972,8 @@ "y": 111.8030014038086 }, { - "x": 701.875, - "y": 115.77300262451172 + "x": 704.7830200195312, + "y": 107.5770034790039 }, { "x": 701.4329833984375, @@ -2336,8 +2336,8 @@ "y": 186.90899658203125 }, { - "x": 364.3710021972656, - "y": 183.8260040283203 + "x": 370.2919921875, + "y": 190.46800231933594 }, { "x": 363.6419982910156, @@ -2740,8 +2740,8 @@ "y": 196.45700073242188 }, { - "x": 1003.2860107421875, - "y": 197.53700256347656 + "x": 1008.4110107421875, + "y": 196.89300537109375 }, { "x": 998.5, diff --git a/e2etests/testdata/txtar/cycle-diagram/dagre/sketch.exp.svg b/e2etests/testdata/txtar/cycle-diagram/dagre/sketch.exp.svg index 015478d74..037adcdac 100644 --- a/e2etests/testdata/txtar/cycle-diagram/dagre/sketch.exp.svg +++ b/e2etests/testdata/txtar/cycle-diagram/dagre/sketch.exp.svg @@ -1,9 +1,9 @@ -abcdabcab + .d2-2450559997 .fill-N1{fill:#0A0F25;} + .d2-2450559997 .fill-N2{fill:#676C7E;} + .d2-2450559997 .fill-N3{fill:#9499AB;} + .d2-2450559997 .fill-N4{fill:#CFD2DD;} + .d2-2450559997 .fill-N5{fill:#DEE1EB;} + .d2-2450559997 .fill-N6{fill:#EEF1F8;} + .d2-2450559997 .fill-N7{fill:#FFFFFF;} + .d2-2450559997 .fill-B1{fill:#0D32B2;} + .d2-2450559997 .fill-B2{fill:#0D32B2;} + .d2-2450559997 .fill-B3{fill:#E3E9FD;} + .d2-2450559997 .fill-B4{fill:#E3E9FD;} + .d2-2450559997 .fill-B5{fill:#EDF0FD;} + .d2-2450559997 .fill-B6{fill:#F7F8FE;} + .d2-2450559997 .fill-AA2{fill:#4A6FF3;} + .d2-2450559997 .fill-AA4{fill:#EDF0FD;} + .d2-2450559997 .fill-AA5{fill:#F7F8FE;} + .d2-2450559997 .fill-AB4{fill:#EDF0FD;} + .d2-2450559997 .fill-AB5{fill:#F7F8FE;} + .d2-2450559997 .stroke-N1{stroke:#0A0F25;} + .d2-2450559997 .stroke-N2{stroke:#676C7E;} + .d2-2450559997 .stroke-N3{stroke:#9499AB;} + .d2-2450559997 .stroke-N4{stroke:#CFD2DD;} + .d2-2450559997 .stroke-N5{stroke:#DEE1EB;} + .d2-2450559997 .stroke-N6{stroke:#EEF1F8;} + .d2-2450559997 .stroke-N7{stroke:#FFFFFF;} + .d2-2450559997 .stroke-B1{stroke:#0D32B2;} + .d2-2450559997 .stroke-B2{stroke:#0D32B2;} + .d2-2450559997 .stroke-B3{stroke:#E3E9FD;} + .d2-2450559997 .stroke-B4{stroke:#E3E9FD;} + .d2-2450559997 .stroke-B5{stroke:#EDF0FD;} + .d2-2450559997 .stroke-B6{stroke:#F7F8FE;} + .d2-2450559997 .stroke-AA2{stroke:#4A6FF3;} + .d2-2450559997 .stroke-AA4{stroke:#EDF0FD;} + .d2-2450559997 .stroke-AA5{stroke:#F7F8FE;} + .d2-2450559997 .stroke-AB4{stroke:#EDF0FD;} + .d2-2450559997 .stroke-AB5{stroke:#F7F8FE;} + .d2-2450559997 .background-color-N1{background-color:#0A0F25;} + .d2-2450559997 .background-color-N2{background-color:#676C7E;} + .d2-2450559997 .background-color-N3{background-color:#9499AB;} + .d2-2450559997 .background-color-N4{background-color:#CFD2DD;} + .d2-2450559997 .background-color-N5{background-color:#DEE1EB;} + .d2-2450559997 .background-color-N6{background-color:#EEF1F8;} + .d2-2450559997 .background-color-N7{background-color:#FFFFFF;} + .d2-2450559997 .background-color-B1{background-color:#0D32B2;} + .d2-2450559997 .background-color-B2{background-color:#0D32B2;} + .d2-2450559997 .background-color-B3{background-color:#E3E9FD;} + .d2-2450559997 .background-color-B4{background-color:#E3E9FD;} + .d2-2450559997 .background-color-B5{background-color:#EDF0FD;} + .d2-2450559997 .background-color-B6{background-color:#F7F8FE;} + .d2-2450559997 .background-color-AA2{background-color:#4A6FF3;} + .d2-2450559997 .background-color-AA4{background-color:#EDF0FD;} + .d2-2450559997 .background-color-AA5{background-color:#F7F8FE;} + .d2-2450559997 .background-color-AB4{background-color:#EDF0FD;} + .d2-2450559997 .background-color-AB5{background-color:#F7F8FE;} + .d2-2450559997 .color-N1{color:#0A0F25;} + .d2-2450559997 .color-N2{color:#676C7E;} + .d2-2450559997 .color-N3{color:#9499AB;} + .d2-2450559997 .color-N4{color:#CFD2DD;} + .d2-2450559997 .color-N5{color:#DEE1EB;} + .d2-2450559997 .color-N6{color:#EEF1F8;} + .d2-2450559997 .color-N7{color:#FFFFFF;} + .d2-2450559997 .color-B1{color:#0D32B2;} + .d2-2450559997 .color-B2{color:#0D32B2;} + .d2-2450559997 .color-B3{color:#E3E9FD;} + .d2-2450559997 .color-B4{color:#E3E9FD;} + .d2-2450559997 .color-B5{color:#EDF0FD;} + .d2-2450559997 .color-B6{color:#F7F8FE;} + .d2-2450559997 .color-AA2{color:#4A6FF3;} + .d2-2450559997 .color-AA4{color:#EDF0FD;} + .d2-2450559997 .color-AA5{color:#F7F8FE;} + .d2-2450559997 .color-AB4{color:#EDF0FD;} + .d2-2450559997 .color-AB5{color:#F7F8FE;}.appendix text.text{fill:#0A0F25}.md{--color-fg-default:#0A0F25;--color-fg-muted:#676C7E;--color-fg-subtle:#9499AB;--color-canvas-default:#FFFFFF;--color-canvas-subtle:#EEF1F8;--color-border-default:#0D32B2;--color-border-muted:#0D32B2;--color-neutral-muted:#EEF1F8;--color-accent-fg:#0D32B2;--color-accent-emphasis:#0D32B2;--color-attention-subtle:#676C7E;--color-danger-fg:red;}.sketch-overlay-B1{fill:url(#streaks-darker-d2-2450559997);mix-blend-mode:lighten}.sketch-overlay-B2{fill:url(#streaks-darker-d2-2450559997);mix-blend-mode:lighten}.sketch-overlay-B3{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-B4{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-B5{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-B6{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-AA2{fill:url(#streaks-dark-d2-2450559997);mix-blend-mode:overlay}.sketch-overlay-AA4{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-AA5{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-AB4{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-AB5{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-N1{fill:url(#streaks-darker-d2-2450559997);mix-blend-mode:lighten}.sketch-overlay-N2{fill:url(#streaks-dark-d2-2450559997);mix-blend-mode:overlay}.sketch-overlay-N3{fill:url(#streaks-normal-d2-2450559997);mix-blend-mode:color-burn}.sketch-overlay-N4{fill:url(#streaks-normal-d2-2450559997);mix-blend-mode:color-burn}.sketch-overlay-N5{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-N6{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.sketch-overlay-N7{fill:url(#streaks-bright-d2-2450559997);mix-blend-mode:darken}.light-code{display: block}.dark-code{display: none}]]>abcdabcab diff --git a/e2etests/testdata/txtar/cycle-diagram/elk/board.exp.json b/e2etests/testdata/txtar/cycle-diagram/elk/board.exp.json index 6eb058a84..94712ff99 100644 --- a/e2etests/testdata/txtar/cycle-diagram/elk/board.exp.json +++ b/e2etests/testdata/txtar/cycle-diagram/elk/board.exp.json @@ -864,8 +864,8 @@ "y": -25.47599983215332 }, { - "x": 209.02099609375, - "y": -22.385000228881836 + "x": 207.6020050048828, + "y": -30.86199951171875 }, { "x": 209.2519989013672, @@ -1228,8 +1228,8 @@ "y": 209.53700256347656 }, { - "x": 40.18000030517578, - "y": 210.00399780273438 + "x": 48.4109992980957, + "y": 208.90499877929688 }, { "x": 38.5, @@ -1592,8 +1592,8 @@ "y": 49.47600173950195 }, { - "x": -185.02099609375, - "y": 46.3849983215332 + "x": -183.6020050048828, + "y": 54.86199951171875 }, { "x": -185.2519989013672, @@ -1972,8 +1972,8 @@ "y": 123.8030014038086 }, { - "x": 674.375, - "y": 127.77300262451172 + "x": 677.2830200195312, + "y": 119.5770034790039 }, { "x": 673.9329833984375, @@ -2336,8 +2336,8 @@ "y": 198.90899658203125 }, { - "x": 336.8710021972656, - "y": 195.8260040283203 + "x": 342.7919921875, + "y": 202.46800231933594 }, { "x": 336.1419982910156, @@ -2740,8 +2740,8 @@ "y": 208.45700073242188 }, { - "x": 936.197021484375, - "y": 209.53700256347656 + "x": 941.3209838867188, + "y": 208.89300537109375 }, { "x": 931.4099731445312, diff --git a/e2etests/testdata/txtar/cycle-diagram/elk/sketch.exp.svg b/e2etests/testdata/txtar/cycle-diagram/elk/sketch.exp.svg index 21e65f752..c894e3d67 100644 --- a/e2etests/testdata/txtar/cycle-diagram/elk/sketch.exp.svg +++ b/e2etests/testdata/txtar/cycle-diagram/elk/sketch.exp.svg @@ -1,9 +1,9 @@ -abcdabcab + .d2-3675916959 .fill-N1{fill:#0A0F25;} + .d2-3675916959 .fill-N2{fill:#676C7E;} + .d2-3675916959 .fill-N3{fill:#9499AB;} + .d2-3675916959 .fill-N4{fill:#CFD2DD;} + .d2-3675916959 .fill-N5{fill:#DEE1EB;} + .d2-3675916959 .fill-N6{fill:#EEF1F8;} + .d2-3675916959 .fill-N7{fill:#FFFFFF;} + .d2-3675916959 .fill-B1{fill:#0D32B2;} + .d2-3675916959 .fill-B2{fill:#0D32B2;} + .d2-3675916959 .fill-B3{fill:#E3E9FD;} + .d2-3675916959 .fill-B4{fill:#E3E9FD;} + .d2-3675916959 .fill-B5{fill:#EDF0FD;} + .d2-3675916959 .fill-B6{fill:#F7F8FE;} + .d2-3675916959 .fill-AA2{fill:#4A6FF3;} + .d2-3675916959 .fill-AA4{fill:#EDF0FD;} + .d2-3675916959 .fill-AA5{fill:#F7F8FE;} + .d2-3675916959 .fill-AB4{fill:#EDF0FD;} + .d2-3675916959 .fill-AB5{fill:#F7F8FE;} + .d2-3675916959 .stroke-N1{stroke:#0A0F25;} + .d2-3675916959 .stroke-N2{stroke:#676C7E;} + .d2-3675916959 .stroke-N3{stroke:#9499AB;} + .d2-3675916959 .stroke-N4{stroke:#CFD2DD;} + .d2-3675916959 .stroke-N5{stroke:#DEE1EB;} + .d2-3675916959 .stroke-N6{stroke:#EEF1F8;} + .d2-3675916959 .stroke-N7{stroke:#FFFFFF;} + .d2-3675916959 .stroke-B1{stroke:#0D32B2;} + .d2-3675916959 .stroke-B2{stroke:#0D32B2;} + .d2-3675916959 .stroke-B3{stroke:#E3E9FD;} + .d2-3675916959 .stroke-B4{stroke:#E3E9FD;} + .d2-3675916959 .stroke-B5{stroke:#EDF0FD;} + .d2-3675916959 .stroke-B6{stroke:#F7F8FE;} + .d2-3675916959 .stroke-AA2{stroke:#4A6FF3;} + .d2-3675916959 .stroke-AA4{stroke:#EDF0FD;} + .d2-3675916959 .stroke-AA5{stroke:#F7F8FE;} + .d2-3675916959 .stroke-AB4{stroke:#EDF0FD;} + .d2-3675916959 .stroke-AB5{stroke:#F7F8FE;} + .d2-3675916959 .background-color-N1{background-color:#0A0F25;} + .d2-3675916959 .background-color-N2{background-color:#676C7E;} + .d2-3675916959 .background-color-N3{background-color:#9499AB;} + .d2-3675916959 .background-color-N4{background-color:#CFD2DD;} + .d2-3675916959 .background-color-N5{background-color:#DEE1EB;} + .d2-3675916959 .background-color-N6{background-color:#EEF1F8;} + .d2-3675916959 .background-color-N7{background-color:#FFFFFF;} + .d2-3675916959 .background-color-B1{background-color:#0D32B2;} + .d2-3675916959 .background-color-B2{background-color:#0D32B2;} + .d2-3675916959 .background-color-B3{background-color:#E3E9FD;} + .d2-3675916959 .background-color-B4{background-color:#E3E9FD;} + .d2-3675916959 .background-color-B5{background-color:#EDF0FD;} + .d2-3675916959 .background-color-B6{background-color:#F7F8FE;} + .d2-3675916959 .background-color-AA2{background-color:#4A6FF3;} + .d2-3675916959 .background-color-AA4{background-color:#EDF0FD;} + .d2-3675916959 .background-color-AA5{background-color:#F7F8FE;} + .d2-3675916959 .background-color-AB4{background-color:#EDF0FD;} + .d2-3675916959 .background-color-AB5{background-color:#F7F8FE;} + .d2-3675916959 .color-N1{color:#0A0F25;} + .d2-3675916959 .color-N2{color:#676C7E;} + .d2-3675916959 .color-N3{color:#9499AB;} + .d2-3675916959 .color-N4{color:#CFD2DD;} + .d2-3675916959 .color-N5{color:#DEE1EB;} + .d2-3675916959 .color-N6{color:#EEF1F8;} + .d2-3675916959 .color-N7{color:#FFFFFF;} + .d2-3675916959 .color-B1{color:#0D32B2;} + .d2-3675916959 .color-B2{color:#0D32B2;} + .d2-3675916959 .color-B3{color:#E3E9FD;} + .d2-3675916959 .color-B4{color:#E3E9FD;} + .d2-3675916959 .color-B5{color:#EDF0FD;} + .d2-3675916959 .color-B6{color:#F7F8FE;} + .d2-3675916959 .color-AA2{color:#4A6FF3;} + .d2-3675916959 .color-AA4{color:#EDF0FD;} + .d2-3675916959 .color-AA5{color:#F7F8FE;} + .d2-3675916959 .color-AB4{color:#EDF0FD;} + .d2-3675916959 .color-AB5{color:#F7F8FE;}.appendix text.text{fill:#0A0F25}.md{--color-fg-default:#0A0F25;--color-fg-muted:#676C7E;--color-fg-subtle:#9499AB;--color-canvas-default:#FFFFFF;--color-canvas-subtle:#EEF1F8;--color-border-default:#0D32B2;--color-border-muted:#0D32B2;--color-neutral-muted:#EEF1F8;--color-accent-fg:#0D32B2;--color-accent-emphasis:#0D32B2;--color-attention-subtle:#676C7E;--color-danger-fg:red;}.sketch-overlay-B1{fill:url(#streaks-darker-d2-3675916959);mix-blend-mode:lighten}.sketch-overlay-B2{fill:url(#streaks-darker-d2-3675916959);mix-blend-mode:lighten}.sketch-overlay-B3{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-B4{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-B5{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-B6{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-AA2{fill:url(#streaks-dark-d2-3675916959);mix-blend-mode:overlay}.sketch-overlay-AA4{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-AA5{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-AB4{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-AB5{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-N1{fill:url(#streaks-darker-d2-3675916959);mix-blend-mode:lighten}.sketch-overlay-N2{fill:url(#streaks-dark-d2-3675916959);mix-blend-mode:overlay}.sketch-overlay-N3{fill:url(#streaks-normal-d2-3675916959);mix-blend-mode:color-burn}.sketch-overlay-N4{fill:url(#streaks-normal-d2-3675916959);mix-blend-mode:color-burn}.sketch-overlay-N5{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-N6{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.sketch-overlay-N7{fill:url(#streaks-bright-d2-3675916959);mix-blend-mode:darken}.light-code{display: block}.dark-code{display: none}]]>abcdabcab