tikz - 如何在图形中弯曲直线

tikz - 如何在图形中弯曲直线

在此处输入图片描述

因此,我尝试绘制带有路线的图表。我有两个问题:

  1. 我怎样才能弯曲线条?我希望 9->4 绕过节点 7。
  2. 我如何更改节点的名称?我希望节点 9 被称为“0”。

我选择的方法来做出这些改变是否简单?

\begin{tikzpicture}[scale=.1,vertex/.style={draw,circle}, arc/.style={draw,thick,->}]
\foreach [count=\i] \coord in {(40,30),(90,10),(20,70),(100,20),(90,20),(70,30),(70,40),(10,70),(50,50)}{
    \node[vertex] (p\i) at \coord {\i};
}
\foreach [count=\r] \row in {{0,0,0,0,0,0,0,0,1},{0,0,0,0,1,0,0,0,0},{1,0,0,0,0,0,0,0,0},{0,1,0,0,0,0,0,0,0},{0,0,0,0,0,1,0,0,0},{0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,0,1},{0,0,1,0,0,0,0,0,0},{0,0,0,1,0,0,0,1,0}}{
    \foreach [count=\c] \cell in \row{
        \ifnum\cell=1%
            \draw[arc] (p\r) edge (p\c);
        \fi
    }
} 
\end{tikzpicture}

答案1

编辑:使用图表库添加了更简单的语法

需要注意的是,手册 3.0.1a 中有一个印刷错误,密钥use existing nodes必须以复数形式(在节点前加一个 s)书写,而不是像手册中那样以单数形式书写。我刚刚为此创建了一张票:#494 印刷错误use existing node

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}[scale=1,every node/.style={draw,circle}]
\foreach \coord [count=\i from 0]  in {(5,5),(4,3),(9,1),(2,7),(10,2),(9,2),(7,3),(7,4),(1,7)}{
    \node (p\i) at \coord {\i};
}
\graph [use existing nodes,edges={thick}]
{
p0->p8->p3->p1->p0->[bend left]p4->p2->p5->p6->p7->p0;
};
\end{tikzpicture}
\end{document}

旧答案:

foreach我利用循环和库的固有可能性,以更简单的方式对您的图形进行了编码chains

可以只进行一次循环:定义节点的循环。要将节点 9(5,5)命名为 0,只需将其放在 foreach 的开头并从零开始计数即可。

\foreach \coord [count=\i from 0]  in {(5,5),(4,3),(9,1),(2,7),(10,2),(9,2),(7,3),(7,4),(1,7)}{
    \node[vertex] (p\i) at \coord {\i};
}

然后使用链库用箭头连接节点。

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[start chain,scale=1,every node/.style={draw,circle}, every join/.style={draw,thick,->}]
\foreach \coord [count=\i from 0]  in {(5,5),(4,3),(9,1),(2,7),(10,2),(9,2),(7,3),(7,4),(1,7)}{
    \node (p\i) at \coord {\i};
}
\chainin (p0);
\chainin (p8)[join];
\chainin (p3)[join];
\chainin (p1)[join];
\chainin (p0)[join];
\chainin (p4)[join=with p0 by {bend left}];
\chainin (p2)[join];
\chainin (p5)[join];
\chainin (p6)[join];
\chainin (p7)[join];
\chainin (p0)[join];
\end{tikzpicture}
\end{document}

查恩

答案2

欢迎来到 TeX.SE!我不得不重新调整您的坐标以避免dimension too large错误,但图表看起来好像我没有这样做。

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[vertex/.style={draw,circle}, arc/.style={draw,thick,->}]
\foreach [count=\i] \coord in {(4,3),(9,1),(2,7),(10,2),(9,2),(7,3),(7,4),(1,7),(5,5)}{
    \ifnum\i=9
    \node[vertex,alias=p0] (p\i) at \coord {0};
    \else
    \node[vertex] (p\i) at \coord {\i};
    \fi
}
\foreach [count=\r] \row in {{0,0,0,0,0,0,0,0,1},{0,0,0,0,1,0,0,0,0},{1,0,0,0,0,0,0,0,0},{0,1,0,0,0,0,0,0,0},{0,0,0,0,0,1,0,0,0},{0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,0,1},{0,0,1,0,0,0,0,0,0},{0,0,0,1,0,0,0,1,0}}{
    \foreach [count=\c] \cell in \row{
        \ifnum\cell=1%
            \ifnum\r>\c
             \pgfmathtruncatemacro{\itest}{\r+\c*10}
             \ifnum\itest=49
              \draw[arc] (p\r) edge[bend left] (p\c);
             \else
              \draw[arc] (p\r) edge (p\c);
             \fi
            \else
             \draw[arc] (p\r) edge (p\c);
            \fi
        \fi
    }
} 
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容