基本 TikZ 绘图

基本 TikZ 绘图

我还没有足够多的 TikZ 绘图经验,因此无法快速完成。我还发现手册有点令人望而生畏——不过,如果我有时间,我相信它能很好地解释我需要知道的一切。

但是,目前我没有时间学习所有内容,所以我寻求帮助。我想要的是这样的(线条是实线,而不是像我在这里画的那样粗糙):

        |
 Text4  |  Text1
------------------
 Text3  |  Text2 
        |

另外,我想要一个圆形箭头,从“Text1”的右上方开始绕一圈,直到它停止(带有箭头)到“Text4”的左上方。

好的。我希望这已经足够清楚了。我慢慢开始理解这里的语法;到目前为止,你们一直都很包容我,我非常感激。

答案1

四个标签和垂直线很容易绘制。您可以使用节点矩阵,例如:

\matrix[matrix of nodes, name=M, nodes={minimum height=1.2cm}] {
  Text 4 & Text 1 \\
  Text 3 & Text 2 \\
};
\draw (M.north) -- (M.south) (M.east) -- (M.west);

Result

圆形箭头更难,特别是如果你需要一个真正的圆形(即圆弧)。Tikz 可以绘制圆弧,但默认语法要求你指定起点、起始和终止角度以及半径,这些在图形中不明显也不容易找到。

我编写了一个名为的宏\arcThroughThreePoints(首先在另一个答案),顾名思义,它接受三个点并绘制通过这些点的圆形路径。使用此宏可以更轻松地绘制箭头,因为三个给定的点将是“文本 1”的右上角、“文本 4”的左下角以及整个节点矩阵底部下方的某个任意点。这三个点需要进行一些手动调整才能获得看起来不错的箭头。

这是完整的代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\newcommand{\arcThroughThreePoints}[4][]{
\coordinate (middle1) at ($(#2)!.5!(#3)$);
\coordinate (middle2) at ($(#3)!.5!(#4)$);
\coordinate (aux1) at ($(middle1)!1!90:(#3)$);
\coordinate (aux2) at ($(middle2)!1!90:(#4)$);
\coordinate (center) at ($(intersection of middle1--aux1 and middle2--aux2)$);
\draw[#1] 
 let \p1=($(#2)-(center)$),
      \p2=($(#4)-(center)$),
      \n0={veclen(\p1)},       % Radius
      \n1={atan2(\x1,\y1)}, % angles
      \n2={atan2(\x2,\y2)},
      \n3={\n2>\n1?\n2:\n2+360}
    in (#2) arc(\n1:\n3:\n0);
}

\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes, name=M, nodes={minimum height=1.2cm}] {
  Text 4 & Text 1 \\
  Text 3 & Text 2 \\
};

\draw (M.north) -- (M.south) (M.east) -- (M.west);
% Coordinates for the three points of the circular arc
\coordinate (end) at ($(M.west)+(-0.2,0.2)$);
\coordinate (start) at ($(M.north east)+(-0.2,-0.2)$);
\coordinate (bottom) at ($(M.south)+(0,-0.2)$);
% Draw the arc in red, and with an arrow tip
\arcThroughThreePoints[<-,red]{end}{bottom}{start};
\end{tikzpicture}
\end{document}

结果:

Final result with arrow

更新

在这种特殊情况下,存在一个更简单(可能更好)的解决方案,假设您的四个节点具有相同的宽度,那么您的圆弧将位于垂直线的交点处,即:在矩阵中心。

在这种情况下,我们可以使用极坐标将起点设置为与该中心的给定角度和距离(例如,角度=35,距离=1.6,大致与“文本 1”的右上角重合)。此点可以指定为(M.center) +(35:1.6)。然后,弧的标准语法非常适合这个问题,因为我们知道弧的起始角度(35),其终止角度(例如-190)及其半径(1.6)。所以\arcThroughThreePoints不需要笨拙的宏:

\begin{tikzpicture}
\matrix[matrix of nodes, name=M, nodes={minimum height=1.2cm}] {
  Text 4 & Text 1 \\
  Text 3 & Text 2 \\
};

\draw (M.north) -- (M.south) (M.east) -- (M.west);
% Draw the arc in red, and with an arrow tip
\draw[red,->] (M.center) +(35:1.6) arc(35:-190:1.6);
\end{tikzpicture}

导致:

Second solution

相关内容