我尝试画一个量角器。但无法沿弧线画出某种分隔符。尤其是较小的半径对我来说似乎是一个问题 :(
以下是 MWE:
\documentclass{scrartcl}
\usepackage[english]{babel}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[color=black] (1,1)--(5,1)--(5,1.5)--(4.5,1.5) arc[radius = 1.5, start angle = 0, end angle = 180]--(1,1.5)--(1,1);
\draw (4,1.5) arc[radius = 1, start angle = 0, end angle = 180]--(4,1.5);
\foreach \angle in {0, 30, 60, 90, 120, 150, 180} {
\draw[line width=1pt] (\angle:1.8cm) -- (\angle:2cm);
}
\end{tikzpicture}
\end{document}
我怎样才能改变刻度的位置,直至圆弧,并改变分隔圆弧的大小?
最终的图片看起来应该是这样的:
答案1
\documentclass[tikz, margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[color=black] (1,1)--(5,1)--(5,1.5)--(4.5,1.5) arc[radius = 1.5, start angle = 0, end angle = 180]--(1,1.5)--(1,1);
\draw (4,1.5) arc[radius = 1, start angle = 0, end angle = 180]--(4,1.5);
\foreach \angle in {0, 30, 60, 90, 120, 150, 180} {
\draw[red,line width=1pt] (3,1.5) ++ (\angle:1.3cm) -- ++ (\angle:0.2);% <-- changed
}
\end{tikzpicture}
\end{document}
在极坐标系中,如果你没有定义起点,则从坐标 (0,0) 开始。因此,在上述解决方案中,起点偏移 ,(3,1.5) ++ (\angle:1.3cm)
并且还使用 的相对定位-- '' (-- ++ (\angle:0.2);
。
另一个可能的解决方案是重新绘制量角器,以便起点位于弧的开始处而不是其左下角:参见@cfr 的答案。
题外话:在绘制圆弧时,可以使用较短的符号:(<start angle>:<end angle>:<radious>)
,例如:
\draw (1.5,0) arc (0:180:1.5) -| ++ (-0.5,-0.5) -| ++ (4,0.5) -- cycle;
\draw (1.0,0) arc (0:180:1) -- cycle;
(其中假设“分隔符”的绘制是通过以下方式完成的:
\foreach \angle in {0, 30, 60, 90, 120, 150, 180}
{
\draw (\angle:1.3cm) -- ++ (\angle:0.2);
}
附录: 为了娱乐:
\documentclass[tikz, margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (1.5,0) arc (0:180:1.5) -| ++ (-0.5,-0.5) -| ++ (4,0.5) -- cycle;
\draw (1.0,0) arc (0:180:1) -- cycle;
\foreach \angle in {0, 30,...,180}
{
\draw (\angle:1.25) node[font=\tiny,sloped,rotate=-90+\angle] {\angle}
++ (\angle:0.1) -- ++ (\angle:0.15);
}
\foreach \angle in {0, 10,...,180}
{
\draw (\angle:1.4) -- ++ (\angle:0.1);
}
\draw (0,0) -- ++ (0,-0.1);
\end{tikzpicture}
\end{document}
这使:
答案2
如果将圆弧底边置于中心(0,0)
,则添加标记等会更容易。例如:
\documentclass[tikz,border=10pt,10pt]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (1.5,0) arc[radius = 1.5, start angle = 0, end angle = 180] -| ++(-.5,-.5) -| ++(4,.5) --cycle;
\draw (1,0) arc[radius = 1, start angle = 0, end angle = 180] -- cycle;
\foreach \angle in {0, 30, 60, 90, 120, 150, 180} {
\draw [line width=1pt] (\angle:1.3cm) -- (\angle:1.5cm);
}
\end{tikzpicture}
\end{document}
答案3
默认情况下,极坐标系以 为中心开始绘制(0,0)
,因此,您需要将分隔符移动 以(3cm,1.5cm)
移动到新的中心。这在 中很容易实现scope
。
\documentclass{scrartcl}
\usepackage[english]{babel}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[color=black] (1,1)--(5,1)--(5,1.5)--(4.5,1.5)coordinate(o) arc[radius = 1.5, start angle = 0, end angle = 180]--(1,1.5)--(1,1);
\draw (4,1.5) arc[radius = 1, start angle = 0, end angle = 180]--(4,1.5);
\begin{scope}[shift={(3cm,1.5cm)}]
\foreach \angle in {0, 30, 60, 90, 120, 150, 180}
\draw[line width=1pt] (\angle:1.5cm) -- (\angle:1.3cm);
\end{scope}
\end{tikzpicture}
\end{document}
答案4
只是为了与 Tikz 解决方案进行比较,这是我之前做的元帖子+luamplib
. 用 编译lualatex
。
\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\usepackage{fontspec}
\setmainfont{TeX Gyre Heros}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
color plastic; plastic = 1/256(215,205,190);
path lower_base, upper_base, inner_arc, outer_arc;
upper_base = (left--right) scaled 5.2 cm;
lower_base = upper_base shifted (0,-1.6cm);
inner_arc = halfcircle scaled 5.4cm;
outer_arc = halfcircle scaled 8.8cm;
% make the plastic body
fill lower_base -- point 1 of upper_base -- outer_arc -- point 0 of upper_base -- cycle withcolor plastic;
unfill inner_arc -- cycle;
% draw the scale along the base
for t = 0 upto 100:
x := 50-t;
draw (origin -- if x mod 10 = 0: 8 elseif x mod 5 = 0: 6 else: 5 fi up) shifted (x*mm, ypart point 0 of lower_base);
if x mod 10 = 0:
label.top(decimal (t/10) infont defaultfont scaled 0.7
rotated 180, (x*mm,ypart point 0 of lower_base + 9));
fi
endfor
% and the scale around the arc
for t = 0 upto 180:
draw (origin -- if t mod 10=0: 16 elseif t mod 5=0: 12 else: 8 fi left)
shifted point 0 of outer_arc rotated t;
if t mod 10=0:
if t=90:
label("90" infont defaultfont scaled 1.4, 26 down shifted point 2 of outer_arc);
else:
label(decimal t infont defaultfont scaled 0.7 rotated (t-90), 22 left shifted point 0 of outer_arc rotated t);
label(decimal (180-t) infont defaultfont scaled 0.7 rotated (t-90), 30 left shifted point 0 of outer_arc rotated t)
withcolor .78 red;
fi
fi
endfor
% finally mark the middle
draw origin -- 4 down;
unfill fullcircle scaled 2;
endfig;
\end{mplibcode}
\end{document}