9

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:

enter image description here

Unfortunately though, when we introduce the rounded corners option, the resulting picture contains the familiar artefacts:

enter image description here

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):

  1. 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 corners option for each corner individually. (Making it a global draw option introduces artefacts at the start/end point as before.)
  2. 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 cycle functionality as it is with the in and out options for the to operation. 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 -- cycle as opposed to the to[out=110,in=-90] cycle of 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}

enter image description here

% 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}

enter image description here


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}

enter image description here

  • Great question. -It could be even better with a couple of more pictures - the original path(without rounded corners) and... – hpekristiansen Apr 6 '16 at 12:14
  • @Hans-PeterE.Kristiansen Thanks for the suggestion, I can understand how that would be useful. So I've now added some more pictures. – Wouter Apr 6 '16 at 12:41
2

Presumably, all you want is a smooth shape passing through certain points. If you want something a little less elliptical, add more points.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[smooth cycle]
 \draw plot[tension=1] coordinates{ (0,-1) (0,1) (0.2,1) (0.2,-1)};
\end{tikzpicture}
\end{document}

demo

  • Interesting! At the moment I'm using my first workaround, with additional control through the distance option for the to path operation, but this is an elegant solution. I'm going to leave the question open though, because I'm still interested in the possibilities with a more explicitly controllable path. But for people who just need this kind of shape (like myself in this case): this is apparently explained in section 20.8 of the TikZ manual (v2.10). – Wouter Apr 6 '16 at 17:35
0

How not to draw a part of this path :

enter image description here

\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] (0,-1)
    % How no to draw this
    to[out=110,in=-110] (0,1);

\end{tikzpicture}

\end{document}
  • I like the outside-the-box thinking, but it still wouldn't be the right shape if that last line could be made invisible. However, you could make it work by going over the path a second time. I thought I might've found a third workaround this way, by adding a "warm-up" line and a "cool-down" line and then using the shorten options to get rid of these (see the updated question). Unfortunately, it doesn't quite work out, and of course this would still be a pretty long workaround, especially with more complicated paths. – Wouter Apr 6 '16 at 16:48
  • Still I can't find the way to stop drawing a path. It sometimes could be useful. – Tarass Apr 6 '16 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.