如何在 TikZ 中正确定位圆弧和直线?

如何在 TikZ 中正确定位圆弧和直线?

我有这个MWE:

\documentclass[11pt]{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[line width=2mm,scale=0.5]
    \draw (-4,-3) circle (4.5cm);
    \draw (-4,-3) circle (5.3cm);
    \draw (-2,-1) arc (0:-180:2);
    \draw (-2,-6) arc (0:180:2);
    \draw (-6,-3.5) -- (-2,-3.5);
    \draw (-4,-6) -- (-4,-1);
\end{tikzpicture}

\end{document}

数字

我希望中间的图形正确地聚焦在两个圆上,并且水平线与两个圆弧接触,但不重叠。另外,所述水平线的长度与圆弧的长度相同(垂直线的高度似乎与两个圆弧和水平线的高度之和相同),如下所示:

正确图形

有没有什么方法可以做到这一点而不需要多次测试眼睛(但没有编程,我仍在学习使用 Tikz 的说明)?

谢谢!

答案1

根据我的评论,您需要了解并执行以下操作:

\documentclass[10pt]{article}
\usepackage[margin=1cm]{geometry}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\centering
We are starting from the base of our figure:

\begin{tikzpicture}[scale=1]
  \draw[line width =0.1pt] (0,0) circle (4.5cm);
  \draw[line width =0.1pt] ({-2 cm},{2. cm}) arc (180:360:2cm);
  \draw[line width =0.1pt] (-2cm,0)--(2cm,0);
  \draw[red,dashed,line width=0.1pt] (-2,-2.2)--(-2,2.2);
  \draw[red,dashed,line width=0.1pt] (2,-2.2)--(2,2.2);
  \node[text width =2.37cm] at (0,-2) {seems perfect without shifts};
\end{tikzpicture}

By adding a linewidth we will find out that we need shifts to fix the distances;

\begin{tikzpicture}[scale=1]
    \draw[line width =0.1pt] (0,0) circle (4.5cm);
  \draw[line width =0.1pt] ({-2 cm},{2. cm}) arc (180:360:2cm);
  \draw[line width =0.1pt] (-2cm,0)--(2cm,0);
  \draw[line width =0.4cm,opacity=0.3,blue] (0,0) circle (4.5cm);
  \draw[line width =0.4cm,opacity=0.3,blue] ({-2 cm},{2. cm}) arc (180:360:2cm);
  \draw[line width =0.4cm,opacity=0.3,blue] (-2cm,0)--(2cm,0);
  \draw[red,dashed,line width=0.1pt] (-2,-2.2)--(-2,2.2);
    \draw[red,dashed,line width=0.1pt] (2,-2.2)--(2,2.2);
  \node[text width =2.37cm] at (0,-2) { now we need shifts};
\end{tikzpicture}

\begin{tikzpicture}[scale=1]
  \draw[line width =0.1pt] (0,0) circle (4.5cm);
  \draw[line width =0.1pt] (-1.8cm,{2. cm}) arc (180:360:{1.8cm});
  \draw[line width =0.1pt] (-2cm,0)--(2cm,0);
  \draw[red,line width=0.1pt] (2,-2.)--(2,2.2);
  \draw[line width =0.4cm,opacity=0.3,blue] (0,0) circle (4.5cm);
  \draw[line width =0.4cm,opacity=0.3,blue] ({-2cm +0.5\pgflinewidth},{2. cm}) arc (180:360:{2cm-0.2 cm});
  \draw[line width =0.4cm,opacity=0.3,blue] (-2cm,0)--(2cm,0);
  \draw[red,line width=0.1pt] (-2cm,-2.2cm)--(-2cm,2.2cm);
  \node[text width =2.37cm] at (0,-2) {reduced arc radius by 0.5*linewidth... also shifted x of its start by the same amount};
\end{tikzpicture}

\begin{tikzpicture}[scale=1]
  \draw[line width =0.1pt] (0,0) circle (4.5cm);
  \draw[line width =0.1pt] (-1.8cm,{2.2 cm}) arc (180:360:1.8cm);
  \draw[line width =0.1pt] (-2cm,0)--(2cm,0);
  \draw[red,line width=0.1pt] (2,-2.2)--(2,2.2);
  \draw[line width =0.4cm,opacity=0.3,blue] (0,0) circle (4.5cm);
  \draw[line width =0.4cm,opacity=0.3,blue] ({-2 cm+0.2 cm},{2 cm+0.2 cm}) arc (180:360:{2cm-0.2 cm});
  \draw[line width =0.4cm,opacity=0.3,blue] (-2cm,0)--(2cm,0);
  \draw[red,line width=0.1pt] (-2,-2.2)--(-2,2.2);
  \node[text width =2.37cm] at (0,-2) {shifted arc's y start by 0.5*linewidth};
\end{tikzpicture}

\pagebreak

After the shifts we adding the missing items of the figure:

\begin{tikzpicture}[scale=1]
  \draw[line width =0.4cm,opacity=0.3,blue] (0,0) circle (4.5cm);
  \draw[line width =0.4cm,opacity=0.3,blue] (0,0) circle (5.3cm);
  \draw[line width =0.4cm,opacity=0.3,blue] ({-2cm+0.2 cm},{2.2 cm}) arc (180:360:2cm-0.2 cm);
  \draw[line width =0.4cm,opacity=0.3,blue] ({-2 cm+0.2 cm},{-2.2 cm}) arc (180:0:2cm-0.2 cm);
  \draw[line width =0.4cm,opacity=0.3,blue] (-2cm,0)--(2cm,0);
  \draw[line width =0.4cm,opacity=0.3,blue] (0cm,-2.2cm)--(0cm,2.2cm);
  \draw[red,line width=0.1pt] (-2cm,-2.2cm)--(-2cm,2.2cm);
    \draw[red,line width=0.1pt] (2cm,-2.2cm)--(2cm,2.2cm);
\end{tikzpicture}

But after scaling we will find out a problem\\(line width didn't scaled and leads to problems):

\begin{tikzpicture}[scale=0.5]
  \draw[line width =0.4cm,opacity=0.3,blue] (0,0) circle (4.5cm);
  \draw[line width =0.4cm,opacity=0.3,blue] (0,0) circle (5.3cm);
  \draw[line width =0.4cm,opacity=0.3,blue] ({-2cm+0.2 cm},{2.2 cm}) arc (180:360:2cm-0.2 cm);
  \draw[line width =0.4cm,opacity=0.3,blue] ({-2 cm+0.2 cm},{-2.2 cm}) arc (180:0:2cm-0.2 cm);
  \draw[line width =0.4cm,opacity=0.3,blue] (-2cm,0)--(2cm,0);
  \draw[line width =0.4cm,opacity=0.3,blue] (0cm,-2.2cm)--(0cm,2.2cm);
  \draw[red,line width=0.1pt] (-2cm,-2.2cm)--(-2cm,2.2cm);
    \draw[red,line width=0.1pt] (2cm,-2.2cm)--(2cm,2.2cm);
 \end{tikzpicture}

\pagebreak

So, we will fix scaling problem this way\\(adding a variable for scaling):

\def\sc{1.2}
\begin{tikzpicture}[line width={0.4*\sc cm},scale=\sc]
  \draw[opacity=0.3,blue] (0,0) circle (4.5cm);
  \draw[opacity=0.3,blue] (0,0) circle (5.3cm);
  \draw[opacity=0.3,blue] ({-2cm+0.2 cm},{2.2 cm}) arc (180:360:{2cm-0.2cm});
  \draw[opacity=0.3,blue] ({-2 cm+0.2cm},{-2.2 cm}) arc (180:0:{2cm-0.2cm});
  \draw[opacity=0.3,blue] (-2cm,0)--(2cm,0);
  \draw[opacity=0.3,blue] (0cm,-2.2cm)--(0cm,2.2cm);
  \draw[red,line width=0.1pt] (-2cm,-2.2cm)--(-2cm,2.2cm);
    \draw[red,line width=0.1pt] (2cm,-2.2cm)--(2cm,2.2cm);
 \end{tikzpicture}


Now scaling is perfect too:

\def\sc{0.5}
\begin{tikzpicture}[line width={0.4*\sc cm},scale=\sc]
  \draw[opacity=0.3,blue] (0,0) circle (4.5cm);
  \draw[opacity=0.3,blue] (0,0) circle (5.3cm);
  \draw[opacity=0.3,blue] ({-2cm+0.2 cm},{2.2 cm}) arc (180:360:{2cm-0.2 cm});
  \draw[opacity=0.3,blue] ({-2 cm+0.2 cm},{-2.2 cm}) arc (180:0:{2cm-0.2 cm});
  \draw[opacity=0.3,blue] (-2cm,0)--(2cm,0);
  \draw[opacity=0.3,blue] (0cm,-2.2cm)--(0cm,2.2cm);
  \draw[red,line width=0.1pt] (-2cm,-2.2cm)--(-2cm,2.2cm);
    \draw[red,line width=0.1pt] (2cm,-2.2cm)--(2cm,2.2cm);
 \end{tikzpicture}
\end{document}

部分输出:

在此处输入图片描述

在此处输入图片描述

答案2

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[line width=3mm,scale=0.5]
\draw (0,0) circle (4.5cm);
\draw (0,0) circle (5.3cm);
\node[minimum size = 2cm,anchor=center] (a){};
\draw ([xshift=\pgflinewidth]a.north west) arc (180:360:2cm and 2cm-0.5\pgflinewidth) 
(a.west) -- (a.east)(a.north) -- (a.south)
([xshift=\pgflinewidth]a.south west) arc (180:0:2cm and 2cm-0.5\pgflinewidth);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容