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

다음은 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

나는 이것이 해결 방법일 뿐이라는 것을 알고 있지만 해당 노드를 두 번 사용할 수 있습니다. 첫 번째는 교차점에 사용하고 두 번째는 실제로 그리는 데 사용합니다.

작업을 좀 더 쉽게 하기 위해 노드에 스타일을 추가하여 필요할 때마다 재사용할 수도 있습니다. 좌표에 대해서도 동일한 작업을 수행할 수 있으므로 여러 용도로 이름이 지정된 좌표를 가질 수 있습니다.

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

여기에 이미지 설명을 입력하세요

편집하다. scope및 레이어를 기반으로 한 예입니다 background.

\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 평면 수동 위치 지정 작업을 위해 평면에 선을 그린 다음 사각형 모양을 사용합니다. 수직 평면이지만 중앙 축으로 나누어 부품이 다른 레이어에 그려지도록 한 다음 이전 작업을 통해 그리기 스타일 옵션에서 원본보다 너비가 더 큰 흰색 선과 시각적 분리를 그릴 수 있습니다. 필요한 것이 에뮬레이션되었으므로 모든 것이 시각화되도록 너비를 늘렸습니다.

이 경우 \hooks두 개의 그리기 명령으로 정의됩니다. 하나는 배경 레이어에 있으며, 이는 첫 번째 반 정사각형과 일반 레이어의 보수에 해당합니다. 옵션이 #1있으며 #2각각에 대한 그리기 속성을 배치할 수 있도록 배치되므로 첫 번째 예에서는 서로 다른 색상을 모두 배치할 수 있고 세 번째 예에서는 흰색 너비가 더 큰 프리액션 스타일을 추가합니다. 마지막으로 모든 이미지에 대해 동일하지만 물론 모두 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}

관련 정보