-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraph.go
121 lines (101 loc) · 2.04 KB
/
graph.go
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
114
115
116
117
118
119
120
121
package layout
type Graph struct {
ID string
Directed bool
// Defaults
LineHeight Length
FontSize Length
Shape Shape
NodePadding Length
RowPadding Length
EdgePadding Length
NodeByID map[string]*Node
Nodes []*Node
Edges []*Edge
}
func NewGraph() *Graph {
graph := &Graph{}
graph.LineHeight = 16 * Point
graph.Shape = Auto
graph.NodeByID = make(map[string]*Node)
return graph
}
func NewDigraph() *Graph {
graph := NewGraph()
graph.Directed = true
return graph
}
// Node finds or creates node with id
func (graph *Graph) Node(id string) *Node {
if id == "" {
panic("invalid node id")
}
node, found := graph.NodeByID[id]
if !found {
node = NewNode(id)
graph.AddNode(node)
}
return node
}
// Edge finds or creates new edge based on ids
func (graph *Graph) Edge(from, to string) *Edge {
source, target := graph.Node(from), graph.Node(to)
for _, edge := range graph.Edges {
if edge.From == source && edge.To == target {
return edge
}
}
edge := NewEdge(source, target)
edge.Directed = graph.Directed
graph.AddEdge(edge)
return edge
}
// AddNode adds a new node.
//
// When a node with the specified id already it will return false
// and the node is not added.
func (graph *Graph) AddNode(node *Node) bool {
if node.ID != "" {
_, found := graph.NodeByID[node.ID]
if found {
return false
}
graph.NodeByID[node.ID] = node
}
graph.Nodes = append(graph.Nodes, node)
return true
}
func (graph *Graph) AddEdge(edge *Edge) {
graph.Edges = append(graph.Edges, edge)
}
func minvector(a *Vector, b Vector) {
if b.X < a.X {
a.X = b.X
}
if b.Y < a.Y {
a.Y = b.Y
}
}
func maxvector(a *Vector, b Vector) {
if b.X > a.X {
a.X = b.X
}
if b.Y > a.Y {
a.Y = b.Y
}
}
func (graph *Graph) Bounds() (min, max Vector) {
for _, node := range graph.Nodes {
minvector(&min, node.TopLeft())
maxvector(&max, node.BottomRight())
}
for _, edge := range graph.Edges {
for _, p := range edge.Path {
minvector(&min, p)
maxvector(&max, p)
}
}
minvector(&min, max)
maxvector(&max, min)
return
}