以下 TikZ 代码生成下图。这是一个很小的细节,但我不希望 3pt 粗黑线被网格覆盖。但是,如果我将网格放在前面,那么黄色填充的内部部分就不再有网格了。理想情况下,我希望有这样的显示顺序:3pt 线的外部部分(出现在下图中)> 黄色填充 > 网格。但是,这似乎要求能够直接绘制出现的 3pt 线,而我没能做到:我画了圆圈,然后在内部部分上面填充。感谢您的帮助!
\begin{tikzpicture}
\draw [line width = 3pt] (0.5,0) circle (1.5cm);
\draw [line width = 3pt] (-0.5,0) circle (1.5cm);
\fill [yellow] (0.5,0) circle (1.5cm);
\fill [yellow] (-0.5,0) circle (1.5cm);
\draw[step=5mm, help lines, color=gray!30] (-2.9,-2.9) grid (2.9,2.9);
\draw[->,thick] (-3,0)--(3,0) node[right]{$x$};
\draw[->,thick] (0,-3)--(0,3) node[above]{$y$};
\end{tikzpicture}
答案1
黄色填充覆盖了线条的一半。我通过以下方式保留了它。要在黄色填充上方制作网格,只需再次绘制它,但只在同一区域内绘制,如下所示:
\documentclass[tikz, border = 1cm] {standalone}
\begin{document}
\begin{tikzpicture}
\draw[step=5mm, help lines, color=gray!30] (-2.9,-2.9) grid (2.9,2.9);
\draw [line width = 3pt] (0.5,0) circle (1.5cm);
\draw [line width = 3pt] (-0.5,0) circle (1.5cm);
\fill [yellow] (0.5,0) circle (1.5cm);
\fill [yellow] (-0.5,0) circle (1.5cm);
\begin{scope}
\clip(-0.5,0) circle (1.5cm) (0.5,0) circle (1.5cm);
\draw[step=5mm, help lines, color=gray!30] (-2.9,-2.9) grid (2.9,2.9);
\end{scope}
\draw[->,thick] (-3,0)--(3,0) node[right]{$x$};
\draw[->,thick] (0,-3)--(0,3) node[above]{$y$};
\end{tikzpicture}
\end{document}
答案2
我建议在绘制黑色轮廓时使用剪切。
\documentclass[tikz, border=0mm]{standalone}
\usetikzlibrary{backgrounds} % for the green background filling
\begin{document}
\begin{tikzpicture}[background rectangle/.style={fill=green!10},
show background rectangle]
\fill[yellow] (0.5,0) circle[radius=1.5cm]
(-0.5,0) circle[radius=1.5cm];
\draw[step=5mm, help lines, color=gray!30] (-2.9,-2.9) grid (2.9,2.9);
\begin{scope}[line width=3pt, radius=1.5cm]
\begin{scope}
\clip (0,-2.9) rectangle (2.9,2.9);
\draw (0.5,0) circle;
\end{scope}
\begin{scope}
\clip (0,-2.9) rectangle (-2.9,2.9);
\draw (-0.5,0) circle;
\end{scope}
\end{scope}
\draw[->, thick] (-3,0) -- (3,0) node[right] {$x$};
\draw[->, thick] (0,-3) -- (0,3) node[above] {$y$};
\end{tikzpicture}
\end{document}
答案3
另一种可能性是只使用一个命令来绘制图形\path
。我用了一个命令,\pic
但这不是必需的。
然后我给你提供两种可能性:
- 设置一个
fill opacity
。 - 玩弄元素的顺序(填充、网格、绘制)。
像这样:
\documentclass[tikz,border=2mm]{standalone}
\pgfmathsetmacro\a{atan(3)} % angle
\tikzset
{%
pics/double circle/.style={
code={%
\path[pic actions] (0,{1.5*sin(\a)}) arc (\a:360-\a:1.5cm) arc (180+\a:540-\a:1.5);
}},
}
\begin{document}
\begin{tikzpicture}
% option 1, opacity
\node at (0,-3) [below] {first};
\draw[step=5mm, help lines, color=gray!30] (-2.9,-2.9) grid (2.9,2.9);
\pic [draw,line width=3pt,fill=yellow,fill opacity=0.5] {double circle};
\draw[->,thick] (-3,0)--(3,0) node[right]{$x$};
\draw[->,thick] (0,-3)--(0,3) node[above]{$y$};
% option 2, playing with the order
\begin{scope}[shift={(7,0)}]
\pic [fill=yellow] {double circle};
\draw[step=5mm, help lines, color=gray!30] (-2.9,-2.9) grid (2.9,2.9);
\draw[->,thick] (-3,0)--(3,0) node[right]{$x$};
\draw[->,thick] (0,-3)--(0,3) node[above]{$y$};
\pic [draw,line width=3pt] {double circle};
\node at (0,-3) [below] {second};
\end{scope}
\end{tikzpicture}
\end{document}
答案4
您可以计算圆弧的起始和终止角度(cos^{-1}{-0.5/1.5}、cos^{-1}{0.5/1.5} 等,并在网格后绘制圆弧:
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\fill [yellow] (0.5,0) circle (1.5cm);
\fill [yellow] (-0.5,0) circle (1.5cm);
\draw[step=5mm, help lines, color=gray!30] (-2.9,-2.9) grid (2.9,2.9);
%
\draw [line width = 3pt] (+0.5,0) ++ (109.47:1.5) arc (109.47:-109.47:1.5);
\draw [line width = 3pt] (-0.5,0) ++ ( 70.53:1.5) arc ( 70.53:+289.47:1.5);
% axis
\draw[->,thick] (-3,0)--(3,0) node[right]{$x$};
\draw[->,thick] (0,-3)--(0,3) node[above]{$y$};
\end{tikzpicture}
\end{document}