TikZ 2 本の線の交差点で一時的に線を中断する方法

TikZ 2 本の線の交差点で一時的に線を中断する方法

私は TikZ で作業しており、1 本の線を停止し、別の線との交差の前後で再び開始するようにしようとしています。交差点にノードを配置して白で塗りつぶそうとしましたが、両方の線が切れてしまいました。青い線を切断し、黒い線を継続させたいだけです。どなたか助けていただければ幸いです。

次のコードの動作は次のとおりです。 ここに画像の説明を入力してください

\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

これは TikZ ライブラリを使用したソリューションですspath3。これを使用すると、関連するパスを定義し、関連する場所にギャップを追加してから、後で描画することができます。

\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}

結果は次のとおりです。

台形を横切る 5 本の線。線と台形に切れ目が挿入されており、線が台形を通過しているような効果を生み出しています。

答え2

これは単なる回避策であることは承知していますが、そのノードを 2 回使用できます。1 回目は交差点用、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使用する必要がないように利点を活用します。たとえば、例では、XY3D 平面の手動配置で作業するために平面に線を描き、垂直平面に正方形を描きますが、これは中線で分割され、一部が別のレイヤーに描画されます。次に、前のアクションを介して描画スタイル オプションで、元よりも幅の広い白い線を描くことができ、必要な視覚的な分離がエミュレートされ、幅を広げてすべてが視覚化されるようにしました。

この場合、\hooks2 つの描画命令で定義されます。1 つは背景レイヤーで、最初の半正方形に対応し、もう 1 つは通常のレイヤーの補数に対応します。オプション#1とが#2配置されているので、それぞれに描画プロパティを配置できます。したがって、最初の例では、2 つの異なる色を配置し、3 番目には、より広い白幅の preaction スタイルを追加します。最終的には、もちろんすべての画像に対して同じですが、YZ平面で描画されます。

結果:

ここに画像の説明を入力してください

MWE:

\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}

関連情報