连接两条路径而不知道它们之间的距离

连接两条路径而不知道它们之间的距离

考虑下面的图片:

在此处输入图片描述

我想连接两条蓝色路径以获得如下图所示的红色路径:

在此处输入图片描述

我想要一个不需要知道红色路径的水平长度的解决方案,这样 Tikz 就会代替我自动计算出合适的长度。

平均能量损失

我尝试了一些坐标,但得到了这个结果(我无法“移动”坐标(A)(二)垂直):

在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand*{\myDistance}{1}%

\begin{document}
    \begin{tikzpicture}
        % Preliminary node positionning
        \draw (0,0) node[rectangle,draw,minimum width=\myDistance] (Nod0) {Node 0}%
        ($ (Nod0.north west)!0.2\myDistance!(Nod0.north east) $) -- ++(0,2*\myDistance)%
        -- ++(\myDistance,0)%
        node[rectangle,draw,minimum width=\myDistance cm,anchor=west] (Nod1) {Node 1}%
        (Nod1.east) -- ++(\myDistance,0)%
        node[rectangle,draw,minimum width=\myDistance cm,anchor=west] (Nod2) {Node 2}%
        ;%
        
        % Two paths to connect
        \draw[blue] ($ (Nod0.north west)!0.8\myDistance!(Nod0.north east) $) --  ++(0,\myDistance);%
        \draw[blue] ($ (Nod1) !.5! (Nod2) $) -- ++(0,-\myDistance);%
        
        \coordinate (A) at ($ (Nod0.north west)!0.8\myDistance!(Nod0.north east) $)  +(0,\myDistance);
        \coordinate (B) at ($ (Nod1) !.5! (Nod2) $) +(0,-\myDistance);%
        
         \draw[red] (A) -- (B);%
    \end{tikzpicture}
\end{document}

为什么我不想要“绝对坐标”?

因为我希望这个图表可以轻松修改,例如如果我想更改垂直和水平距离节点 0节点 1,我只需要改变\我的距离值,所有内容将自动调整,无需手动重新定位蓝色路径,因为所有内容都相对于节点 0

答案1

只需在蓝色路径的末尾定义两个坐标,如下所示:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand*{\myDistance}{1}%

\begin{document}
    \begin{tikzpicture}
        % Preliminary node positionning
        \draw (0,0) node[rectangle,draw,minimum width=\myDistance] (Nod0) {Node 0}%
        ($ (Nod0.north west)!0.2\myDistance!(Nod0.north east) $) -- ++(0,2*\myDistance)%
        -- ++(\myDistance,0)%
        node[rectangle,draw,minimum width=\myDistance cm,anchor=west] (Nod1) {Node 1}%
        (Nod1.east) -- ++(\myDistance,0)%
        node[rectangle,draw,minimum width=\myDistance cm,anchor=west] (Nod2) {Node 2}%
        ;%
        
        % Two paths to connect
        \draw[blue] ($ (Nod0.north west)!0.8\myDistance!(Nod0.north east) $) --  ++(0,\myDistance) coordinate (A);% <----
        \draw[blue] ($ (Nod1) !.5! (Nod2) $) -- ++(0,-\myDistance) coordinate (B);% <----
        
%        \coordinate (A) at ($ (Nod0.north west)!0.8\myDistance!(Nod0.north east) $)  +(0,\myDistance);
%        \coordinate (B) at ($ (Nod1) !.5! (Nod2) $) +(0,-\myDistance);%
        
         \draw[red] (A) -- (B);%
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容