我在 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}
結果如下:
答案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
例如,在範例中,我在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}