TransWikia.com

Fill the region bounded by two path in TikZ

TeX - LaTeX Asked on December 26, 2020

How to fill the region (with gray!50) bounded by two path in TikZ? I have tried many solutions, but they all don’t work.

begin{tikzpicture}[scale=1.7]
draw[semithick] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
draw[name path=f, semithick] (1,1) circle (1);
draw[name path=g, semithick] (0,0) arc (270:360:2);
end{tikzpicture}

Namely, I want something like this:
enter image description here

3 Answers

documentclass[tikz,border=3mm]{standalone}
usetikzlibrary{intersections}% <- not needed for the below code
begin{document}
begin{tikzpicture}[scale=1.7]
begin{scope}
 clip  (1,1) circle [radius=1];
fill[gray]  (0,0) arc[start angle=270,end angle=360,radius=2] |- cycle;
end{scope}
draw[semithick] (0,0) rectangle (2,2);
draw[name path=f, semithick] (1,1) circle [radius=1];
draw[name path=g, semithick] (0,0) arc[start angle=270,end angle=360,radius=2];
end{tikzpicture}
end{document}

enter image description here

Correct answer by user230294 on December 26, 2020

clip makes everything following only drawn inside the clip path.

documentclass[tikz, border=1cm]{standalone}
begin{document}
begin{tikzpicture}
draw (0,0) rectangle (2,2);
draw (0,0) arc (270:360:2);
draw[clip] (1,1) circle (1);
fill[gray!50] (0,0) arc (270:360:2) -- (2,0) -- cycle;
end{tikzpicture}
end{document}

Filled moon shape

Answered by hpekristiansen on December 26, 2020

An other way to do it, without the clip command, is to fill the region first, then draw the lines.

documentclass[tikz, border=1cm]{standalone}
usetikzlibrary{intersections}
begin{document}
begin{tikzpicture}[scale=1.7]
fill[gray!50] (1,1) circle (1);    % gray circle
fill[white] (0,0) arc (270:360:2) -| cycle;    % white sector over
draw[semithick] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
draw[name path=f, semithick] (1,1) circle (1);
draw[name path=g, semithick] (0,0) arc (270:360:2);
end{tikzpicture}
end{document}

This gives the following

enter image description here

Answered by Alain Remillard on December 26, 2020

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