TransWikia.com

Tikz: even odd rule with intersecting shapes

TeX - LaTeX Asked on July 28, 2021

I do not know how to use the even odd rule to fill intersecting shapes. This is the desired outcome:

enter image description here

And this is my code. I’d appreciate an explanation of how to apply the even odd rule, when to use clip and when to use scope, which I have seen used in simpler examples of this kind. Thanks!

documentclass[border=3pt,tikz]{standalone}

% define the angle theta = pi/7
deftheta{25.71}
% define the circle of radius 1
defshapeA{(0,0) circle (1cm)}
% define the annulus of radii 1 and 2
defshapeB{(0,0) circle (1cm) (0,0) circle (2cm)}
% define the annulus of radii 2 and 3
defshapeC{(0,0) circle (2cm) (0,0) circle (3cm)}
% define the intersecting lines
% attempted to group them for even-odd rule
%defshapeD{(180+theta:3.5cm) -- (0,0) -- (theta:3.5cm)}
%defshapeE{(-theta:3.5cm) -- (0,0) -- (180-theta:3.5cm)}
defshapeD{(-theta:3.5cm) -- (0,0) -- (theta:3.5cm)}
defshapeE{(180+theta:3.5cm) -- (0,0) -- (180-theta:3.5cm)}

begin{document}

begin{tikzpicture}[thick]
  fill[fill=lightgray, even odd rule]  shapeE shapeB;
  fill[fill=lightgray, even odd rule]  shapeD shapeA;
  draw shapeA shapeB shapeC shapeD shapeE;
end{tikzpicture}

end{document}

enter image description here

2 Answers

You complicated too much.

even odd rule on multiple shapes

documentclass[border=3pt,tikz]{standalone}

% define the angle theta = pi/7
deftheta{25.71}
defshapeA{(0,0) circle (1cm)}
defshapeB{(0,0) circle (2cm)}
defshapeC{(0,0) circle (3cm)}

defshapeD{(-theta:3.5cm) -- (0,0) -- (theta:3.5cm)}
defshapeE{(180+theta:3.5cm) -- (0,0) -- (180-theta:3.5cm)}

begin{document}

begin{tikzpicture}[thick]
    begin{scope}
        clip shapeC;
        fill[fill=lightgray, even odd rule]  shapeE shapeB shapeD shapeA;
    end{scope}
  
  draw shapeA shapeB shapeC shapeD shapeE;
end{tikzpicture}

end{document}

Just declare all your shapes, in a simple way (here, for example, the circles needed to be declared as they are, not as annuli like you tried to). Then fill with even odd rule all the shapes together.

As for the clip, here you don't want to fill outside the larger circle, so you scope the filling with a clip shapeC;, meaning that, inside your scope environment, nothing is drawn outside shapeC. And you finally draw your lines outside the scope environment to let them be drawn outside the larger circle.

Correct answer by SebGlav on July 28, 2021

Since there is already a nice and accepted answer, I feel free to add a solution using nofill and eofill from MetaPost/MetaFun. Mostly for fun, but I hope someone could have a use for it! The file is compiled with context.

startMPpage[offset=3bp]
u:=1cm;

path circle[],line[];

for i=0 upto 2:
circle[i] = fullcircle scaled (2(i+1)*u);
endfor

line0 = ((-1,0)--(1,0)) rotated 360/28 scaled 4u;
line1 = line0 rotated (180-360/14);

nofill circle1;
nofill line0 -- line1 -- cycle;
eofill circle0 withcolor lightgray;
clip currentpicture to circle2;

for i=circle0,circle1,circle2,line0,line1:
draw i;
endfor
stopMPpage

The result shows as follows:

Result, which looks like the image in the question

Answered by mickep on July 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP