我正在尝试创建一个家庭作业,我想画一些标有半径和角度的圆。以下代码可以绘制一个半径为 5 且角度为 120 度的圆。
\begin{tikzpicture}[thick,font=\sffamily\Large]
\draw (0,0) circle (5cm);
\draw[very thick,fill=blue!30] (0,0) -- (120:5) arc(120:0:5) -- cycle;
\draw[latex-latex] (120:1.2) arc(120:0:1.2) node[above=5pt, midway]{$120^{\circ}$};
\draw[very thick,-latex] (0,0) -- (0:5) node[midway,below=2pt]{$5$};
\end{tikzpicture}
我的问题是:有没有办法用弧度输入来做同样的事情?我想出了一种计算 pi/2 的方法,但它不适用于 5pi/4 之类的数字。
答案1
您可以使用deg()
函数将弧度转换为度。因此,deg(5*pi/4)
结果为225
。
请注意,不建议使用arc
andcircle
语法。使用较新的语法 and deg(5*pi/4)
,您的图表将如下所示:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick, font=\sffamily\Large]
\draw (0,0) circle[radius=5];
\draw[very thick, fill=blue!30]
(0,0) -- ({deg(5*pi/4)}:5)
arc[start angle={deg(5*pi/4)}, end angle=0, radius=5] -- cycle;
\draw[latex-latex]
({deg(5*pi/4)}:1.2)
arc[start angle={deg(5*pi/4)}, end angle=0, radius=1.2]
node[above=5pt, midway] {$225^{\circ}$};
\draw[very thick, -latex] (0,0) -- (0:5)
node[midway, below=2pt]{$5$};
\end{tikzpicture}
\end{document}
因为我非常喜欢\pic
s:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\tikzset{
pics/circle with segment/.style 2 args={
code={
\draw (0,0) circle[radius=#2];
\draw[pic actions]
(0,0) -- ({deg(#1)}:#2)
arc[start angle={deg(#1)}, end angle=0, radius=#2] -- cycle;
\draw[<->]
({deg(#1)}:1.2)
arc[start angle={deg(#1)}, end angle=0, radius=1.2]
node[midway, shift={({deg(#1)/2}:15pt)}] {
\pgfmathparse{deg(#1)}
$\pgfmathprintnumber{\pgfmathresult}^{\circ}$
};
\draw[->] (0,0) -- (0:#2)
node[midway, below=2pt]{
\pgfmathparse{#2}
$\pgfmathprintnumber{\pgfmathresult}$
};
}
}
}
\begin{document}
\begin{tikzpicture}[thick, >=latex, font=\sffamily\Large]
\pic[fill=blue!30] {circle with segment={5*pi/4}{5}};
\pic[fill=red!30] at (10,0) {circle with segment={pi/2}{3}};
\end{tikzpicture}
\end{document}
对于显示弧度的标签,有一个(可能不是最佳的)解决方案:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\ExplSyntaxOn
% transforms any number of the form x, x*pi, pi/x or x*pi/y
% into properly styled math-mode
% where x and y can be any number, with or without decimal separator (dot)
\seq_new:N \l_topifrac_temp_seq
\NewDocumentCommand{\topifrac}{ m }{
\regex_extract_once:nnN { \A(\d*\.?\d+)?(\*?pi)?(/(\d*\.?\d+))?\Z }
{ #1 } \l_topifrac_temp_seq
\seq_item:Nn \l_topifrac_temp_seq { 2 }
\tl_if_blank:eTF { \seq_item:Nn \l_topifrac_temp_seq { 5 } }
{ \tl_if_blank:eTF { \seq_item:Nn \l_topifrac_temp_seq { 3 } }
{ }
{ \pi }
}
{ \frac{\pi}{ \seq_item:Nn \l_topifrac_temp_seq { 5 } } }
}
\ExplSyntaxOff
\tikzset{
pics/circle with segment/.style 2 args={
code={
\draw (0,0) circle[radius=#2];
\draw[pic actions]
(0,0) -- ({deg(#1)}:#2)
arc[start angle={deg(#1)}, end angle=0, radius=#2] -- cycle;
\draw[<->]
({deg(#1)}:1.2)
arc[start angle={deg(#1)}, end angle=0, radius=1.2]
node[midway, shift={({deg(#1)/2}:15pt)}] {
$\topifrac{#1}$
};
\draw[->] (0,0) -- (0:#2)
node[midway, below=2pt]{
\pgfmathparse{#2}
$\pgfmathprintnumber{\pgfmathresult}$
};
}
}
}
\begin{document}
\begin{tikzpicture}[thick, >=latex, font=\sffamily\Large]
\pic[fill=blue!30] {circle with segment={5*pi/4}{5}};
\pic[fill=red!30] at (10,0) {circle with segment={pi/2}{3}};
\end{tikzpicture}
\end{document}