我正在复制在线获得的代码,以剪切两个封闭路径的交点并用特定颜色填充。它没有报告任何警告或错误,但我看不到剪切工作。
\usepackage{tikz}
\usetikzlibrary{hobby}
\def\A{(-1,0) circle (1.5cm);}
\def\B{(0,0) circle (1.2cm);}
\begin{tikzpicture}[use Hobby shortcut,closed=true]
\fill[gray!50] (-3,-2) rectangle (3,2);
\begin{scope}[fill opacity=0.5]
\fill[color=blue]\A;
\fill[color=red] \B;
\draw \A;
\draw \B;
\end{scope}
\begin{scope}[fill opacity=1]
\clip \B;
\fill[color=white] \A;
\end{scope}
\end{tikzpicture}
代码会用白色突出显示相交部分。但如果我想突出显示蓝色和红色之外的任何部分,但在矩形内,剪辑可以做到这一点吗?
答案1
这是使用invclip
密钥的另一种方法(来自https://tex.stackexchange.com/a/59168/14500):
\documentclass{standalone}
\usepackage{tikz}
\tikzset{invclip/.style={clip,insert path={{[reset cm]
(-16383.99999pt,-16383.99999pt) rectangle (16383.99999pt,16383.99999pt)}}}}
\begin{document}
\begin{tikzpicture}
\def\A{(-1,0) circle (1.5cm)}
\def\B{(0,0) circle (1.2cm)}
\begin{scope}
\begin{pgfinterruptboundingbox}
\clip[invclip] \B;
\end{pgfinterruptboundingbox}
\fill[fill=blue!50]\A;
\end{scope}
\begin{scope}
\begin{pgfinterruptboundingbox}
\clip[invclip] \A;
\end{pgfinterruptboundingbox}
\fill[fill=red!50]\B;
\end{scope}
\begin{scope}
\begin{pgfinterruptboundingbox}
\clip[invclip] \A \B;
\end{pgfinterruptboundingbox}
\filldraw[gray!50] (current bounding box.south west) rectangle (current bounding box.north east);
\end{scope}
\draw \A;
\draw \B;
\end{tikzpicture}
\end{document}
答案2
请注意,奇偶规则不会剪切两个圆的并集。
线条一半在区域内绘制,一半在区域外绘制,因此,除非您想将其宽度减少一半,否则请最后绘制它们。
此外,您提供了两个分号,第一个分号之后的所有内容都会被忽略,直到下一个分号\path
等等。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{hobby}
\begin{document}
\def\A{(-1,0) circle (1.5cm)}
\def\B{(0,0) circle (1.2cm)}
\begin{tikzpicture}[use Hobby shortcut,closed=true]
\begin{scope}[fill opacity=0.5]
\fill[color=blue]\A;
\fill[color=red] \B;
\end{scope}
\begin{scope}[even odd rule]
\clip (-3,-2) rectangle (3,2) \B \A;
\fill[gray!50] (-3,-2) rectangle (3,2);
\end{scope}
\draw \A;
\draw \B;
\end{tikzpicture}
\end{document}
答案3
calc
很容易就能确定 和 的并集轮廓intersections
。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,intersections}
\begin{document}
\def\A{(-1,0) circle [radius=1.5cm];}
\def\B{(0,0) circle [radius=1.2cm];}
\begin{tikzpicture}
\begin{scope}[fill opacity=0.5]
\fill[color=blue]\A;
\fill[color=red] \B;
\end{scope}
\draw[name path=A] \A;
\draw[name path=B] \B;
\begin{scope}[fill opacity=1]
\path[name intersections={of=A and B},fill=orange]
let \p1=($(intersection-1)-(-1,0)$),\p2=($(intersection-1)$),
\n1={atan2(\y1,\x1)},\n2={atan2(\y2,\x2)} in
(intersection-1) arc[start angle=\n1,end angle=360-\n1,radius=1.5]
arc[start angle=360-\n2,end angle=\n2+360,radius=1.2]
-- cycle (-3,-2) rectangle (1.7,2) ;
\end{scope}
\end{tikzpicture}
\end{document}
答案4
尝试使用 Asymptote
size(300);
unitsize(1cm);
path c1=circle((-1,0),1.5),c2=circle(0,1.2);
picture pic;
fill(c1,blue);
fill(c2,red);
fill(pic,c1,white);
clip(pic,c2);
add(pic);
draw(c1^^c2,black+0.7bp);
shipout(bbox(2mm,Fill(.5green+.6blue)));
尝试使用 PSTricks
\documentclass[pstricks,12pt]{standalone}
\begin{document}
\begin{pspicture}[linewidth=.7bp](-3,-2)(2,2)
{
\psset{linestyle=none}
\psframe[fillstyle=solid,fillcolor=green!50!blue](-3,-2)(2,2)
\pscircle[fillcolor=blue,fillstyle=solid](-1,0){1.5cm}
\psclip{
\pscircle[fillcolor=red,fillstyle=solid](0,0){1.2cm}}
\pscircle[fillcolor=white,fillstyle=solid](-1,0){1.5cm}
\endpsclip
}
\pscircle(-1,0){1.5cm}
\pscircle(0,0){1.2cm}
\end{pspicture}
\end{document}