我想将节点与复杂形状的边缘连接起来。我只能将其与中心连接起来。
我有以下代码:
\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}