在 TikZ 中绘制水平线到圆角矩形节点

在 TikZ 中绘制水平线到圆角矩形节点

我有一个圆角矩形节点,我想在该节点上绘制一条水平线。我可以使用诸如 之类的东西(A.210) to (A.210 -| -2, 0),但我希望能够明确指定 y 坐标,而我很难找到实现这一点的方法(除非手动进行三角学计算以找出直角锚点)。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}

\begin{document}
\begin{tikzpicture}
  \node (A) [draw, rounded rectangle, minimum height=4em] {};
  \draw (-2, -1em) to (A); % I want this line to be horizontal.
\end{tikzpicture}
\end{document}

输出:

答案1

尝试这个:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw[line width=0.5pt,color=red] (-2,0-1em) -- (0,0-1em);
    \draw[line width=0.5pt] (0,0) circle (2em);
    \fill[color=white] (0,0) circle (2em);
\end{tikzpicture}
\end{document}

在此处输入图片描述

您可以将线条宽度和颜色更改为您想要的任何颜色。请注意,必须将颜色\fill设置为纸张背景颜色,以隐藏红线的不需要的部分。

答案2

您也可以使用交叉点(以防您将 A 节点放在某些背景的顶部,这会阻止您将其填充为白色)。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.misc,intersections}

\begin{document}
\begin{tikzpicture}
  \node (A) [draw, rounded rectangle, minimum height=4em,name path=A] {};
  \path[name path=B] (-2, -1em) coordinate (aux) -- (0,-1em);
  \draw[red,name intersections={of=A and B}] (aux) -- (intersection-1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容