我想按以下方式连接节点:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (foo) at (0,0) {(0,0)};
\node (bar) at (5,3) {(5,3)};
\node (baz) at (0,-1) {(0,-1)};
\node (boo) at (5,1) {(5,1)};
% What I want to happen
\draw (foo) -- ++(3,3) -- (bar);
\draw (baz) -- ++(2,2) -- (boo);
% The kind of syntax I would like, but if there is a more
% 'correct' syntax I'm happy to use it
%\draw[**magic**] (foo) -- (bar);
%\draw[**magic**] (baz) -- (boo);
\end{tikzpicture}
\end{document}
我知道一定有一个具有样式的解决方案,但我的 TikZ 技能目前不足以知道如何或在哪里查找。
我怎样才能按照上述风格连接节点?
答案1
我使用abs
并sign
允许其他用例。
更通用的版本:
选择to[diagonal line]
连接后,首先对角线(45°)然后水平或垂直线绘制一条直线。如果起点和终点之间的角度是 45° 的倍数,则只绘制一条直线。
代码:
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{
diagonal line/.style={
to path={
let \p{start}=(\tikztostart),
\p{target}=(\tikztotarget),
\p{diff}=({\x{target}-\x{start}}, {\y{target}-\y{start}}),
\p{absdiff}=({abs(\x{diff})}, {abs(\y{diff})}),
\n{mindiff}={min(\x{absdiff}, \y{absdiff})},
\p{inter}=(
{sign(\x{diff}) * \n{mindiff}},
{sign(\y{diff}) * \n{mindiff}}
)
in
\ifnum \ifdim\x{target}=\x{start} 1
\else \ifdim\y{target}=\y{start} 1
\else \ifdim\x{absdiff}=\y{absdiff} 1
\else 0\fi\fi\fi=1 %primitive tex or condition
-- (\tikztotarget)
\else
--++ (\p{inter}) -- (\tikztotarget)
\fi
},
},
}
\begin{document}
\begin{tikzpicture}
\foreach \d in {1,...,16} {
\node (a\d) at ($(0,0)+(90-\d*22.5:12mm)$) {\footnotesize $a_{\d}$};
\node (b\d) at ($(0,0)+(90-\d*22.5:35mm)$) {\footnotesize $b_{\d}$};
\draw (a\d) to[diagonal line] (b\d);
}
\end{tikzpicture}
\end{document}
结果:
较旧的非通用版本
代码:
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{
special line/.style={
to path={
let \p{start}=(\tikztostart), \p{target}=(\tikztotarget),
\p{inter}=({\x{start} + sign(\x{target}-\x{start}) * abs(\y{target}-\y{start})}, \y{target})
in -- (\p{inter}) -- (\tikztotarget)
},
},
}
\begin{document}
\begin{tikzpicture}
\node (x) at (0,0) {(0,0)};
\node (y1) at (5,2) {(5,2)};
\node (y2) at (5,-2) {(5,-2)};
\node (y3) at (-5,-2) {(-5,-2)};
\node (y4) at (-5,2) {(-5,2)};
\draw (x) to[special line] (y1);
\draw (x) to[special line] (y2);
\draw (x) to[special line] (y3);
\draw (x) to[special line] (y4);
\end{tikzpicture}
\end{document}
结果:
使用 Pauls 或 Ignasis 代码的结果是:
答案2
calc
以下是使用TikZ 库、let
操作和样式的解决方案to path
:
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{
special line/.style={
to path={
let \p{start}=(\tikztostart), \p{target}=(\tikztotarget),
\p{inter}=(\x{start}+\y{target}-\y{start},\y{target})
in -- (\p{inter}) -- (\tikztotarget)
},
},
}
\begin{document}
\begin{tikzpicture}
\node (foo) at (0,0) {(0,0)};
\node (bar) at (5,3) {(5,3)};
\node (baz) at (0,-1) {(0,-1)};
\node (boo) at (5,1) {(5,1)};
\draw (foo) to[special line] (bar);
\draw (baz) to[special line] (boo);
\end{tikzpicture}
\end{document}
答案3
style
与 Paul 的解决方案不同,而是command
:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning,calc}
\newcommand{\connect}[3][]{%
\draw[#1] (#2) let \p1 = ($(#3)-(#2)$) in --++(\y1,\y1)--(#3);
}
\begin{document}
\begin{tikzpicture}
\node (a) at (0,0) {A};
\node (b) at (5,3) {B};
\connect{a}{b}
\node (c) at (0,-1) {C};
\node (d) at (5,1) {D};
\connect{c}{d}
\connect[red]{a}{d}
\connect[dashed, blue]{c}{b}
\end{tikzpicture}
\end{document}