在 TikZ 中沿路径绘制

在 TikZ 中沿路径绘制

B对于下面的图像和代码,如何才能使从到X和从X到 的路径完全沿着从到C绘制的红色路径延伸?BC

我设法找到了红色路径和节点的交点X,但我不知道如何找到和的入/出角度XC而且我不知道即使我有角度,是否也需要设置张力之类的东西。要么根据红色路径和节点的明确计算来做某事,X要么采用一些更通用/神奇的解决方案。

在此处输入图片描述

代码:

\documentclass[margin=6]{standalone}

\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}

\begin{tikzpicture}[
  thick,
  every node/.style={
    draw,
    circle,
    minimum size=1cm,
  }
  ]
  \def\sep{4cm}
  \node (i) {i};
  \node at ($(i) + (0:\sep)$) (C) {C};
  \node at ($(C) + (-120:\sep)$) (B) {B};
  \node at ($(C) + (-60:\sep)$) (A) {A};

  \draw (i) -- (C);
  \draw (C) to [bend left] (B);

  \draw (B) -- (A);
  \draw (C) -- (A);

  % add a node
  \path [draw,red] (B) to [bend left] node [black,midway] (X) {X} (C);

  % but draw edges along the original (red) path
  \draw [->,shorten >=2pt] (B) to [bend left] (X);
  \draw [->,shorten >=2pt] (X) to [bend left] (C);

\end{tikzpicture}

\end{document}

答案1

绘制图像的一种方法如下:

\documentclass[tikz, margin=6]{standalone}
\usetikzlibrary{arrows.meta,
                calc,
                intersections}

\begin{document}
    \begin{tikzpicture}[
                > = Straight Barb,
every node/.style = {circle, draw, minimum size=1cm}
                        ]
\def\sep{4cm}
  \node (i) {i};
  \node at ($(i) + (0:\sep)$) (C) {C};
  \node at ($(C) + (-120:\sep)$) (B) {B};
  \node at ($(C) + (-60:\sep)$) (A) {A};

  \draw (i) -- (C) 
        (C) to [bend left] (B) 
        (B) -- (A)
        (C) -- (A);
\draw[->, name path=bc]   
        (B) to [bend left] node [draw, fill=white, midway, name path=x] {X} (C);
\draw[->]   (B) to [bend left] node [draw, fill=white, midway] {X} (C);
\draw [name intersections={of=bc and x, by={x1,x2}},<-] 
    (x2) -- ++ (-105:0.01); % 105 = 120 - <band angle>/2
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

无需知道弯曲角度或手动添加内容即可重建弧线。弧线通过其上的三个点唯一定义,这三个点可以是起点、p0终点p1和节点的中心X。给定圆心(和半径),计算点的角度p0以及p1弧线与节点边界的交点i0和是非常简单的i1。因此,您现在精确地知道了弧线,甚至可以适当地弯曲箭头。

\documentclass[margin=6]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,intersections,through,arrows.meta,bending}
\tikzset{circle through 3 points/.style n args={3}{% https://tex.stackexchange.com/a/461180
insert path={let    \p1=($(#1)!0.5!(#2)$),
                    \p2=($(#1)!0.5!(#3)$),
                    \p3=($(#1)!0.5!(#2)!1!-90:(#2)$),
                    \p4=($(#1)!0.5!(#3)!1!90:(#3)$),
                    \p5=(intersection of \p1--\p3 and \p2--\p4)
                    in },
at={(\p5)},
circle through= {(#1)}
}}
\begin{document}

\begin{tikzpicture}[
  thick,
  every node/.style={
    draw,
    circle,
    minimum size=1cm,
  }
  ]
  \def\sep{4cm}
  \node (i) {i};
  \node at ($(i) + (0:\sep)$) (C) {C};
  \node at ($(C) + (-120:\sep)$) (B) {B};
  \node at ($(C) + (-60:\sep)$) (A) {A};

  \draw (i) -- (C);
  \draw (C) to [bend left] (B);

  \draw (B) -- (A);
  \draw (C) -- (A);

  % add a node
  \path [%draw,red,
  name path=arc] (B) to [bend left] 
  node[black,midway,name path=X] (X) {X} 
  coordinate [pos=0] (p0) coordinate [pos=1] (p1)  (C);
  \node[circle through 3 points={p0}{X.center}{p1},overlay,draw=none](Y){};
  \path[name intersections={of=arc and X,by={i1,i0}}];
  \draw[-{Stealth[bend]}] let \p1=($(p0)-(Y.center)$),\p2=($(i0)-(Y.center)$),
   \n1={atan2(\y1,\x1)},\n2={atan2(\y2,\x2)},\n3={veclen(\x1,\y1)} in
   (p0) arc(\n1:\n2-360:\n3);
  \draw[-{Stealth[bend]}] let \p1=($(i1)-(Y.center)$),\p2=($(p1)-(Y.center)$),
   \n1={atan2(\y1,\x1)},\n2={atan2(\y2,\x2)},\n3={veclen(\x1,\y1)} in
   (i1) arc(\n1:\n2:\n3);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容