3

I'd like to draw multi-segment "wavy" paths, so I tried both the standard snake decoration, and the very promising complete sines one as well, but in both cases, the segment endpoints are not kept.

Here's an illustration of what I mean. The first rectangle is drawn from single-segment paths, and look great (especially the complete sines one at the bottom). However, when drawing two segments, or a whole rectangle, in one go, the wavy segments don't end at the intended segment endpoints.

\documentclass[margin=3mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\input{sine.tex} %% From https://tex.stackexchange.com/a/60757/2113

\begin{document}

\begin{tikzpicture}
  \tikzset
      { wavy/.style={red, decorate, sharp corners, decoration={snake, amplitude=0.4mm}}
      , siny/.style={blue, decorate, sharp corners, decoration={complete sines, amplitude=1mm}}
      }

\newcommand{\test}[1]{
  \begin{scope}[shift={(0,0)}]
    \draw (0,0) -- ++ (1,0);
    \draw[#1] (0,0) -- ++ (1,0);
    
    \draw (1,0) -- ++ (0,1);
    \draw[#1] (1,0) -- ++ (0,1);
    
    \draw (1,1) -- ++ (-1,0);
    \draw[#1] (1,1) -- ++(-1,0);
    
    \draw (0,1) -- (0,0);
    \draw[#1] (0,1) -- (0,0);
  \end{scope}
  
  \begin{scope}[shift={(2,0)}]
    \draw (0,0) -- ++ (1,0) -- ++ (0,1);
    \draw[#1] (0,0) -- ++ (1,0) -- ++ (0,1);
  \end{scope}
  
  \begin{scope}[shift={(4,0)}]
    \draw (0,0) rectangle ++ (1,1);
    \draw[#1] (0,0) rectangle ++ (1,1);
  \end{scope}
}

\begin{scope}[shift={(0,0)}]
  \test{wavy}
\end{scope}

\begin{scope}[shift={(0,-2)}]
  \test{siny}
\end{scope}

\end{tikzpicture}

\end{document}

enter image description here

Is there a way to get results matching the first column? I'd like to clip by these paths so I think I need to draw them as one multi-segment path.

1 Answer 1

3

At least for snake, it seems to work if I break down the segments into their own decorate{} block:

\documentclass[margin=3mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\newcommand{\segmentrect}[3]{
  decorate{ #1 -- ++ (#2,0) }
  decorate{ -- ++ (0,#3) }
  decorate{ -- ++ ({-(#2)},0) }
  decorate{ -- cycle }
}
  
\begin{document}

\begin{tikzpicture}
\draw (0,0) rectangle ++ (1,1);
\draw[decoration={snake, amplitude=0.4mm,segment length=2mm},red]
  \segmentrect{(0,0)}{1}{1};
\end{tikzpicture}
\end{document}

enter image description here

Unfortunately, the same approach for complete sines results in complete disaster:

\documentclass[margin=3mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\input{sine.tex} %% From https://tex.stackexchange.com/a/60757/2113

\newcommand{\segmentrect}[3]{
  decorate{ #1 -- ++ (#2,0) }
  decorate{ -- ++ (0,#3) }
  decorate{ -- ++ ({-(#2)},0) }
  decorate{ -- cycle }
}
  
\begin{document}

\begin{tikzpicture}
\draw (0,0) rectangle ++ (1,1);
\draw[decoration={complete sines},blue]
  \segmentrect{(0,0)}{1}{1};
\end{tikzpicture}
\end{document}

enter image description here

4
  • Good answer. The snake only ends in a wave node at the ends of the decoration, so to enforce nodes at the corners, you simply decorate each line separately. Always post complete compilable code. Jul 10, 2021 at 8:27
  • @hpekristiansen you meanI should replicate everything outside the begin{tikzpicture}?
    – Cactus
    Jul 10, 2021 at 8:28
  • You should(like OP) post code that starts with \documentclass... and ends with \end{document}. That way OP and others can copy+paste your code directly and see/modify you findings. Jul 10, 2021 at 8:31
  • @hpekristiansen but doctor, I am Pagliacci^WOP
    – Cactus
    Jul 10, 2021 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .