考虑下面的代码:
\documentclass[convert = false]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
\draw[-latex] (0, 0) ellipse [x radius = 3cm, y radius = 2cm, start angle = 30,
end angle = 150];
\end{tikzpicture}
\end{document}
此代码不会产生编译错误,但它不承认起点和角度或箭头选项。我可以使用命令arc
,但是为了将arc
中心置于原点,我需要将arc
起始坐标定义为
\coordinate (P) at ($(0, 0) + (30:3cm and 2cm)$);
然后画圆弧:
\documentclass[convert = false]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw[-latex] (0, 0) ellipse [x radius = 3cm, y radius = 2cm,
start angle = 30, end angle = 150];
\coordinate (P) at ($(0, 0) + (30:3cm and 2cm)$);
\draw[thick, red, -latex] ($(0, 0) + (30:3cm and 2cm)$(P) arc
(30:150:3cm and 2cm);
\end{tikzpicture}
\end{document}
这并不是太难,但是有没有办法使用ellipse
命令来实现所需的结果?
答案1
您可以定义一种新样式,自动设置起始坐标并绘制圆弧:
\tikzset{
partial ellipse/.style args={#1:#2:#3}{
insert path={+ (#1:#3) arc (#1:#2:#3)}
}
}
然后你可以简单地说
\draw[thick, red, -latex] (0,0) [partial ellipse=30:150:3cm and 2cm];
绘制圆弧:
答案2
除了定义自定义to path
或insert path
解决方案之外,还可以重新定义 TikZ 绘制圆弧的方式。为此,我添加了arc starts
接受三个选择的键:
at last point
:这是默认行为,after moveto
:从当前位置(即圆弧中心)移动到后圆弧开始,after lineto
:圆弧从当前位置(即圆弧的中心)开始画一条线之后开始。
但此实现仅影响arc [ ]
语法。
将其设置为键的优点是它可以应用于范围或路径,并且可以在其他样式中使用。
代码
\documentclass[tikz]{standalone}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\tikz@arc@opt}{\xdef}{\tikz@arc@do\xdef}{}{}
\let\tikz@arc@do\relax
\tikzset{
arc starts/.cd, .is choice,
at last point/.code=\let\tikz@arc@do\relax,
after moveto/.code=\tikz@arc@do@\pgfpathmoveto,
after lineto/.code=\tikz@arc@do@\pgfpathlineto}
\def\tikz@arc@do@#1{%
\def\tikz@arc@do{\tikz@@@parse@polar{\tikz@arc@do@@#1}(\tikz@s%
:\pgfkeysvalueof{/tikz/x radius} and \pgfkeysvalueof{/tikz/y radius})}}
\def\tikz@arc@do@@#1#2{#1{\pgfpointadd{#2}{\tikz@last@position@saved}}}
\makeatother
\begin{document}
\begin{tikzpicture}[x radius=1, y radius=.6]
\coordinate (C) at (rand,rand);
\draw[ultra thick] (C) ellipse [];
\draw[green] ([shift=(30:1 and .6)] C)
arc [start angle=30, end angle=150];
\draw[red] (C) -- ++ (180:1 and .6)
arc [start angle=180, delta angle=70] -- cycle;
\draw[blue!50] (C) ++ (270:1 and .6)
arc [start angle=270, delta angle=80] -- cycle;
\end{tikzpicture}
\begin{tikzpicture}[x radius=1, y radius=.6, arc starts=after moveto]
\coordinate (C) at (rand,rand);
\draw[ultra thick] (C) ellipse [];
\draw[green] (C) arc [start angle= 30, end angle=150];
\draw[red] (C) arc [arc starts=after lineto,
start angle=180, delta angle= 70] -- cycle;
\draw[blue!50] (C) arc [start angle=270, delta angle= 80] -- cycle;
\end{tikzpicture}
\end{document}