我想將節點與複雜形狀的邊緣連接起來。我只能將其與中心連接起來。
我有以下程式碼:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm]
\coordinate (S1) at (0,0) {};
\coordinate (S2) at (3,4) {};
\draw ($(S1)-(1,2)$) rectangle ($(S1)+(1,2)$);
\draw[pattern=north west lines] ($(S1)-(1,2)$) rectangle ($(S1)+(1,1)$);
\node (N2) at (S2) {blubber};
\draw (S1) -- (N2);
\end{tikzpicture}
\end{document}
結果影像:
答案1
使用節點代替矩形。您可以定位它們的錨點、調整寬度和高度、填充和繪製邊框。那麼連接就變得非常容易。
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\coordinate (S2) at (3cm,4cm);
\node[draw,anchor=south west,minimum width=2cm,minimum height=4cm] (R1) at (-1,-2) {};
\node[draw,anchor=south west,pattern=north west lines,minimum width=2cm,minimum height=3cm] (R2) at (-1,-2) {};
\node (N2) at (S2) {blubber};
\draw (R1) -- (N2);
\end{tikzpicture}
\end{document}
答案2
這將是一個解決方案,但不知何故我不喜歡嵌套圖片。
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm]
\coordinate (S1) at (0,0) {};
\coordinate (S2) at (3,4) {};
\node[inner sep=0,outer sep=0] (N1) at (S1) {
\begin{tikzpicture}
\draw (-1,-2) rectangle (1,2);
\draw[pattern=north west lines] (-1,-2) rectangle (1,1);
\end{tikzpicture}
};
\node (N2) at (S2) {blubber};
\draw (N1) -- (N2);
\end{tikzpicture}
\end{document}