我在使用 tikz 时遇到以下问题:我想绘制一些圆弧,但不幸的是这些圆弧的角度超过了 360° 线。我希望能够用一个命令绘制这些圆弧,但我不知道该怎么做。如果我给圆弧命令两个角度,其中第一个大于第二个,tikz 会在绘制它们之前简单地交换角度。
% radius, two angles, node attribute/name
\newcommand{\drawarc}[5]
{
\draw[gray, very thin] (0,0) -- (#2:#1);
\draw (#2:#1) arc(#2:#3:#1);
\draw[gray, very thin] (0,0) -- (#3:#1);
\draw({(#2+#3)/2}:#1) node[#4] {#5};
}
\begin{tikzpicture}[scale=4]
\drawarc{1cm}{80}{100}{above}{1};
\drawarc{0.9cm}{60}{110}{below}{2};
\drawarc{0.75cm}{20}{85}{below left}{4};
\drawarc{0.8cm}{95}{150}{below right}{3};
\drawarc{1cm}{105}{200}{above}{5};
% I would like to replace these 2 commands
% by one, drawing an arc from 195 to 75 degrees
\drawarc{1.2cm}{195}{360}{above}{6};
\drawarc{1.2cm}{0}{75}{}{};
\drawarc{1.3cm}{160}{280}{left}{8};
\drawarc{0.9cm}{140}{190}{right}{7};
% and the same here
\drawarc{1cm}{260}{360}{above left}{9};
\drawarc{1cm}{0}{40}{}{};
\end{tikzpicture}
我用从/到 (0,0) 的线来限制每个圆弧,因此如果我在只需要一个圆弧的地方绘制多个圆弧,那么额外的线看起来会有些糟糕。
答案1
您可以使用大于 360 度的角度!
用以下两行替换您的 4 行:
\drawarc{1.2cm}{195}{435}{above}{6};
%\drawarc{1.2cm}{0}{75}{}{}; 360+75=435
和
\drawarc{1cm}{260}{400}{above left}{9};
%\drawarc{1cm}{0}{40}{}{}; 360+40=400
得到期望的结果:
答案2
要执行所需的操作,可以使用delta angle
弧线选项。 你要写的是
\draw (195:1.2) arc[radius=1.2,start angle=195,delta angle=240]
如果您想反过来,请使用 -240 而不是 240。
或者
使用Yossi的解决方案,end angle = 435
。
答案3
该死,半个小时过去了,突然就有三个答案了。无论如何,这是我的答案:
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{ifthen}
\begin{document}
% radius, two angles, node attribute/name
\newcommand{\drawarc}[5]{%
\ifthenelse{#3>#2}%
% if second anvle is bigger, then as usual
{\draw (#2:#1) arc(#2:#3:#1);
\draw({(#2+#3)/2}:#1) node[#4] {#5};}%
% otherwise, draw two arcs, and also adapt the node position;
% colors are just for illustration, just remove the [red] and [blue]
{\draw[red] (#2:#1) arc(#2:360:#1);
\draw[blue] (360:#1) arc(0:#3:#1);
\draw({(#2+#3)/2+180}:#1) node[#4] {#5};}%
% connections to arc ends, un changed
\draw[gray, very thin] (0,0) -- (#2:#1);%
\draw[gray, very thin] (0,0) -- (#3:#1);%
}
\begin{tikzpicture}[scale=2]
% old "solution
\drawarc{1.2cm}{195}{360}{above}{6};
\drawarc{1.2cm}{0}{75}{right}{7};
% new example
\drawarc{1.7cm}{195}{75}{below}{8};
% free bonus pacman!
\drawarc{2.7cm}{225}{135}{right}{Waka Waka!};
\end{tikzpicture}
\end{document}
答案4
我不会用这种方式定义命令,而是会按照下面的方法。它不仅会自动按照你定义的角度以正确的方式执行,而且还会使中间连接更加漂亮的。
\newcommand{\drawarc}[5]{
\ifnum#3<#2
\draw (#2:#1) arc(#2:360:#1) arc(0:#3:#1);
\node[#4] at ({(#2+360+#3)/2}:#1) {#5};
\else
\draw (#2:#1) arc(#2:#3:#1);
\node[#4] at ({(#2+#3)/2}:#1) {#5};
\fi
\draw[gray, very thin,rounded corners=.1pt] (#3:#1) -- (0,0) -- (#2:#1);
}
我改变了放置节点的方式。至少我认为这样更简洁。进一步注意,rounded corners
这使得中心部分看起来很多更好的。