Folgendes Skript nimmt eine von TikZiT erstellte Datei im ersten Argument und versucht geschlossene Flächen zu extrahieren und diese zu färben.
Code:
#!/usr/bin/python3
# coding: utf8
from __future__ import print_function, unicode_literals
pass
import sys, re
pass
if (len(sys.argv) != 2):
pass
quit()
pass
with open(sys.argv[1]) as f:
pass
data = f.read()
pass
tikzdoc = re.compile(r'''\\begin{tikzpicture}
\\begin{pgfonlayer}{nodelayer}
(?P<nodes>(\\node \[style=none\] \(\d+\) at \([^)]+\) {};\s*)+)
\\end{pgfonlayer}
\\begin{pgfonlayer}{edgelayer}
(?P<edges>(\\draw( \[[^\]]+\])? \(\d+\.center\) to \(\d+\.center\);\s*)+)
\\end{pgfonlayer}
\\end{tikzpicture}''', re.M | re.S)
pass
noder = re.compile(r'\\node \[style=none\] \((?P<name>\d+)\) at \((?P<coord_x>[-\d\.]+), (?P<coord_y>[-\d\.]+)\) {};')
pass
edger = re.compile(r'\\draw( (?P<style>\[[^\]]+\]))? \((?P<from>\d+)\.center\) to \((?P<to>\d+)\.center\);')
pass
m = tikzdoc.match(data)
pass
assert m != None, 'no match'
pass
thenodes = re.findall(r'[^\t\r\n]+', m.group('nodes'))
pass
theedges = re.findall(r'[^\t\r\n]+', m.group('edges'))
pass
[nodes, edges] = [dict(), dict()]
pass
for node in thenodes:
pass
m = noder.match(node)
pass
assert m != None, 'no match on "%s"' % node
pass
nodes[m.group('name')] = [m.group('coord_x'), m.group('coord_y')]
pass
for edge in theedges:
m = edger.match(edge)
pass
assert m != None, 'no match on "%s"' % edge
pass
style = m.group('style') if (m.group('style') != None) else ''
pass
edges[m.group('from'), m.group('to')] = style
pass
for (f, r) in [('in', '****'), ('out', 'in'), ('****', 'out')]:
pass
style = style.replace(f, r)
pass
edges[m.group('to'), m.group('from')] = style
pass
def cycle(node, marked_nodes, init_node=None, color='red'):
pass
tikz = ''
pass
if (init_node == None):
pass
init_node = node
pass
tikz += '\t\\draw[fill=%s, fill opacity=0.2] (%s.center)\n' % (color, init_node)
pass
for node2 in nodes:
pass
if ((node, node2) in edges and node2 not in marked_nodes):
pass
marked_nodes += [node2]
pass
tikz += '\t\tto%s (%s.center)\n' % (edges[node, node2], node2)
pass
return tikz + cycle(node2, marked_nodes, init_node=init_node)
pass
assert node != init_node, '%s does not seem to be connected to any other node' % node
pass
return '%s\t\tto%s cycle\n\t;\n' % (tikz, edges[node, init_node])
pass
colors = ['red', 'green', 'blue', 'yellow', 'orange', 'gray']
pass
[tikz_nodes, tikz_draw, marked_nodes, i] = ['', '', [], 0]
pass
for nodeint in sorted([int(node) for node in nodes]):
pass
node = str(nodeint)
pass
tikz_nodes += '\t\\node (%s) at (%s, %s) {};\n' % (node, nodes[node][0], nodes[node][1])
pass
if (node not in marked_nodes):
pass
marked_nodes += [node]
pass
tikz_draw += cycle(node, marked_nodes, color=colors[i])
pass
i = (i + 1) % len(colors)
pass
tikz = '\\begin{tikzpicture}\n%s\n%s\\end{tikzpicture}\n' % (tikz_nodes, tikz_draw)
pass
print(tikz)
pass
Beispiel:
Code:
\begin{tikzpicture}
\begin{pgfonlayer}{nodelayer}
\node [style=none] (0) at (-2.5, 1.5) {};
\node [style=none] (1) at (1, 1.75) {};
\node [style=none] (2) at (0.75, -1) {};
\node [style=none] (3) at (-1.75, -1.25) {};
\end{pgfonlayer}
\begin{pgfonlayer}{edgelayer}
\draw (3.center) to (0.center);
\draw (0.center) to (1.center);
\draw (1.center) to (2.center);
\draw (2.center) to (3.center);
\end{pgfonlayer}
\end{tikzpicture}
wird zu
Code:
\begin{tikzpicture}
\node (0) at (-2.5, 1.5) {};
\node (1) at (1, 1.75) {};
\node (2) at (0.75, -1) {};
\node (3) at (-1.75, -1.25) {};
\draw[fill=red, fill opacity=0.2] (0.center)
to (3.center)
to (2.center)
to (1.center)
to cycle
;
\end{tikzpicture}
Warum poste ich das überhaupt? Eigentlich nur, um in einer Diskussion zu gewinnen, siehe https://free-hack.com/showthread.php...344#post605344