我想绘制附图右侧所示的从节点 3 到节点 1 和节点 2 之间的线。
我尝试使用 tikz calc 包来计算出该线的正确位置。但无法找出语法。
左边的图片是我示例代码目前实现的。线的终点位于节点 3 的西边,即从节点 1 到节点 2 的线的 y 坐标处。
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\tikzset{block/.style={rectangle,draw}}
\begin{document}
\begin{tikzpicture}
\node[block] (node1) {node1};
\node[block, right=of node1] (node2) {node2};
\draw (node1.east) -- (node2.west);
\node [block, above=of node1, xshift=-0.5cm] (node3) {node3};
\draw (node3.east) -- +(1.,0) -- (node3.east |- node1);
%\draw (node3.east) -- +(1.,0) -- ($(node3.east)+(1.,0)$ |- node1);
\end{tikzpicture}
\end{document}
答案1
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\tikzset{block/.style={rectangle,draw}}
\begin{document}
\begin{tikzpicture}
\node[block] (node1) {node1};
\node[block, right=of node1] (node2) {node2};
\draw (node1.east) -- (node2.west) coordinate[midway] (A);
\node [block, above=of node1, xshift=-0.5cm] (node3) {node3};
\draw (node3.east) -| (A);
\end{tikzpicture}
\end{document}
答案2
你甚至不需要计算。
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{block/.style={rectangle,draw}}
\begin{document}
\begin{tikzpicture}
\node[block] (node1) {node1};
\node[block, right=of node1] (node2) {node2};
\draw (node1.east) -- (node2.west);
\node [block, above=of node1, xshift=-0.5cm] (node3) {node3};
\draw (node3.east) -- +(1.,0) -- ([xshift=1cm]node3.east |- node1);
\end{tikzpicture}
\end{document}
答案3
只需修改一个代码
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\tikzset{block/.style={rectangle,draw}}
\begin{document}
\begin{tikzpicture}
\node[block] (node1) {node1};
\node[block, right=of node1] (node2) {node2};
\draw (node1.east) -- (node2.west);
\node [block, above=of node1, xshift=-0.5cm] (node3) {node3};
\draw (node3.east) -- +(1,0) -| ($(node1)!.5!(node2)$);
%\draw (node3.east) -- +(1.,0) -- ($(node3.east)+(1.,0)$ |- node1);
\end{tikzpicture}
\end{document}
($(node1)!.5!(node2)$) 表示两个节点的中点