Repository navigation
Expand file tree
/
Copy pathloop.go
More file actions
113 lines (103 loc) · 3.17 KB
/
Copy pathloop.go
File metadata and controls
113 lines (103 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package layout
import (
"cmp"
"math"
"slices"
)
// loopCount numbers the self-loops without ports of each node, which
// stack down its side
type loopCount struct {
count, index map[*lnode]int
}
func countLoops(edges []*ledge) *loopCount {
loops := &loopCount{count: map[*lnode]int{}, index: map[*lnode]int{}}
for _, edge := range edges {
if edge.loop() && !edge.ported() {
loops.count[edge.From]++
}
}
return loops
}
// next returns the place of the self-loop edge among its node's loops
func (loops *loopCount) next(edge *ledge) int {
if edge.ported() {
return 0
}
k := loops.index[edge.From]
loops.index[edge.From]++
return k
}
func loopPath(edge *ledge, width, height Length, k, n int) Path {
node := edge.From
right := node.Right() + width
// the n loops without ports stack down the right side, loop k in the
// k-th share of it; a single loop spans the middle half
share := 2 * node.Radius.Y / Length(max(n, 1))
top := node.Center.Y - node.Radius.Y + Length(k)*share
up := Vector{X: node.Right(), Y: top + share/4}
down := Vector{X: node.Right(), Y: top + 3*share/4}
if !edge.ported() {
// leave and return horizontally from where the outline is
from, to := node.Boundary(up), node.Boundary(down)
return []Vector{from, {X: right, Y: from.Y}, {X: right, Y: to.Y}, to}
}
// With ports, leave each end straight out to the node box grown by
// width and height, then go around it on the shorter side.
from, to := node.Boundary(up), node.Boundary(down)
if edge.FromPort != CompassAuto {
from = node.CompassPoint(edge.FromPort)
}
if edge.ToPort != CompassAuto {
to = node.CompassPoint(edge.ToPort)
}
outward := func(p Vector) Vector {
d := Vector{X: 1} // the center port leaves to the right
if p != node.Center {
d = p.Sub(node.Center).Unit()
}
// where the ray along d leaves the grown box
t := Length(math.Inf(1))
if d.X != 0 {
t = (node.Radius.X + width) / d.X.Abs()
}
if d.Y != 0 {
t = min(t, (node.Radius.Y+height)/d.Y.Abs())
}
return node.Center.Add(Vector{X: d.X * t, Y: d.Y * t})
}
a, b := outward(from), outward(to)
if from == to {
// both ends on one port: split the loop sideways
d := a.Sub(from)
side := Vector{X: -d.Y / 2, Y: d.X / 2}
return []Vector{from, a.Add(side), a.Sub(side), to}
}
angle := func(p Vector) float64 {
return math.Atan2(float64(p.Y-node.Center.Y), float64(p.X-node.Center.X))
}
start := angle(a)
sweep := math.Remainder(angle(b)-start, 2*math.Pi)
type corner struct {
at float64
p Vector
}
var corners []corner
for _, p := range []Vector{
{X: node.Left() - width, Y: node.Top() - height},
{X: node.Right() + width, Y: node.Top() - height},
{X: node.Right() + width, Y: node.Bottom() + height},
{X: node.Left() - width, Y: node.Bottom() + height},
} {
at := math.Remainder(angle(p)-start, 2*math.Pi)
// strictly between the start and the end of the sweep
if lo, hi := min(0, sweep), max(0, sweep); at > lo && at < hi {
corners = append(corners, corner{math.Abs(at), p})
}
}
slices.SortFunc(corners, func(x, y corner) int { return cmp.Compare(x.at, y.at) })
path := []Vector{from, a}
for _, c := range corners {
path = append(path, c.p)
}
return append(path, b, to)
}