将节点之间的边缩短至绝对长度

将节点之间的边缩短至绝对长度

如何才能在节点之间仅绘制边/箭头的一部分(具有定义的绝对长度);类似以下操作,但shorten始终必须适应之间的距离AB这是困难且麻烦的:

\documentclass{article}
\usepackage{tikz}
\begin{document}
  %% --- start tikz ---
  \begin{tikzpicture}[x=1cm,y=1cm]
    % create two nodes
    \node[draw=black,circle] (A) at (0,0) {A}; 
    \node[draw=black,circle] (B) at (3,4) {B};
    % create arrow starting at A with direction A->B and length 1cm
    % ... I know the shorten option, however; this is not want I want!
    \draw[->,shorten >=3.5cm,shorten <=0cm] (A) -- (B);
  \end{tikzpicture}
  %% --- end tikz ---
\end{document}

输出

答案1

您可以使用距离修改器

($(A)!1cm!(B)$)

这需要\usetikzlibrary{calc}

代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
  %% --- start tikz ---
  \begin{tikzpicture}[x=1cm,y=1cm]
    % create two nodes
    \node[draw=black,circle] (A) at (0,0) {A};
    \node[draw=black,circle] (B) at (3,4) {B};
    % create arrow starting at A with direction A->B and length 1cm
    %    ... I know the shorten option, however; this is not want I want!
    \draw[->] (A) -- ($(A)!1cm!(B)$);
  \end{tikzpicture}
  %% --- end tikz ---
\end{document}

在此处输入图片描述

答案2

这是一个可能的解决方案,其中intersection找到了单位圆的点(名称为 path=curve)和线 AB(名称为 path=line)。但是,2cm 的红色箭头用于演示。可以更改宏值\dist以获得其他可能性。

在此处输入图片描述

代码

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,intersections,calc}

\def\dist{2cm} % the length should be less than the node distance, also note that a node has size.

\begin{document}
 %% --- start tikz ---
\begin{tikzpicture}[x=1cm,y=1cm]
% create two nodes
\node[draw=black,circle] (A) at (0,0) {A}; 
\node[draw=black,circle] (B) at (3,4) {B};
% create arrow starting at A with direction A->B and length 1cm
% ... I know the shorten option, however; this is not what I want! 
\path[name path=line]  (A)--(B); 
\path[name path=curve] (A)--+(\dist,0) arc (0:90:\dist);  % change \path to \draw see the arc intersection.
\draw[->,shorten >=3.5cm,shorten <=0cm] (A) -- (B);  % OP's code
\draw[name intersections= {of=curve and line, by={a}}, red,->,thick] (A) -- (a);
\end{tikzpicture}
%% --- end tikz ---
\end{document}

相关内容