TikZ:如何连接节点并指定线方向?

TikZ:如何连接节点并指定线方向?

在 TikZ 中在两个节点之间画一条线时,您可以指定起始节点和结束节点上的锚点来控制线的绘制方式。

我想指定起始节点上的锚点以及绘制线条的角度,然后让 TikZ 确定它将在何处截取结束节点。通常的用例是当我连接到“大节点”时,我希望线条是垂直的。

我可以通过在大节点后面放置一个坐标,改为绘制一条指向该坐标的线,然后绘制大节点以将其覆盖。但是,如果我需要在末端放置一个箭头,这种方法就行不通了。

我可以尝试手动计算大节点边缘的截距坐标,但这似乎违背了 TikZ 的精神。

|-在这种情况下不起作用。

\documentclass[10pt]{minimal}
\usepackage[active,tightpage,pdftex]{preview}
\usepackage{tikz}

\begin{document}
\begin{preview}

\tikzstyle{RectObject}=[rectangle,fill=white,draw,line width=0.5mm]
\tikzstyle{line}=[draw]
\tikzstyle{arrow}=[draw, -latex] 

\begin{tikzpicture}
  %%%%%%%%%%%%%%%%%%%%%%%%
  %%  LEFT SIDE
  %%%%%%%%%%%%%%%%%%%%%%%%
  \draw (1,0) node[RectObject] (Small1A) {A};
  \draw (3,0) node[RectObject] (Small1B) {B};
  \coordinate (Cheat1A) at (1,2);
  \coordinate (Cheat1B) at (3,2);
  \path [line] (Small1A.north) -- (Cheat1A);
  \path [line] (Small1B.north) -- (Cheat1B);

  % need to draw Big Node last to cover up our cheating
  \draw (2,2) node[RectObject, inner xsep=1cm] (Big1) {Big Node};

  \draw (2,4) node[text width=4cm] (text1) {L: Cheating to control line orientation. Arrows would be hard.};

  %%%%%%%%%%%%%%%%%%%%%%%%
  %%  RIGHT SIDE
  %%%%%%%%%%%%%%%%%%%%%%%%
  \draw (7,2) node[RectObject, inner xsep=1cm] (Big2) {Big Node};
  \draw (6,0) node[RectObject] (Small2A) {A};
  \draw (8,0) node[RectObject] (Small2B) {B};
  \path [arrow] (Small2A) -- (Big2);
  \path [arrow] (Small2B) -- (Big2);

  \draw (7,4) node[text width=4cm] (text2) {R: No cheating. Arrows easy. Can't control line orientation.};
\end{tikzpicture}
\end{preview}
\end{document}

下面是“作弊”的并排示例,它显示了我想要做的事情,以及“不作弊”,这并没有做我想要做的事情。

并排的“作弊”示例,显示了我想要做的事情,以及“不作弊”,这并没有做我想要做的事情

此示例的左侧显示了我想要做的事情,只是我想在不作弊的情况下完成它,并且我想在线的末尾放置箭头。

答案1

也许我误解了某些内容,但我认为 |- 语法没有任何问题。使用 Jake 的代码:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\tikzstyle{RectObject}=[rectangle,fill=white,draw,line width=0.5mm]
\tikzstyle{line}=[draw]
\tikzstyle{arrow}=[draw, -latex] 

\begin{tikzpicture}
  \draw (7,2) node[RectObject, inner xsep=1cm] (Big2) {Big Node};
  \draw (6,0) node[RectObject] (Small2A) {A};
  \draw (8,0) node[RectObject] (Small2B) {B};
  \draw[arrow] (Small2A.north)--(Small2A|-Big2.south);
  \draw[arrow] (Small2B.north)--(Small2B|-Big2.south);

\end{tikzpicture}
\end{document}

在此处输入图片描述

如果有人需要它,这个语法解释如下手册“13.3.1 垂直线的交点”部分。

(<p> |- <q>)或表示通过 坐标的垂直线与通过 的水平线(<q> -| <p>)的交点处的坐标。您可以使用命名坐标(如 )或数字对(如 )。<p><q>(Small2B|-Big2.south)(2,1 |- 3,4)

答案2

对于我给出的例子,即希望节点之间有严格的垂直线,Jake 和 Ignasi 的回答非常出色。我找到了一种对线的任意方向进行相同操作的方法。TikZ 的“交点”机制可用于查找由节点或坐标定义的线的交点。“交点”语法很棘手;似乎您不能在“交点”规范中使用“计算”​​语法,因此您必须创建一个新的坐标并在那里进行计算。

示例图:三个节点、两条线、任意方向的线

\documentclass[10pt]{minimal}
\usepackage[active,tightpage,pdftex]{preview}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}

\begin{document}
\begin{preview}

\tikzstyle{RectObject}=[rectangle,fill=white,draw,line width=0.5mm]
\tikzstyle{line}=[draw]
\tikzstyle{arrow}=[draw, -latex] 

\begin{tikzpicture}
  \draw (2,2) node[RectObject, inner xsep=1cm] (Big1) {Big Node};
  \draw (1,0) node[RectObject] (Small1A) {A};
  \draw (3,0) node[RectObject] (Small1B) {B};

  % Method 1: use positioning library to put a coordinate at a special place
  % on the target node.  Doesn't really satisfy my criteria for setting the
  % line's orientation directly.
  \coordinate[left=1.4cm of Big1.south] (Small1AatBig1);
  \path [arrow] (Small1A) -- (Small1AatBig1);

  % Method 2: set a phantom coordinate using polar coordinates, then draw
  % a line to the intersection of the target node and the phantom line
  % Intersection syntax is tricky: no parentheses and no spaces around --
  \coordinate (Phantom) at ($ (Small1B) + (115:5cm) $);
  \path [arrow] (Small1B) -- (intersection of Big1.south--Big1.south east and Small1B--Phantom);

\end{tikzpicture}
\end{preview}

答案3

两种方法,均使用 TikZcalc库:要么使用语法,它指定在从到 的线上($(A)!(C)!(B)$)的投影,要么使用语法,它将坐标存储在宏中并允许您使用和分别访问其 x 和 y 坐标:(C)(A)(B)let \p1 = (<coordinate>) in ...\p1\x1\y1

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\tikzstyle{RectObject}=[rectangle,fill=white,draw,line width=0.5mm]
\tikzstyle{line}=[draw]
\tikzstyle{arrow}=[draw, -latex] 

\begin{tikzpicture}
  \draw (7,2) node[RectObject, inner xsep=1cm] (Big2) {Big Node};
  \draw (6,0) node[RectObject] (Small2A) {A};
  \draw (8,0) node[RectObject] (Small2B) {B};
  \path [arrow]  let \p1=(Small2A), \p2=(Big2.south) in (Small2A) -- (\x1,\y2);
  \path [arrow] (Small2B) -- ($(Big2.south east)!(Small2B)!(Big2.south)$);

\end{tikzpicture}
\end{document}

带箭头的垂直线

相关内容