![有没有更好的方法在两个既不垂直也不水平对齐的节点之间绘制平行线](https://linux22.com/image/294223/%E6%9C%89%E6%B2%A1%E6%9C%89%E6%9B%B4%E5%A5%BD%E7%9A%84%E6%96%B9%E6%B3%95%E5%9C%A8%E4%B8%A4%E4%B8%AA%E6%97%A2%E4%B8%8D%E5%9E%82%E7%9B%B4%E4%B9%9F%E4%B8%8D%E6%B0%B4%E5%B9%B3%E5%AF%B9%E9%BD%90%E7%9A%84%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%BB%98%E5%88%B6%E5%B9%B3%E8%A1%8C%E7%BA%BF.png)
我知道如何在水平或垂直对齐的两个节点之间画平行线。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[myn/.style={circle,draw,inner sep=0.25cm,outer sep=3pt}]
\node[myn] (A) at (0,0) {A};
\node[myn] (B) at (5,0) {B};
\node[myn] (C) at (5,3) {C};
\node[myn] (Z) at (10,-5) {Z};
\draw[->] (A.10) -- (B.170);
\draw[<-] (A.-10) -- (B.190);
\draw[->] (B.80) -- (C.-80);
\draw[<-] (B.100) -- (C.-100);
\end{tikzpicture}
\end{document}
我想知道的是如何在那些不太对齐的节点之间做类似的事情,比如A
在Z
上图中,我不一定事先知道确切的位置Z
。
我可以得到类似
\path let \p1=($(Z)-(A)$),
\n1={atan(\y1/\x1)},
\n2={\n1+180},
\n3={\n1+90},
\n4={1ex*cos(\n3)},
\n5={1ex*sin(\n3)}
in
[draw,blue] ([yshift=\n5,xshift=\n4]A.\n1) -- ([yshift=\n5,xshift=\n4]Z.\n2);
可以工作,但这似乎比必要的要复杂得多(或者,是吗?)。
顺便说一句,尽管这可能开始看起来像一个交换图,但它不应该是那样的。
答案1
以下是使用部分修饰符的建议:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
% Syntax:
% \DoublLine[half of the double line distance]{first node}{second node}{options line 1}{options line 2}
\newcommand\DoubleLine[5][3pt]{%
\path(#2)--(#3)coordinate[at start](h1)coordinate[at end](h2);
\draw[#4]($(h1)!#1!90:(h2)$)--($(h2)!#1!-90:(h1)$);
\draw[#5]($(h1)!#1!-90:(h2)$)--($(h2)!#1!90:(h1)$);
}
\begin{document}
\begin{tikzpicture}[myn/.style={circle,draw,inner sep=0.25cm,outer sep=3pt}]
\node[myn] (A) at (0,0) {A};
\node[myn] (B) at (5,0) {B};
\node[myn] (C) at (5,3) {C};
\node[myn] (Z) at (10,-5) {Z};
% double lines:
\foreach \p in {A,C,Z}{
\DoubleLine{B}{\p}{<-,red}{->,blue}
}
\end{tikzpicture}
\end{document}
答案2
只是为了和 PSTricks 一起玩。
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}
\begin{document}
\begin{pspicture}[radius=15pt,nodesep=5pt,showgrid](8,8)
\rput(2,6){\Circlenode{a}{A}}
\rput(6,2){\Circlenode{b}{B}}
\ncline[linecolor=red]{a}{b}
\ncline[offset=6pt,linecolor=green]{a}{b}
\ncline[offset=-6pt,linecolor=blue]{a}{b}
\end{pspicture}
\end{document}