It's a known problem in TikZ that artefacts are introduced when using the rounded corners option with a value that is larger than about half the distance between two corners (see e.g. Dynamic radius for rounded corners). Now consider the following MWE.
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=5]
\draw[rounded corners=20pt] (0,-1) to[out=110,in=-110] (0,1) to[out=-70,in=70] cycle;
\end{tikzpicture}
\end{document}
The path drawn by this code contains two corners, equivalent in every way except that one of them happens to also be the start and end point of the path. Without corner rounding, it looks like this:
Unfortunately though, when we introduce the rounded corners option, the resulting picture contains the familiar artefacts:
I guess that using cycle to close a path, does not behave the way I assumed it does. I.e. I would expect cycle to basically merge the start and end point of the path, thus creating just one corner at this merged point. Instead, it seems to behave as if there are two corners there, at zero distance from each other, making any nonzero rounding radius too large.
I've come up with two workarounds (see below for the code):
- Use a different start/end point. (E.g. one of the points halfway between the two corners where the tangent is vertical.) This is possible, but it only looks right if you get that point exactly right and you need to specify the
rounded cornersoption for each corner individually. (Making it a globaldrawoption introduces artefacts at the start/end point as before.) - Split the corners. Looking at the results of my particular implementation of this workaround, it seems that the problem is not so much to do with the
cyclefunctionality as it is with theinandoutoptions for thetooperation. Because the start/end point in this workaround still is a corner that does get rounded properly this time, and the only real difference with the original code is the-- cycleas opposed to theto[out=110,in=-90] cycleof the original code.
The code for these two workarounds and the resulting pictures are shown below.
My question then is: can I use the combination of cycle with the in/out options for the to operation together with rounded corners without having to change the start point to some point I might not know? (Obviously using a copy of the start point instead of the keyword cycle doesn't improve things here, in that case the second corner just isn't recognized as one and rounding isn't even attempted.) Or is there another straightforward way to get the TikZ picture I'm after?
% Start at one of the two points halfway between the corners
\begin{tikzpicture}[scale=5]
\draw (-0.22,0) to[out=90,in=-110,rounded corners=20pt] (0,1) to[out=-70,in=70,rounded corners=20pt] (0,-1) to[out=110,in=-90] cycle;
\end{tikzpicture}
% Introduce extra corners (split the original two)
\begin{tikzpicture}[scale=5]
\draw[rounded corners=2pt] (-0.02,-1) to[out=110,in=-110] (-0.02,1) -- (0.02,1) to[out=-70,in=70] (0.02,-1) -- cycle;
\end{tikzpicture}
Tarass' answer (https://tex.stackexchange.com/a/302811/25596) gave me an idea for a possible third workaround, using a "warm-up" line and a "cool-down" line together with the shorten option. However, it appears that shorten and rounded corners don't play together.
\begin{tikzpicture}[scale=5]
% Start point
\fill[green!80!black] (0.2,-1) circle (0.02cm);
% End point
\fill[blue!80!black] (-0.2,-1) circle (0.02cm);
% Shorten the start by 1cm, shorten the end by 0.2cm
\draw[rounded corners=20pt,shorten <=1cm,shorten >=0.2cm]
% "Warm-up"
(0.2,-1) -- (0,-1)
% Enter the path
to[out=110,in=-110] (0,1)
to[out=-70,in=70] (0,-1)
% Go around a second time, then exit the path
to[out=110,in=-110] (0,1)
to[out=-70,in=70] (0,-1)
% "Cool-down"
-- (-0.2,-1);
\end{tikzpicture}






