5

I am copying a code obtained online to clip the intersection of two closed paths and fill it with a certain color. It does not report any warning or error but I don't see the clipping working.

  \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}

The code will highlight the intersected part in white. But if I want to high light any part outside the blue and red but in the rectangular, will the clip be able to do that?

enter image description here

user1285419
  • 1,121

4 Answers4

5

Here is another method using the invclip key (from 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}

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
4

Note the even odd rule does not clip the union of both circles.

Lines are drawn half in and half out of a region, so unless you want to reduce their width by half, draw them last.

Also, you were providing two semicolons, and everything after the first is ignored until the next \path etc.

\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}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
3

It is straightforward to determine the contour of the union with calc and 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}

enter image description here

0

Try with 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)));

Try with 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}

enter image description here