tikz :在路径中指定百分比参数

tikz :在路径中指定百分比参数

我正在尝试定义一种样式来绘制从一个形状的南到另一个形状的西的弯曲线

\documentclass[border=5mm, convert, usenames, dvipsnames]{standalone}
\usepackage{tikz, pifont, xcolor}
\usetikzlibrary{shapes, positioning, calc, arrows.meta, matrix, chains, scopes, fit}
\usepackage{lmodern}
\usepackage{underscore}

\begin{document}
\begin{tikzpicture}[auto]
    \tikzset{downbendjoin/.style={to path = {(\tikztostart.south) -- ($(\tikztostart.south)!0.5!(\tikztostart.south|-\tikztotarget.north)$) -| ([xshift=-1cm]\tikztotarget.west) -- (\tikztotarget)}, rounded corners}}
    \node [draw=black, rectangle, minimum width=4cm, minimum height=3cm] (a) {A};
    \node [draw=black, rectangle, minimum width=4cm, minimum height=3cm, below left = of a] (b) {B};
    \draw [downbendjoin] (a) to (b);
\end{tikzpicture}
\end{document}

它正如我预期的那样正常工作。 enter image description here

接下来我想做的是向下弯连接,如下所示

\tikzset{downbendjoin/.style n args ={1}{to path = {(\tikztostart.south) -- ($(\tikztostart.south)!#1!(\tikztostart.south|-\tikztotarget.north)$) -| ([xshift=-1cm]\tikztotarget.west) -- (\tikztotarget)}, rounded corners}}
\tikzset{downbendjoin/.default={0.5}}

目的是控制路径的弯曲点,我设置了默认的位置百分比为0.5。但是这并不如预期的那样起作用,生成的图片如下,百分比参数根本不起作用。 enter image description here

做了一些调查,但没有找到根本原因。有人能指出我的参数设置有什么问题吗?

答案1

您正在加载calc库(以及更多;-)因此您可以简单地使用它......

\documentclass[border=3.14mm,tikz]{standalone}
\usetikzlibrary{positioning,calc}
\begin{document}
\tikzset{downbendjoin/.style={to path = {
    let
\p1=(\tikztostart.south),\p2=(\tikztotarget.north),\p3=(\tikztotarget.west) in
    (\tikztostart.south) -- ++(0,{#1*(\y2-\y1)}) -- ++ ({\x3-\x1-1cm},0)
    |-  (\tikztotarget)}, rounded corners}}
\foreach \X in {0.2,0.25,...,0.8}
{\begin{tikzpicture}[auto]
    \node [draw=black, rectangle, minimum width=4cm, minimum height=3cm] (a) {A};
    \node [draw=black, rectangle, minimum width=4cm, minimum height=3cm, below left = of a] (b) {B};
    \draw [downbendjoin=\X] (a) to (b);
\end{tikzpicture}}
\end{document}

enter image description here

相关内容