![tikzpicture:填充多条曲线之间阴影区域的最佳实践](https://linux22.com/image/454407/tikzpicture%EF%BC%9A%E5%A1%AB%E5%85%85%E5%A4%9A%E6%9D%A1%E6%9B%B2%E7%BA%BF%E4%B9%8B%E9%97%B4%E9%98%B4%E5%BD%B1%E5%8C%BA%E5%9F%9F%E7%9A%84%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5.png)
使用下面的最小工作示例,我已经能够用 tikzpicture 制作此图像:
我想像这样在圆圈和两个外椭圆之间的区域进行阴影处理:
使用绘制 y=x^2 和 y=x可以通过一种略显拙劣的解决方案来实现这一点,即首先填充两个椭圆之间的区域,然后填充圆与填充椭圆所创建的垂直线之间的剩余区域。这感觉不是“最佳实践”。有没有更巧妙的方法来做到这一点?
这是用于生成第一幅图像的最小工作示例
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
%draw the circle
\draw (0,0) circle(1);
\draw[fill] (0,1) circle [radius =0.05];
\draw[fill] (0,-1) circle [radius =0.05];
\draw plot[domain=pi:2*pi] ({cos(\x r)},{.2*sin(\x r)});
\draw[dashed] plot[domain=0:pi] ({cos(\x r)},{.2*sin(\x r)});
%draw the upper ellipse
\draw plot[domain=pi:2*pi] ({0.864*cos(\x r)},{.2*sin(\x r)+.5});
\draw[dashed] plot[domain=0:pi] ({0.864*cos(\x r)},{.2*sin(\x r)+.5});
%draw the lower ellipse
\draw plot[domain=pi:2*pi] ({0.864*cos(\x r)},{.2*sin(\x r)-.5});
\draw[dashed] plot[domain=0:pi] ({0.864*cos(\x r)},{.2*sin(\x r)-.5});
\end{tikzpicture}
\end{document}
答案1
正如我的评论所述,使用省略号和剪辑对我来说似乎更容易。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) circle[radius=2];
%fills
\fill[blue,opacity=0.2] (0,1) ellipse[x radius=1.72,y radius=0.4];
\begin{scope}
\clip (0,0) circle[radius=2];
\fill [blue,opacity=0.6] (-1.72,1) arc[start angle = -180, end angle = 0,x radius=1.72,y radius=0.4] -- (2,1) |- (1.72,-1) arc[start angle = 0, end angle = -180,x radius=1.72,y radius=0.4] -- (-2,-1) |- cycle;
\end{scope}
% Upper ellipse
\draw (-1.72,1) arc[start angle = -180, end angle = 0,x radius=1.72,y radius=0.4];
\draw[dashed] (-1.72,1) arc[start angle = 180, end angle = 0,x radius=1.72,y radius=0.4];
% Middle ellipse
\draw (-2,0) arc[start angle = -180, end angle = 0,x radius=2,y radius=0.5];
\draw[dashed] (-2,0) arc[start angle = 180, end angle = 0,x radius=2,y radius=0.5];
% Lower ellipse
\draw (-1.72,-1) arc[start angle = -180, end angle = 0,x radius=1.72,y radius=0.4];
\draw[dashed] (-1.72,-1) arc[start angle = 180, end angle = 0,x radius=1.72,y radius=0.4];
% Dots
\filldraw (0,2) circle [radius =0.05]
(0,-2) circle [radius =0.05];
\end{tikzpicture}
\end{document}