如何移动连接两个节点的边?

如何移动连接两个节点的边?

我预定义了节点,现在边连接节点如下:

\documentclass {scrartcl}
\usepackage{tikz}
\begin{document}
\begin{center}

\begin{tikzpicture}  
    \tikzstyle{vertex} = [ color=black, text=black, fill=black!10]
    \tikzstyle{edge} = [thick]
    
  
    % place nodes
    \node[draw]  [vertex] at (0, 3)   (a) {Node 6};
    \node[draw] [vertex] at (1, 4)   (b) {Node 12};
    \node[draw] [vertex] at (2, 3)  (c)     {Node 19};
    \node[draw] [vertex] at (3, 1)  (e)     {Node 20};
    \node[draw] [vertex] at (4,3)  (d)     {Node 18};
    \node[draw] [vertex] at (4,7)  (f)     {Node 10};
    \node[draw] [vertex] at (6,1)  (g)     {Node 4};
    \node[draw] [vertex] at (6,3)  (h)     {Node 2};
    \node[draw] [vertex] at (6,6)  (i)     {Node 13};
    \node[draw] [vertex] at (6,8)  (j)     {Node 14};
    \node[draw] [vertex] at (7,3)  (k)     {Node 21};
    \node[draw] [vertex] at (7,4)  (l)     {Node 1};
    \node[draw] [vertex] at (7,7)  (m)     {Node 16};
    \node[draw] [vertex] at (8,3)  (n)     {Node 3};
    \node[draw] [vertex] at (8,9)  (o)     {Node 8};
    \node[draw] [vertex] at (9,2)  (p)     {Node 5};
    \node[draw] [vertex] at (9,5)  (q)     {Node 9};
    \node[draw] [vertex] at (5,1)  (r)     {Node 11};
    \node[draw] [vertex] at (7,-1)  (s)     {Node 7};
    \node[draw] [vertex] at (8,-3)  (t)     {Node 17};
    \node[draw] [vertex] at (9,-5)  (u)     {Node 22};
    \node[draw] [vertex] at (5,-3)  (v)     {Node 15};
    

%Draw edges 
\draw[edge][color=red] (o)--(m)--(i)--(h)--(d)--(e)--(r)--(v);
\draw[edge][color=purple] (j)--(i)--(h)--(g)--(s)--(t)--(u);
\draw[edge][color=blue] (f)--(i)--(h)--(k)--(n)--(q);
\draw[edge][color=green] (l)--(h)--(d)--(b);
\draw[edge][color=yellow] (b)--(d)--(e)--(r)--(g)--(s)--(t)--(u);




    \end{tikzpicture}
    \end{center}
\end{document}



\end{document}

当两条边连接相同的节点时,我希望看到两条线,而不是一条线,所以我想将一条线移动几毫米。我该如何实现?

答案1

[经过进一步测试,从下面的评论中扩展:]可以使用xshift和来移动起点和终点yshift,也可以移动北/南/东/西(NSWE)锚点(您正在将这些点“锚定”到顶点的 NSEW 侧):

\draw[edge, color=red] ([xshift=1pt]i.south)--([xshift=1pt]h.north);
\draw[edge, color=purple] (i)--(h);
\draw[edge, color=blue] ([xshift=-1pt]i.south)--([xshift=-1pt]h.north);

上述代码将使红线向右移动 1pt,使蓝线向左移动 1pt。

这样做的一个缺点是,您可能必须单独指定每个移位路径,而不必指定一般路径。在下面的示例中,移位路径以虚线图案绘制。

然后,我提到了positioningtikz 库。你可以使用相对定位来避免节点重叠;下图是核心的一个简单示例:

    \node[vertex] (h) at (0,0) {Node 2h};
    \node[vertex] (i) [above= 3cm of h] {Node 13i};
    \node[vertex] (f) [above left= of i] {node 10f};
    \node[vertex] (j) [above= of i] {Node 14j};
    \node[vertex] (k) [right= of h] {Node 21k};
    \node[vertex] (l) [above right= of h] {Node 1l};
    \node[vertex] (m) [above right= of i] {Node 16m};
    \node[vertex] (n) [right= of k] {Node 3n};
    \node[vertex] (g) [below= of h] {Node 4g};
    \node[vertex] (d) [left=of h] {Node 18d};
    
%%%%%% Paths are grouped by color %%%%%%%

    \draw[edge, color=red] (m) -- (i);
    \draw[edge, color=red, dashed] ([xshift=1pt]i.south) -- ([xshift=1pt]h.north);
    \draw[edge, color=red, dashed] ([yshift=1pt]h.west) -- ([yshift=1pt]d.east);
    
    \draw[edge, color=purple] (j) -- (i) -- (h) -- (g);
    
    \draw[edge, color=blue] (f) -- (i);
    \draw[edge, color=blue, dashed] ([xshift=-1pt]i.south) -- ([xshift=-1pt]h.north);
    \draw[edge, color=blue] (h) -- (k) -- (n);
    
    \draw[edge, color=green] (l) -- (h) -- (d);

给出下图:

这是一个令人紧张的人物!

可以看出,节点之间的距离几乎总是相同的,除了指定了距离的节点 13i(任何单位都可以)。您还可以通过在[node distance = .5em, auto]后面添加 来为整个图形设置此距离\begin{tikzpicture}

我希望这涵盖了所有内容!

相关内容