2

This question is inspired by my own answer: How to draw the crossings in a tikz picture? where I draw the blue ellipse in four stages. I was thinking that it should be possible to draw with only one \fill command. To make the correct \clip, I need to understand the default \clip or \fill non-Zero Winding Rule, the nonzero rule (the manual 15.5.2 page 181) and not the even odd rule.


In this example, we can see that circles(and ellipses) are drawn with reverse stroke compared to rectangles:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
 \draw[fill=red]  (0,0) circle (1) (0,0) rectangle (2,2);
 \draw[fill=green] (4,-1) rectangle (6,1) (5,0) rectangle (7,2);
 \draw[fill=blue] (9,0) circle (1) (9.5,0.5) circle (1);
\end{tikzpicture}
\end{document}

Overlapping circles and rectangles. The red circle and rectangle overlap is not filled


To make the correct clip, I need to select an area from four overlapping ellipses and a rectangle. I believe that it is not possible without altering the stroke direction of some of the ellipses!? It would not help to make clip use even odd rule.

Question: How do I not fill the intersection of two overlapping circles(ellipses) without the even odd rule?

MWE:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
 \fill (0,0) circle (2) (1,1) circle (2);
\end{tikzpicture}
\end{document}

Desired output: Overlapping circles without intersection fill

I can not find anything in the manual about changing the stroke direction. Can it be done? Maybe there is an other way?

Edit: I just found that for my original purpose, it does help to make clip use even odd rule (can be done with a scope). -but my question is still the same -is it possible without.

2
  • How technical are you prepared to go? My ’spath3` library contains code for reversing paths, but it was designed as a utility library for other packages to build on rather than with a simple user interface so a solution using it may not look all that elegant. Commented Oct 31, 2020 at 8:47
  • @AndrewStacey: I am not willing to dive into some technical code. I was hoping for an option, or some trick that reverses the stroke direction or otherwise makes the fill of a circle change parity. If you can explain that it is not possible without being technical or a lot of code, I will accept that as an answer. Commented Oct 31, 2020 at 16:21

2 Answers 2

2

Stroke direction of rectangle is relative to the provided points. For example, in your first code, try (5,2) rectangle (7,0) instead of (5,0) rectangle (7,2). With circle, you can use circle(-2 and 2) instead of circle(2).

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[fill=red]  (0,0) circle (1) (0,0) rectangle (2,2);
  \draw[fill=green] (4,-1) rectangle (6,1) (5,2) rectangle (7,0);
  \draw[fill=blue] (9,0) circle (1) (9.5,0.5) circle (-1 and 1);
\end{tikzpicture}
\end{document}

enter image description here

3
  • So simple - thanks. Is circle (\rx and \ry) the same as ellipse (\rx and \ry)? Commented Nov 1, 2020 at 16:31
  • Note that the circle (x and y) syntax is depreciated, you should use circle[x radius=..., y radius=] instead. Commented Nov 1, 2020 at 19:12
  • @AndrewStacey: Thanks. I reread the manual about circles. I will try to remember using the better notation on this site. -also I did not know that it was possible to give two different radii to circle - making ellipse superfluous. Commented Nov 1, 2020 at 19:34
3

My spath3 library was designed to do low level manipulation of paths, including reversing the stroke direction. The public interface is a little basic as the intention was to provide functionality that could be used in other packages (such as the knot and calligraphy TikZ libraries). Here's a proof of concept.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/568999/86}

\usepackage{tikz}
\usepackage{spath3}
\begin{document}
\begin{tikzpicture}
\path[save spath=circle1]  (0,0) circle[radius=2];
\path[save spath=circle2] (1,1) circle[radius=2];
 \fill (0,0) [insert spath=circle1] (1,1) [insert reverse spath=circle2];
\end{tikzpicture}
\end{document}

Reversed path used in a fill

Edit by OP: I believe that spath3 syntax has been updated. A working code is now:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{spath3}
\begin{document}
\begin{tikzpicture}
\path[spath/save=circle1]  (0,0) circle[radius=2];
\path[spath/save=circle2] (1,1) circle[radius=2];
\fill (0,0) [spath/insert=circle1] (1,1) [spath/insert reverse=circle2];
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .