summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/examples/SimpleEdgeDemo.lua
blob: 8eb9d7d70194a84ab07f2781cce63a47c6fde182 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
-- Copyright 2010 by Renée Ahrens, Olof Frahm, Jens Kluttig, Matthias Schulz, Stephan Schuster
-- Copyright 2012 by Till Tantau
--
-- This file may be distributed an/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License
--
-- See the file doc/generic/pgf/licenses/LICENSE for more information

-- @release $Header$


---
-- @section subsubsection {How To Generate Edges Inside an Algorithm}
--
-- @end


-- Imports
local InterfaceToAlgorithms = require "pgf.gd.interface.InterfaceToAlgorithms"
local declare               = InterfaceToAlgorithms.declare

-- The class object
local SimpleEdgeDemo = {}


---
declare {
  key = "simple edge demo layout",
  algorithm = SimpleEdgeDemo,

  summary = "This algorithm shows how edges can be created by an algorithm.",
  documentation = [["  
       For its job, the algorithm uses the function |createEdge|, which can be
       called during the run of the algorithm to create edges in the
       syntactic graph. The algorithm first does exactly the same as the
       simple demo layout, then it creates an edge for every node where the
       |new edge to| option is set. You will see in the code how this
       option is declared and how we use it to look up a vertex in the
       graph by its name. 
      
\begin{codeexample}[]
\tikz [simple edge demo layout]
\graph [radius=2cm] {
  a, b, c, d, e, f;
        
  e -> [red] f; % normal edge
  
  % Edges generated by the algorithm:
  a[new edge to=b]; 
  b[new edge to=d];
  c[new edge to=f];
};
\end{codeexample}
      
       And the algorithm:
\begin{codeexample}[code only, tikz syntax=false]
 -- File pgf.gd.examples.SimpleEdgeDemo
       
 -- Imports
 local InterfaceToAlgorithms = require "pgf.gd.interface.InterfaceToAlgorithms"
 local declare               = InterfaceToAlgorithms.declare

 -- The class object
 local SimpleEdgeDemo = {}

declare {
  key = "simple edge demo layout",
  algorithm = SimpleEdgeDemo,
  summary = "This algorithm shows...",
}
\end{codeexample}
       
       Next comes the declaration of the new option |new edge to|:
      
\begin{codeexample}[code only, tikz syntax=false]
declare {
  key = "new edge to",
  type = "string",
  summary = "This option takes the name of a vertex..."
}
\end{codeexample}
      
       Finally, the algorithm's code:
      
\begin{codeexample}[code only, tikz syntax=false]
function SimpleEdgeDemo:run()
  -- As in a SimpleDemo:
  ...
  -- Now add some edges:
  for _,tail in ipairs(g.vertices) do
    local name = tail.options['new edge to']
    if name then
      local node = InterfaceToAlgorithms.findVertexByName(name)
      if node and self.digraph:contains(node) then
        InterfaceToAlgorithms.createEdge (self, tail, node)
      end
    end
  end
end
\end{codeexample}
  "]]
}

---
declare {
  key = "new edge to",
  type = "string",

  summary = [["  
       This option takes the name of a vertex. An edge leading to this
       vertex is added to the syntactic digraph.
   "]]
}


function SimpleEdgeDemo:run()
  
  -- As in a SimpleDemo:
  local g = self.digraph
  local alpha = (2 * math.pi) / #g.vertices

  for i,vertex in ipairs(g.vertices) do
    local radius = vertex.options['radius'] or g.options['radius']
    vertex.pos.x = radius * math.cos(i * alpha)
    vertex.pos.y = radius * math.sin(i * alpha)
  end

  -- Now add some edges:
  for _,tail in ipairs(g.vertices) do
    local name = tail.options['new edge to']
    if name then
      local node = InterfaceToAlgorithms.findVertexByName(name)
      if node and self.digraph:contains(node) then
        InterfaceToAlgorithms.createEdge (self, tail, node)
      end
    end
  end
end

return SimpleEdgeDemo