使用 Tikz 实现多条直线和曲线之间的交点

使用 Tikz 实现多条直线和曲线之间的交点

我尝试计算交点,并在名为“mincut”的曲线和名为“arc-67”的线之间添加一个红点。

它不起作用并出现如下错误:! Package pgf Error: No shape named `intersection-1' is known.

尝试文档中提供的更简单的代码,完全没有出现任何错误。这可能是由于加载了太多 Tikz 包而导致的某种兼容性问题吗?

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{adjustbox}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{shapes,arrows,fit,automata}
\usetikzlibrary{arrows.meta, 
                calc, 
                positioning,
                quotes
                }
         
                
\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,
node distance = 20mm and 40mm, 
semithick]
\tikzstyle{every state}=[circle, black, draw=red, very thick]
\tikzset{
  basic/.style={
    draw=blue,
    line width=1pt,
    ->,             % alwways draw arrow tip
    >=stealth,      % style of arrow tip
    shorten >=2pt,  % shorten a bit, so that it doesn't quite
    shorten <=2pt,  % touch the nodes
    preaction={     % provide a wider white background for
      draw=white,   % each arrow for the intersection effect
      line width=3pt,
      -,            % no arrow tip for background
      }
    }
}
\tikzstyle{mincut}=[dashed, green!40!black, very thick,-,line width=3pt]
\tikzstyle{inc_flow}=[basic,line width=3pt]
\tikzstyle{nobasic}=[basic,dashed]


\def\circledarrow#1#2#3{ % #1 Style, #2 Center, #3 Radius
\draw[#1,<-] (#2) +(80:#3) arc(80:-260:#3);
}



\node[state]  (1-1) [               label=left:{\large $(4^-,3) $}] {1};
\node[state]  (4-1) [right= of 1-1,   label=above left:{\large $(2^+,3)  $}] {4};
\node[state]  (2-1) [above=  of 4-1,  label=above:{\large $(s^+,3) $}] {2};
\node[state]  (3-1) [right=of 2-1,    label=above:{\large $  $}] {3};
\node[state]  (5-1) [below= of 3-1,   label=left :{\large $ (2^+,2) $}] {5};
\node[state]  (6-1) [below right=2 and 1.7 of 4-1, label=below:{\large $(4^+,2) $}] {6};
\node[state]  (7-1) [right= of 5-1, label=right:{\large $ $}] {7};

\node[state]  (s-1) [left=8 of 2-1, label=above:{\large $ (-,\infty) $}] {s};
\node[state]  (e-1) [below= of 7-1, label=below:{\large $ $}] {e};

%%%
% grafo con costos relativos
%%%

% \foreach \inode in {s,1,2,3,4,5,6,7,e}{
%         \node[state]  (\inode-1) [below=8 of \inode,   label=left:{\large $ $}] {\inode};
% }


\path[basic] (s-1) edge node[sloped,above]  {\large $5,0,5$} (1-1);
\path[basic] (s-1) edge node[sloped,above]  {\large $3,0,0$} (2-1);
\path[basic,out=30,in=150] (s-1) edge node[sloped,above]  {\large $3,0,2$} (3-1);
\path[basic,out=270,in=180] (s-1) edge node[sloped,above]  {\large $2,0,2$} (6-1);


\path[nobasic] (1-1) edge node[sloped,above]  {\large $2,2$} (2-1);
\path[basic] (1-1) edge node[sloped,above]  {\large $3,0,3$} (4-1);
\path[basic] (1-1) edge node[sloped,above]  {\large $2,0,2$} (6-1);

\path[basic] (2-1) edge node[sloped,above]  {\large $3,0,0$} (4-1);
\path[basic] (2-1) edge node[sloped,above, near end]  {\large $ 2,0,0 $} (5-1);

\path[nobasic,name path=arc-32] (3-1) edge node[sloped,above]  {\large $3,2 $} (2-1);
\path[basic] (3-1) edge node[sloped,above]  {\large $3,0,2$} (7-1);

\path[nobasic,name path=arc-43] (4-1) edge node[sloped,above, near end]  {\large $ 3,1 $} (3-1);
\path[basic] (4-1) edge node[sloped,above]  {\large $ 2,0,0 $} (6-1);
\path[basic] (4-1) edge node[sloped,above,near start]  {\large $ 3,0,3 $} (e-1);

\path[nobasic,name path=arc-57] (5-1) edge node[sloped,above]  {\large $ 4,2 $} (7-1);
\path[basic] (5-1) edge node[sloped,above, near end]  {\large $ 4,0,4 $} (e-1);

\path[basic] (6-1) edge node[sloped,above,near start]  {\large $ 4,0,4 $} (5-1);
\path[nobasic,name path=arc-67] (6-1) edge node[sloped,above, near end]  {\large $ 3,1 $} (7-1);

\path[basic] (7-1) edge node[sloped,above]  {\large $ 9,0,2 $} (e-1);


\draw[mincut,name path=mincut] (12,-4) .. controls (12,0) and (8,2) .. (9,6);

% testing to draw dot at intersections

\fill[red,name intersections={of=mincut and arc-67}]
    (intersection-1) circle (2pt) node {1};

\end{tikzpicture}


\end{document}

答案1

显然,“mincut” 和“arc-67” 边缘不相交(“mincut” 穿过“arc-67” 以关闭其末端或在其末端,这不太明显)。您需要将mincut曲线向左移动一点。

在您的 MWE 中,许多边被绘制了两次,而且样式定义也不一致。所以我擅自重新编码了您的 MWE(从头开始)。您可能喜欢得到的结果:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata,
                intersections,
                positioning,
                quotes
                }

\begin{document}
    \begin{tikzpicture}[auto,
     node distance = 20mm and 30mm,
        dot/.style = {circle, fill=red, inner sep=2pt, node contents={}},
every state/.style = {circle, draw=red, very thick},
 every edge/.style = {draw=blue, line width=1pt, -Stealth,
                      shorten >=2pt, shorten <=2pt,
                      },
 every edge quotes/.style = {auto, sloped, inner sep=2pt},
       every label/.style = {rectangle, inner sep=2pt, font=\large},
     mincut/.style = {draw=green!40!black, line width=3pt, dashed}
                                              ]
\node (1-1) [state, label=left:{$(4^-,3)$}]                        {1};
\node (4-1) [state, right=of 1-1, label=above left:{$(2^+,3)$}]    {4};
\node (2-1) [state, above=of 4-1, label=above:{$(s^+,3)$}]         {2};
\node (3-1) [state, right=of 2-1]  {3};
\node (5-1) [state, below=of 3-1, label=left:{$(2^+,2)$}]          {5};
\node (6-1) [state, below right=of 4-1, label=below:{$(4^+,2)$}]   {6};
\node (7-1) [state, right=of 5-1]  {7};
%
\node (e-1) [state, accepting, below= of 7-1] {e};
\node (s-1) [state, accepting, left=of 1-1 |- 2-1, label={$(-,\infty)$}] {s};
%   edges
\path   (s-1) edge ["{$5,0,5$}"] (1-1)
        (s-1) edge ["{$3,0,0$}"] (2-1)
        (s-1) edge [bend left, "{$3,0,2$}"] (3-1)
        (s-1) edge [out=270,in=180, "{$2,0,2$}"] (6-1)

    (1-1) edge [dashed, "{$2,2$}"] (2-1)
        (1-1) edge ["{$3,0,3$}"] (4-1)
        (1-1) edge ["{$2,0,2$}"] (6-1)

        (2-1) edge ["{$3,0,0$}"] (4-1)
        (2-1) edge [near end, "{$2,0,0$}"] (5-1)

        (3-1) edge ["{$3,0,2$}"]    (7-1)
   (3-1) edge [name path=arc-32, dashed, near end,
               "{$3,2$}"]           (2-1)
        (3-1) edge ["{$3,0,2$}"]    (7-1)

        (4-1) edge ["{$2,0,0$}"]    (6-1)
        (4-1) edge [near start, "{$3,0,3$}"] (e-1)

   (4-1) edge [name path=arc-43, dashed, near start,
                    "{$3,1$}"] (3-1)
    (5-1) edge [name path=arc-57, dashed, 
                "{$4,2 $}"]    (7-1)
        (5-1) edge [near end, "{$ 4,0,4$}"] (e-1)

        (6-1) edge [near start, "{$ 4,0,4$}"] (5-1)
    (6-1) edge [name path=arc-67, dashed, near end,
                "{$3,1$}"]   (7-1)

        (7-1) edge ["{$9,0,2$}"] (e-1);
%         
\path[mincut,name path=mincut] (9,-3.3) .. controls +(2,4) and +(-1,-4) .. (6,5);
%   intersections
    \begin{scope}[every label/.append style = {font=\small\bfseries, text=red}]
\fill[name intersections={of=mincut and arc-67}]
    (intersection-1) node[dot,label=left: 1];
\fill[name intersections={of=mincut and arc-57}]
    (intersection-1) node[dot,label=2];
\fill[name intersections={of=mincut and arc-43}]
    (intersection-1) node[dot,label=3];
\fill[name intersections={of=mincut and arc-32}]
    (intersection-1) node[dot,label=above right:4];
    \end{scope}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容