2

I have the following code which is generated by some script. The first and last coordinate in the path are the same. I don't know how to tell TikZ to close the path. I have tried adding --cycle but it only seems to work when the first and last coordinated are different. As a work around I can displace the last point slightly (then --cycle closes the path) but it is not an elegant solution. How can I close the path?

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}[x=1cm,y=1cm]%
\filldraw[fill=green!20!white,line width=0.5mm]
   (-4.5:1)
   arc (-4.5 : 4.5   : 1)
   arc (94.5 : 115.5 : 5.3955171743191)
   arc (205.5: 214.5 : 1)
   arc (124.5: -4.5  : 0.47697553269816)
   arc (265.5: 274.5 : 1)
   arc (184.5: 85.5  : 0.85408068546347)
   -- cycle;
  \end{tikzpicture}

\end{document}

enter image description here

Suggestion given by user3337813 does not work, the defect is better visible in this example:

\filldraw[fill=green!20!white] ( -4.5 :1)
arc ( -4.5 : 34.5 :1)
arc ( -55.5 : -64.5 : 12.706204736175 )
arc ( 205.5 : 214.5 :1)
arc ( 124.5 : -4.5 : 0.47697553269816 )
arc ( 265.5 : 274.5 :1)
arc ( 184.5 : 85.5 : 0.85408068546347 )
arc ( -4.5 : 34.5 :1)
-- cycle;
\end{tikzpicture}

enter image description here

If I remove --cycle the result looks good but technically the path is open.

2 Answers2

1

You can add the first arc as the last arc...

EDIT. The OP cites appearance of double arc as being non-desirable. Thus, an alternative approach is to break the first arc into two partial arcs, placed at the beginning and end of the circuit:

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}[x=1cm,y=1cm]%
\filldraw[fill=green!20!white,line width=0.5mm]
   (-4.5:1)
   arc (-4.4 : 4.5   : 1)% PARTIAL ARC
   arc (94.5 : 115.5 : 5.3955171743191)
   arc (205.5: 214.5 : 1)
   arc (124.5: -4.5  : 0.47697553269816)
   arc (265.5: 274.5 : 1)
   arc (184.5: 85.5  : 0.85408068546347)
   arc (-4.5 : -4.4   : 1)% PARTIAL ARC (COMPLETED)
   -- cycle;
  \end{tikzpicture}
\end{document}

enter image description here

This EDITED approach resolves the counter-case shown in the OP's edited question:

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}[x=1cm,y=1cm]%
\filldraw[fill=green!20!white] ( -4.5 :1)
arc ( 0 : 34.5 :1)
arc ( -55.5 : -64.5 : 12.706204736175 )
arc ( 205.5 : 214.5 :1)
arc ( 124.5 : -4.5 : 0.47697553269816 )
arc ( 265.5 : 274.5 :1)
arc ( 184.5 : 85.5 : 0.85408068546347 )
arc ( -4.5 : 0 :1)
-- cycle;
\end{tikzpicture}

\end{document}

enter image description here

  • That looks OK in the rasterised form but when I open the PDF in a vector graphics editor I can clearly see the double arc which also confuses some editors like affinity design. Is there no way to close the path in an elegant way? – user3337813 Jan 23 '17 at 13:14
  • @user3337813 I am no tikz expert to know, but does an abbreviated arc in closing, such as arc (-4.5 : -4.4 : 1) work better for you in this case? – Steven B. Segletes Jan 23 '17 at 13:19
  • If the arc is larger it looks like this: https://s30.postimg.org/kyqfb7zmp/stacke.png – user3337813 Jan 23 '17 at 13:26
  • @user3337813 I am not allowed to access the site. Perhaps you could append your question to show an example of things not working with this approach. – Steven B. Segletes Jan 23 '17 at 13:28
  • @user3337813 I edited to propose an alternative...break the first arc into two pieces and use as first and last segments. – Steven B. Segletes Jan 23 '17 at 13:32
  • Thanks, this seems to work. If someone knows a direct way of closing such a path without extra control points, please let me know. – user3337813 Jan 23 '17 at 13:40
0

For some reason it seems to work if you replace 85.5 by 85.6. I don't know whether this is acceptable for your application. Maybe there is a problem with cycle if the points are too close.

enter image description here

\documentclass[border=1mm,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=1cm,y=1cm]%
\draw[fill=green!20!white,line width=0.5mm]
   (-4.5:1)
   arc (-4.5 : 4.5   : 1)
   arc (94.5 : 115.5 : 5.3955171743191)
   arc (205.5: 214.5 : 1)
   arc (124.5: -4.5  : 0.47697553269816)
   arc (265.5: 274.5 : 1)
   arc (184.5: 85.6  : 0.85408068546347)
   -- cycle;
  \end{tikzpicture}
\end{document}
gernot
  • 49,614