TransWikia.com

Reverse TikZ stroke direction to use with fill rules

TeX - LaTeX Asked on April 12, 2021

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 Answers

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

Correct answer by Paul Gaborit on April 12, 2021

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

Answered by Andrew Stacey on April 12, 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