我对 TikZ 还不太熟悉,对 TikZ 宏更是一窍不通。我想修改另一个线程,目标是实现节点循环,但能够将入角和出角作为宏的参数输入。类似以下代码:
\documentclass[tikz]{standalone}
% Weighted graph from adjacency matrix
\newcommand{\weigthgraphfromadj}[5][draw,->]{
\foreach [count=\r] \row in {#3}{
\foreach [count=\c] \cell in \row{
\ifnum\cell>0
\ifnum\c=\r
\draw[arc/.try=\cell] (#2\r) edge[loop arc/.try=\c , loop arc/.style={in=#4\r , out=#5\r}] node[arc label/.try=\cell]{\cell} (#2\c);
\else
\draw[arc/.try=\cell, #1] (#2\r) edge node[arc label/.try=\cell]{\cell} (#2\c);
\fi
\fi
}
}
}
\begin{document}
\begin{tikzpicture}[scale=4,
vertex/.style={draw,circle},
arc/.style={draw=blue!#10,thick,->},
arc label/.style={fill=white, font=\tiny, inner sep=1pt},
loop arc/.style={min distance=2.5mm}
]
\foreach [count=\i] \coord in {(0.809,0.588),(0.309,0.951),(-0.309,0.951),(-0.809,0.588),(-1.,0.),(-0.809,-0.588),(-0.309,-0.951),(0.309,-0.951),(0.809,-0.588),(1.,0.)}{
\node[vertex] (p\i) at \coord {\i};
}
\weigthgraphfromadj[bend left=10]{p}
{{5,5,0,0,1,0,5,0,0,5},{2,5,1,0,0,5,0,2,0,0},{0,5,5,2,0,0,2,0,5,0},{0,0,7,5,5,0,0,2,0,5},{7,0,0,7,5,5,0,0,1,0},{0,5,0,0,2,5,5,0,0,1},{2,0,5,0,0,1,5,5,0,0},{0,7,0,5,0,0,2,5,1,0},{0,0,5,0,7,0,0,5,5,1},{5,0,0,5,0,1,0,0,1,5}}{{0,36,72,108,144,180,216,252,288,324}}{{30,66,102,138,174,210,246,282,318,354}}
\end{tikzpicture}
\end{document}
该宏采用图邻接矩阵来绘制有向图,但是,循环的角度应该适应顶点位置,而前面的帖子中并非如此。
提前感谢您的帮助。
罗曼。
答案1
\documentclass[tikz]{standalone}
\usepackage{luatex85}
% Weighted graph from adjacency matrix
\newcommand{\weigthgraphfromadj}[5][draw,->]{
\foreach [count=\r] \row in {#3}{
\foreach [count=\c] \cell in \row{
\ifnum\cell>0
\ifnum\c=\r
\pgfmathsetmacro{\In}{36*\r-45}
\pgfmathsetmacro{\Out}{36*\r+45}
\draw[arc/.try=\cell] (#2\r)
edge[loop arc,in=\In , out=\Out]
node[arc label/.try=\cell]{\cell} (#2\c);
\else
\draw[arc/.try=\cell, #1] (#2\r) edge node[arc label/.try=\cell]{\cell} (#2\c);
\fi
\fi
}
}
}
\begin{document}
\begin{tikzpicture}[scale=4,
vertex/.style={draw,circle},
arc/.style={draw=blue!#10,thick,->},
arc label/.style={fill=white, font=\tiny, inner sep=1pt},
loop arc/.style={min distance=2.5mm}
]
\foreach [count=\i] \coord in {(0.809,0.588),(0.309,0.951),(-0.309,0.951),(-0.809,0.588),(-1.,0.),(-0.809,-0.588),(-0.309,-0.951),(0.309,-0.951),(0.809,-0.588),(1.,0.)}{
\node[vertex] (p\i) at \coord {\i};
}
\weigthgraphfromadj[bend left=10]{p}
{{5,5,0,0,1,0,5,0,0,5},{2,5,1,0,0,5,0,2,0,0},{0,5,5,2,0,0,2,0,5,0},{0,0,7,5,5,0,0,2,0,5},{7,0,0,7,5,5,0,0,1,0},{0,5,0,0,2,5,5,0,0,1},{2,0,5,0,0,1,5,5,0,0},{0,7,0,5,0,0,2,5,1,0},{0,0,5,0,7,0,0,5,5,1},{5,0,0,5,0,1,0,0,1,5}}{{0,36,72,108,144,180,216,252,288,324}}{{120,66,102,138,174,210,246,282,318,354}}
\end{tikzpicture}
\end{document}
答案2
这就是我要找的,参数 #4 是顶点之间的角度,参数 #5 是在和出去循环的参数。
\documentclass[tikz]{standalone}
% Weighted graph from adjacency matrix
\newcommand{\weigthgraphfromadj}[5][draw,->]{
\foreach [count=\r] \row in {#3}{
\foreach [count=\c] \cell in \row{
\ifnum\cell>0
\ifnum\c=\r
\draw[arc/.try=\cell] (#2\r) edge[loop arc, in= #4*\r -#5 , out= #4*\r +#5] node[arc label/.try=\cell]{\cell} (#2\c);
\else
\draw[arc/.try=\cell, #1] (#2\r) edge node[arc label/.try=\cell]{\cell} (#2\c);
\fi
\fi
}
}
}
\begin{document}
\begin{tikzpicture}[scale=4,
vertex/.style={draw,circle},
arc/.style={draw=blue!#10,thick,->},
arc label/.style={fill=white, font=\tiny, inner sep=1pt},
loop arc/.style={min distance=2.5mm}
]
\foreach [count=\i] \coord in {(0.809,0.588),(0.309,0.951),(-0.309,0.951),(-0.809,0.588),(-1.,0.),(-0.809,-0.588),(-0.309,-0.951),(0.309,-0.951),(0.809,-0.588),(1.,0.)}{
\node[vertex] (p\i) at \coord {\i};
}
\weigthgraphfromadj[bend left=10]{p}
{{5,5,0,0,1,0,5,0,0,5},{2,5,1,0,0,5,0,2,0,0},{0,5,5,2,0,0,2,0,5,0},{0,0,7,5,5,0,0,2,0,5},{7,0,0,7,5,5,0,0,1,0},{0,5,0,0,2,5,5,0,0,1},{2,0,5,0,0,1,5,5,0,0},{0,7,0,5,0,0,2,5,1,0},{0,0,5,0,7,0,0,5,5,1},{5,0,0,5,0,1,0,0,1,5}}{36}{20}
\end{tikzpicture}
\end{document}
谢谢
罗曼