TikZ如何在两条线的交叉点处暂时中断线

TikZ如何在两条线的交叉点处暂时中断线

我正在使用 TikZ 工作,我想让一条线停止,然后在与另一条线相交之前和之后重新开始。我尝试在交点处放置一个节点并用白色填充,但这会切断两条线。我只希望蓝线断开,黑线继续。任何帮助都很好!

以下代码的作用如下: 在此处输入图片描述

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}%
\usetikzlibrary{arrows.meta,shapes.misc, positioning, shapes.geometric, intersections}%

\begin{document}
   \begin{tikzpicture}
    \draw [-Latex, RoyalBlue, name path = b1] (0,-1) -- (10,-1);
    \draw [-Latex, RoyalBlue, name path = b2] (0,-0.5) -- (10,-0.5);
    \draw [-Latex, RoyalBlue, name path = b3] (0,0) -- (10,0);
    \draw [-Latex, RoyalBlue, name path = b4] (0,0.5) -- (10,0.5);
    \draw [-Latex, RoyalBlue, name path = b5] (0,1) -- (10,1);

    \node[trapezium,
    draw = black,
    thick,
    rotate=90,
    minimum width = 2.5cm,
    minimum height= 1cm,
    trapezium left angle = 100,
    trapezium right angle = 80,
    trapezium stretches=true,
    trapezium stretches body =true,
    name path = trap1] (t) at (1,0) {};
    
    \draw[name intersections={of=b1 and trap1}] (intersection-1) node[fill=white] {};
    \draw[name intersections={of=b2 and trap1}] (intersection-1) node[fill=white] {};
    \draw[name intersections={of=b3 and trap1}] (intersection-1) node[fill=white] {};
    \draw[name intersections={of=b4 and trap1}] (intersection-1) node[fill=white] {};
    \draw[name intersections={of=b5 and trap1}] (intersection-1) node[fill=white] {};
    \end{tikzpicture}
\end{document}

答案1

这是一个使用spath3TikZ 库的解决方案。通过它,我们可以定义相关路径,在相关位置添加间隙,然后再绘制它们。

\documentclass{article}
%\url{https://tex.stackexchange.com/q/715947/86}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}%
\usetikzlibrary{
  arrows.meta,
  shapes.misc,
  positioning,
  shapes.geometric,
  intersections,
  spath3
}%

\begin{document}

\begin{tikzpicture}[>=Latex]

% Create and save the arrow paths
\foreach \k in {1,...,5} {
  \path [spath/save global=b\k] (0,-1.5 + \k/2) -- ++(10,0);
}

% Create the trapezium shape
\node[
  trapezium,
    rotate=90,
    minimum width = 2.5cm,
    minimum height= 1cm,
    trapezium left angle = 100,
    trapezium right angle = 80,
    trapezium stretches=true,
    trapezium stretches body =true,
    spath/save global=trap1
] (t) at (1,0) {};

% Split the trapezium and each of the arrows where they meet,
% then insert gaps in the arrow paths at one of the intersections,
% then remove the split in the arrow paths where we didn't insert a gap.
\foreach \k in {1,...,5} {
  \tikzset{
    spath/split globally at intersections={trap1}{b\k},
    spath/insert gaps globally after components={b\k}{5pt}{1},
    spath/spot weld globally={b\k},
  }
}

% This inserts the gaps in the trapezium path, we make an alias style
% just to shorten the code.
\tikzset{
  do stuff/.style={
    spath/insert gaps after components={trap1}{5pt}{#1},
  },
  do stuff/.list={5,6,7,8,9},
}

% Now render the arrow paths
\foreach \k in {1,...,5} {
  \draw[spath/use=b\k, ->, RoyalBlue];
}

% Now render the trapezium path
\draw[black,thick,spath/use=trap1];

\end{tikzpicture}
\end{document}

结果如下:

五条线穿过一个梯形,线和梯形之间插入断点,以产生线穿过梯形的效果。

答案2

我知道这只是一种解决方法,但您可以使用该节点两次:第一次用于交叉点,第二次用于实际绘制它。

为了让事情变得简单一点,您还可以为节点添加样式,以便随时重复使用它。坐标也可以这样做,这样您就可以命名坐标以供多次使用。

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}%
\usetikzlibrary{arrows.meta,shapes.misc, positioning, shapes.geometric, intersections}%

\tikzset{
    tnode/.style = {
        trapezium,
        thick,
        rotate=90,
        minimum width = 2.5cm,
        minimum height= 1cm,
        trapezium left angle = 100,
        trapezium right angle = 80,
        trapezium stretches=true,
        trapezium stretches body =true,
        name path = trap1,
    },
 }


\begin{document}
   \begin{tikzpicture}
    \coordinate (ct) at (1,0);
    \node[tnode] (t) at (ct) {};
    
    \draw [-Latex, RoyalBlue, name path = b1] (0,-1) -- (10,-1);
    \draw [-Latex, RoyalBlue, name path = b2] (0,-0.5) -- (10,-0.5);
    \draw [-Latex, RoyalBlue, name path = b3] (0,0) -- (10,0);
    \draw [-Latex, RoyalBlue, name path = b4] (0,0.5) -- (10,0.5);
    \draw [-Latex, RoyalBlue, name path = b5] (0,1) -- (10,1);

    \draw[name intersections={of=b1 and trap1}] (intersection-1) node[fill=white] {};
    \draw[name intersections={of=b2 and trap1}] (intersection-1) node[fill=white] {};
    \draw[name intersections={of=b3 and trap1}] (intersection-1) node[fill=white] {};
    \draw[name intersections={of=b4 and trap1}] (intersection-1) node[fill=white] {};
    \draw[name intersections={of=b5 and trap1}] (intersection-1) node[fill=white] {};

    \node [tnode, draw] at (ct) {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑。基于scopebackground层的示例。

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}

\usetikzlibrary{
    arrows.meta,
    shapes.misc,
    positioning,
    shapes.geometric,
    intersections,
    backgrounds,        % For a background layer
}


\begin{document}
   \begin{tikzpicture}
    \node[
        trapezium,
        draw = black,
        thick,
        rotate=90,
        minimum width = 2.5cm,
        minimum height= 1cm,
        trapezium left angle = 100,
        trapezium right angle = 80,
        trapezium stretches=true,
        trapezium stretches body =true,
        name path = trap1,
    ] (t) at (1,0) {};

    \begin{scope}[on background layer]
        \foreach \c [evaluate=\c as \y using {\c/2}] \n in {-2,...,2} {
            \draw [-Latex, RoyalBlue, name path={b\c}] (0,\y) -- (10,\y);
            \path [name intersections={of={b\c} and trap1, name=i}] (i-1) node[fill=white] {};
        };
    \end{scope}
\end{tikzpicture}
\end{document}

答案3

我会利用 TikZ 尊重绘制顺序这一事实。想法是先用白色覆盖蓝色箭头,然后再绘制梯形。我做了一些额外的改进。

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{tikzpicture}
        % Drawing from background to foreground.
        % Note that the blue arrows will be on top of the right side of the trapezoid.
        \draw[xshift=1cm, thick] (.5,1.2) -- (.5,-1.1);
        % \foreach loop, my favourite features in TikZ
        \foreach \y in {-1,-.5,...,1}
            \draw [-Latex, RoyalBlue] (0,\y) -- (10,\y);
        % Overdrawing blue arrows with white.
        % Width is "ultra thick" (1.6pt) + "thick" (.8pt) + "ultra thick" (1.6pt).
        \draw[xshift=1cm, white, line width=4pt] (-.5,1.1) -- (-.5,-1.2);
        % The thick lines of the trapezoid's sides at the top and bottom right corners don't look good.
        % I added line cap to make it almost unnoticeable, but still not perfect.
        \draw[xshift=1cm, thick, line cap=rect] (.5,1.2) -- (-.5,1.1) -- (-.5,-1.2) -- (.5,-1.1);
     \end{tikzpicture}
\end{document}

在此处输入图片描述

line cap由于盖子与 y 轴不平行,因此解决方案仍然不完美。为了避免这种情况,您可以一次性完成\draw整个梯形或使用您的trapezium形状。但是,这会失去蓝色箭头位于梯形右侧顶部的属性。

答案4

好吧,如果您需要将图像放在多色背景上,则可能需要使用交叉点,但如果所有背景都是特定的颜色,在本例中是白色,您可以使用图层并使用一些与背景相同的颜色进行绘制以模拟切割,然后利用使用3D library,不必使用梯形,例如在示例中,我在平面上绘制了线条XY以使用 3D 平面手动定位,然后在垂直平面上绘制一个正方形,但由中轴划分,以便在不同的图层上绘制一部分,然后在绘图样式选项中通过上一个操作,您可以绘制一条比原始宽度更大的白线,并模拟所需的视觉分离,我增加了宽度以便一切都可视化。

在这种情况下,\hooks定义了两个绘图指令,一个在背景层中,对应于第一个半正方形,另一个在普通层中;选项#1#2的放置方式是我可以为每个选项放置绘图属性,因此在第一个示例中,我可以放置两种不同的颜色,对于第三个示例,我可以添加具有更大白色宽度的预作用样式,最后是相同的,但对于任何图像,当然都是在YZ平面上绘制的。

结果:

在此处输入图片描述

梅威瑟:

\documentclass[border=0.5cm]{standalone}
\usepackage{tikzducks}
\usepackage[dvipsnames]{xcolor}
\usetikzlibrary{3d,graphs,arrows.meta}
\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main}

\begin{document}
    
    \begin{tikzpicture}
        \def\hooks#1#2{
            \draw[#1, rounded corners=3pt, line width=2pt] (0,1.2) -| (1,0) |- (0,-1.2););
            \begin{pgfonlayer}{background layer}
            \draw[#2, rounded corners=3pt, line width=2pt] (0,1.2) -| (-1,0) |- (0,-1.2);
            \end{pgfonlayer}
        }
    
        \begin{scope}[canvas is xy plane at z=0]
            \foreach \y in {-1,-.5,...,1}
            \draw [-Latex, RoyalBlue,line width=2pt,preaction={draw=white,line width=3.5pt}] (0,\y) -- (7,\y);
        \end{scope}
        \begin{scope}[canvas is zy plane at x=0.75]
            \hooks{blue}{red}
        \end{scope}
        \begin{scope}[canvas is zy plane at x=2]
            \hooks{black}{black}
        \end{scope}
        \begin{scope}[canvas is zy plane at x=3.25]
            \hooks{green!50!blue,preaction={draw=white,line width=3.5pt,shorten <=1pt,shorten >=1pt}}{green!50!blue}
        \end{scope}
        \begin{scope}[canvas is zy plane at x=5]
            \clip(2.5,1.5) rectangle (0,-1.5);
            \draw(0,0)node[transform shape]{\includegraphics[height=2.2cm]{example-image-duck}};
            \begin{pgfonlayer}{background layer}
                \clip(-2.5,1.5) rectangle (0,-1.5);
                \draw(0,0)node[transform shape]{\includegraphics[height=2.2cm]{example-image-duck}};
            \end{pgfonlayer}
            
        \end{scope}

    \end{tikzpicture}
    
\end{document}

相关内容