20

I came across the following to draw a rope connecting two points. Is this a custom-made object, or is this something that came from a package?

If it is custom-made, what is the best way of constructing something like this without the code being overly convoluted?

enter image description here

  • 1
    What makes you think that this was drawn with TikZ? – Henri Menke Oct 9 '18 at 11:42
  • 6
    Questions asking us to recommend or find a package, font, tool, book or other off-site resource are off-topic as they usually do not revolve around an abstract issue. Instead, describe the problem and what has been done so far to solve it or, if applicable, ask on Software Recommendations SX. – Henri Menke Oct 9 '18 at 11:42
  • 6
    I'm voting to reopen this question. The question itself may not show any effort, but the answer is great and useful for many things. It would be a waste to hide it under a question put on hold. – user36296 Oct 9 '18 at 21:06
47

It is not too difficult to draw something along these lines.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
  \begin{tikzpicture}
    \draw[decorate,decoration={markings,
    mark=between positions 2mm and \pgfdecoratedpathlength-2mm step 2mm
      with
      {
        \draw[ultra thick,gray] 
        (-3.5mm,-1.25mm) to[out=0,in=160] (-2mm,-1.25mm) to[out=-20,in=160] 
        (2mm,1.25mm) to[out=-20,in=180] (3.5mm,1.25mm);
      }}] 
    (0,0) -- (4,-4);
    \fill (0,0) circle (3mm);   
  \end{tikzpicture}
\end{document}

enter image description here

An arguably cleaner way is to do it with decorations.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations} %  decorations.text just 4 fun

\pgfkeys{/tikz/.cd,
    rope width/.store in=\RopeWidth,
    rope width=5pt,
    rope step/.store in=\RopeStep,
    rope step=2mm,
}

\pgfdeclaredecoration{rope}{initial}
{% 
\state{initial}[width=\RopeStep,next state=cont] {
    \pgfmoveto{\pgfpoint{0pt}{-\RopeWidth/2}}
    \pgfpathcurveto
    {\pgfpoint{5*\RopeStep/6}{0.25*\RopeWidth}}
    {\pgfpoint{7*\RopeStep/6}{0.45*\RopeWidth}}
    {\pgfpoint{1.5*\RopeStep}{\RopeWidth/2}}
     \pgfpathcurveto
    {\pgfpoint{10*\RopeStep/6}{0.55*\RopeWidth}}
    {\pgfpoint{11*\RopeStep/6}{0.6*\RopeWidth}}
    {\pgfpoint{13.5*\RopeStep/6}{\RopeWidth/2}}
    \pgfcoordinate{lastup}{\pgfpoint{-1.5*\RopeStep/6}{-\RopeWidth/2}}
  }
  \state{cont}[width=\RopeStep]{ 
     \pgfmoveto{\pgfpointanchor{lastup}{center}}
     \pgfpathcurveto
    {\pgfpoint{-5*\RopeStep/6}{-0.6*\RopeWidth}}
    {\pgfpoint{-4*\RopeStep/6}{-0.55*\RopeWidth}}
    {\pgfpoint{-3*\RopeStep/6}{-0.55*\RopeWidth}}
     \pgfpathcurveto
    {\pgfpoint{-\RopeStep/6}{-0.45*\RopeWidth}}
    {\pgfpoint{\RopeStep/6}{-0.25*\RopeWidth}}
    {\pgfpoint{3*\RopeStep/6}{0pt}}
    \pgfpathcurveto
    {\pgfpoint{5*\RopeStep/6}{0.25*\RopeWidth}}
    {\pgfpoint{7*\RopeStep/6}{0.45*\RopeWidth}}
    {\pgfpoint{9*\RopeStep/6}{\RopeWidth/2}}
     \pgfpathcurveto
    {\pgfpoint{10*\RopeStep/6}{0.55*\RopeWidth}}
    {\pgfpoint{11*\RopeStep/6}{0.6*\RopeWidth}}
    {\pgfpoint{13.5*\RopeStep/6}{\RopeWidth/2}}
    \pgfcoordinate{lastup}{\pgfpoint{-1.5*\RopeStep/6}{-\RopeWidth/2}}
  }
  \state{final}[width=5pt]
  {
     \pgfmoveto{\pgfpointanchor{lastup}{center}}
     \pgfpathcurveto
    {\pgfpoint{-5*\RopeStep/6}{-0.6*\RopeWidth}}
    {\pgfpoint{-4*\RopeStep/6}{-0.55*\RopeWidth}}
    {\pgfpoint{-0.5*\RopeStep}{-0.55*\RopeWidth}}
     \pgfpathcurveto
    {\pgfpoint{-\RopeStep/6}{-0.45*\RopeWidth}}
    {\pgfpoint{\RopeStep/6}{-0.25*\RopeWidth}}
    {\pgfpoint{0.5*\RopeStep}{0pt}}
    \pgfmoveto{\pgfpointdecoratedpathlast}
  }
}
\begin{document}
\begin{tikzpicture}[decoration=rope]
  \draw[red,thick,decorate,rope width=8pt] (-4,0) to[out=0,in=90] (0,-4);
  \draw[gray,thick,decorate] (0,0) to (4,-4);
  \draw[blue,ultra thick,decorate,rope width=8pt] (4,0) to (8,-4);
  \fill (-4,0) circle (8pt) (0,0) circle (8pt) (4,0) circle (8pt);
\end{tikzpicture}  
\end{document}

enter image description here

All these decorations leave some room for improvement, in particular the one along the curved path has small gaps.

ADDENDUM: Here is a version that does not leave gaps. Yet it does not look too good when the curvature is large.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations} %  decorations.text just 4 fun
\newcounter{ropept}
\pgfkeys{/tikz/.cd,
    rope width/.store in=\RopeWidth,
    rope width=5pt,
    rope step/.store in=\RopeStep,
    rope step=2mm,
}

\pgfdeclaredecoration{rope}{initial}
{% 
\state{initial}[width=\RopeStep,next state=cont] {
    \pgfmoveto{\pgfpoint{0pt}{-\RopeWidth/2}}
    \pgfpathcurveto
    {\pgfpoint{5*\RopeStep/6}{0.25*\RopeWidth}}
    {\pgfpoint{7*\RopeStep/6}{0.45*\RopeWidth}}
    {\pgfpoint{1.5*\RopeStep}{\RopeWidth/2}}
     \pgfpathcurveto
    {\pgfpoint{10*\RopeStep/6}{0.55*\RopeWidth}}
    {\pgfpoint{11*\RopeStep/6}{0.6*\RopeWidth}}
    {\pgfpoint{13.5*\RopeStep/6}{\RopeWidth/2}}
    \setcounter{ropept}{0}
    \pgfcoordinate{lastup-\theropept}{\pgfpoint{-1.5*\RopeStep/6}{-\RopeWidth/2}}
    \pgfcoordinate{rope-auxA-\theropept}{\pgfpoint{13.5*\RopeStep/6}{\RopeWidth/2}}
  }
  \state{cont}[width=\RopeStep]{ 
     \pgfmoveto{\pgfpointanchor{lastup-\theropept}{center}}
     \pgfpathcurveto
    {\pgfpoint{-5*\RopeStep/6}{-0.6*\RopeWidth}}
    {\pgfpoint{-4*\RopeStep/6}{-0.55*\RopeWidth}}
    {\pgfpoint{-3*\RopeStep/6}{-0.55*\RopeWidth}}
     \pgfpathcurveto
    {\pgfpoint{-\RopeStep/6}{-0.45*\RopeWidth}}
    {\pgfpoint{\RopeStep/6}{-0.25*\RopeWidth}}
    {\pgfpoint{3*\RopeStep/6}{0pt}}
    \pgfpathcurveto
    {\pgfpoint{5*\RopeStep/6}{0.25*\RopeWidth}}
    {\pgfpoint{7*\RopeStep/6}{0.45*\RopeWidth}}
    {\pgfpoint{9*\RopeStep/6}{\RopeWidth/2}}
     \pgfpathcurveto
    {\pgfpoint{10*\RopeStep/6}{0.55*\RopeWidth}}
    {\pgfpoint{11*\RopeStep/6}{0.6*\RopeWidth}}
    {\pgfpoint{13.5*\RopeStep/6}{\RopeWidth/2}}
    \pgfmoveto{\pgfpointanchor{rope-auxA-\theropept}{center}}
    \pgfpathlineto{\pgfpoint{9*\RopeStep/6}{\RopeWidth/2}}
    \stepcounter{ropept}
    \pgfcoordinate{lastup-\theropept}{\pgfpoint{-1.5*\RopeStep/6}{-\RopeWidth/2}}
    \pgfcoordinate{rope-auxA-\theropept}{\pgfpoint{13.5*\RopeStep/6}{\RopeWidth/2}}
  }
  \state{final}[width=5pt]
  {
     \pgfmoveto{\pgfpointanchor{lastup-\theropept}{center}}
     \pgfpathcurveto
    {\pgfpoint{-5*\RopeStep/6}{-0.6*\RopeWidth}}
    {\pgfpoint{-4*\RopeStep/6}{-0.55*\RopeWidth}}
    {\pgfpoint{-0.5*\RopeStep}{-0.55*\RopeWidth}}
     \pgfpathcurveto
    {\pgfpoint{-\RopeStep/6}{-0.45*\RopeWidth}}
    {\pgfpoint{\RopeStep/6}{-0.25*\RopeWidth}}
    {\pgfpoint{0.5*\RopeStep}{0pt}}
    \pgfmoveto{\pgfpointdecoratedpathlast}
    \xdef\LastRope{\theropept}
  }
}
\begin{document}
\begin{tikzpicture}[decoration=rope]
  \draw[decorate] plot[smooth cycle, fill=yellow, thick] coordinates{ (4.,8.4) (6.5,9.) (8.,9) (9.,8.1) (11.34,6.18) (11,4) (11.3,2.2) (10.2 7,0.7 ) (8. 4,0.14) (6.2,0.29) (4.40,0.51) (3.2,0.29) (1.5,0.34) } ; 
  \typeout{\theropept}
\end{tikzpicture}  
\end{document}

enter image description here

  • feel free to include a rope decoration into your answer to my arithmetic rope question if you like... – Bart Oct 12 '18 at 19:17
  • @Bart Yes, I'll do. Thanks for your patience. There are still things that I do not understand and within the next week there is no 2h window in which I have time to systematically study what's going on. – marmot Oct 12 '18 at 20:16
  • How can I add the [decoration=rope] to the \draw plot so that I get a free curve with this rope pattern? – Thumbolt Nov 28 '18 at 5:16
  • @Thumbolt What is a "free" curve? – marmot Nov 28 '18 at 5:23
  • Say, \draw plot[smooth cycle, fill=yellow, thick] coordinates{ (4.,8.4) (6.5,9.) (8.,9) (9.,8.1) (11.34,6.18) (11,4) (11.3,2.2) (10.2 7,0.7 ) (8. 4,0.14) (6.2,0.29) (4.40,0.51) (3.2,0.29) (1.5,0.34) } ; – Thumbolt Nov 28 '18 at 5:46

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.