![绘制顶部边缘为虚线、底部边缘为实线的填充椭圆的更简单的方法是什么?](https://linux22.com/image/395839/%E7%BB%98%E5%88%B6%E9%A1%B6%E9%83%A8%E8%BE%B9%E7%BC%98%E4%B8%BA%E8%99%9A%E7%BA%BF%E3%80%81%E5%BA%95%E9%83%A8%E8%BE%B9%E7%BC%98%E4%B8%BA%E5%AE%9E%E7%BA%BF%E7%9A%84%E5%A1%AB%E5%85%85%E6%A4%AD%E5%9C%86%E7%9A%84%E6%9B%B4%E7%AE%80%E5%8D%95%E7%9A%84%E6%96%B9%E6%B3%95%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F.png)
我想画一个椭圆,上半部分是虚线,下半部分是实线,但也填充了。有没有直接使用命令ellipse
来做到这一点的方法\usetikzlibrary{shapes}
?
我现在做的,感觉像个小把戏,就是为上半部分和下半部分画两个圆弧,这样我就可以将一个圆弧设置为虚线,另一个不设置。然后在它们上面画一个椭圆,但没有边,只是填充。像这样
\documentclass[tikz]{standalone}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}
\def\xR{1.25}; %x radius of ellipse
\def\yR{0.5}; %y radius of ellipse
\draw [dashed,domain=0:180] plot ({\xR*cos(\x)}, {\yR*sin(\x)}); %top half
\draw [domain=-180:0] plot ({\xR*cos(\x)}, {\yR*sin(\x)}); %bottom half
\fill[gray!20] (0,0) ellipse ({\xR} and {\yR}); %overlay with ellipse
\end{tikzpicture}
\end{document}
有没有更好的方法呢?
答案1
使用弧线。我也会先填充该区域,这样看起来可能会更好一些。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\def\xR{1.25}; %x radius of ellipse
\def\yR{0.5}; %y radius of ellipse
\fill[gray!20] (0,0) ellipse ({\xR} and {\yR}); %overlay with ellipse
\draw [dashed] (-\xR,0) arc[start angle=180,end angle=0,x radius=1.25,y radius=0.5]; %top half
\draw (-\xR,0) arc[start angle=180,end angle=360,x radius=1.25,y radius=0.5]; %bottom half
\end{tikzpicture}
\end{document}
或者使用两个命令“only”。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\def\xR{1.25}; %x radius of ellipse
\def\yR{0.5}; %y radius of ellipse
\draw [dashed,fill=gray!20] (0,0) ellipse ({\xR} and {\yR});
\draw (-\xR,0) arc[start angle=180,end angle=360,x radius=1.25,y radius=0.5]; %bottom half
\end{tikzpicture}
\end{document}
您可以编写样式来划掉路径的前 50%(或任意%):
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[draw only after/.style={preaction={decorate,decoration={markings,mark=at
position 0 with {\pgfmathsetmacro\mypoff{\pgfdecoratedpathlength*#1}\xdef\mypoff{\mypoff}
\pgfmathsetmacro\mypl{\pgfdecoratedpathlength}\xdef\mypl{\mypl}}}},
postaction={draw,dash pattern=on 0pt off \mypoff on \mypl}}]
\draw [dashed,fill=gray!20,draw only after=1/2] (1.25,0)
arc[start angle=00,end angle=360,x radius=1.25,y radius=0.5];
\end{tikzpicture}
\end{document}