8

Is there a way to join a plot and a line segment in TikZ?

Here is a shape I'm trying to draw:

a complex shape

I almost happy with the result, but there is a gap between the plotted segment and the rest of the drawing:

the gap

\documentclass[12pt, border=0.5mm]{standalone}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}    
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}    
\begin{tikzpicture}[x=1mm, y=1mm]
    \def\r{10.5}
    \def\w{18}  

    \coordinate (a) at ({\r*cos(180-\w)}, {\r*sin(180-\w)});
    \coordinate (b) at ({\r*cos(\w)}, {\r*sin(\w)});

    \draw[domain=\w:{180-\w}, variable=\x, samples=250] 
        (a) to[out=270, in=180] 
        (-8, 1.25) --
        (-8, 0.75) -- 
        (-1, 0.75) -- 
        (-1, 8) to[out=20, in=250] 
        (0, 9) to[out=290, in=160] 
        (1, 8) -- 
        (1, 0.75) -- 
        (8, 0.75) -- 
        (8, 1.25) to[out=0, in=270] 
        (b) plot ({\r*cos(\x)}, {\r*sin(\x)}) -- (a);
\end{tikzpicture}
\end{document}

The percusse's solution works well:

\documentclass[12pt, border=0.5mm]{standalone}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}    
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}    
\begin{tikzpicture}[x=1mm, y=1mm]
    \def\r{10.5}
    \def\w{18}  

    \coordinate (b) at ({\r*cos(180-\w)}, {\r*sin(180-\w)});
    \coordinate (a) at ({\r*cos(\w)}, {\r*sin(\w)});
    \coordinate (c) at ({\r*cos(-\w)}, {\r*sin(-\w)});
    \coordinate (d) at ({\r*cos(-180+\w)}, {\r*sin(-180+\w)});

    \pgfmathsetmacro\t{90-\w}
    \pgfmathsetmacro\z{90+\w}

    \draw[line width=0.2mm, domain={180-\w}:\w, variable=\x, samples=250]  plot ({\r*cos(\x)}, {\r*sin(\x)}) -- 
        (a) to[out=-\t, in=0] 
        (8, 1.25) --
        (8, 0.75) -- 
        (1, 0.75) -- 
        (1, 8) to[out=160, in=290] 
        (0, 9) to[out=250, in=20]
        (-1, 8) --
        (-1, 0.75) -- 
        (-8, 0.75) -- 
        (-8, 1.25) to[out=180, in=-\z] 
        (b) -- cycle;
\end{tikzpicture}
\end{document}

I've adjusted the angles and plotted the round segment first. There is no gap even at 6400%:

no gap solution

If you wonder, why do I need a shape like this, it's a part of the Artobolevskii's tool for drawing of logarithmic spirals:

Artobolevskii's drawing tool

  • 4
    change the order, start with the plot and finish on b with --cycle ? – percusse May 30 '16 at 23:53
  • Put there a point (disk) to cover the hole. – Matsmath May 31 '16 at 0:00
  • @percusse --cycle is good for closed shapes, but I have some unclosed too. – GeMir May 31 '16 at 0:07
7

If --cycle does not work for you, one option would be to adjust the out and in angle so that the lines are at the same angle as what they are connecting to. This slightly changes the shape, so if that is not required to be that precise you can get (at 6400% zoom on the left hand side):

enter image description here

Notes:

  • On the left hand side I changed 270 to 250, but on the right hand side the required change was from 270 to 288. Not sure why the right hand side required a slightly different angle delta. For more precision you could compute the actual angle, but trial and error works pretty well.

Code:

\documentclass[12pt, border=0.5mm]{standalone}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}    
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}    
\begin{tikzpicture}[x=1mm, y=1mm, join=cap]
    \def\r{10.5}
    \def\w{18}  

    \coordinate (a) at ({\r*cos(180-\w)}, {\r*sin(180-\w)});
    \coordinate (b) at ({\r*cos(\w)}, {\r*sin(\w)});

    \draw[domain=\w:{180-\w}, variable=\x, samples=250] 
        (a) to[out=250, in=180] %% <--- Adjusted out angle here
        (-8, 1.25) --
        (-8, 0.75) -- 
        (-1, 0.75) -- 
        (-1, 8) to[out=20, in=250] 
        (0, 9) to[out=290, in=160] 
        (1, 8) -- 
        (1, 0.75) -- 
        (8, 0.75) -- 
        (8, 1.25) to[out=0, in=288] <--- Adjusted in angle here
        (b) plot ({\r*cos(\x)}, {\r*sin(\x)}) -- (a)
        ;
\end{tikzpicture}
\end{document}
  • Adjusting the angle will work for sure. I tried to avoid it too, but probably it's the best solution. Thanks. – GeMir May 31 '16 at 8:21

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.