3

I would like to know how to create serpentine curves in the 'tikzpicture' and 'axis' environments on the x and y axes.

I used the 'tikzpicture' and 'axis' environments to build the uncolored part of the figure below following user Torbjørn T.'s directions in this answer here. As you can see I added the design of four curves in the shape of a serpentine in red, green, orange and blue

enter image description here

I need the serpentine-shaped curves to satisfy the following requirements:

  1. That they are transparent and that I can control the level of transparency.
  2. That I can choose to place hollow circles or filled circles at the beginning and end of the ends of the serpentine curves.
  3. that I can place them both vertically and horizontally.
  4. that I can control their colors
  5. Control the length of the serpentine curve.
  6. if possible, control the width of the space occupied by the serpentine curve.

Thanks.

UPDATE after comment of user @Qrrbrbirlbel

The serpentine curve = similar to sin wave. Circles be centered at the start/end. The connection of the serpentine to the border should be at the border of the circle (on the left when it is the final circle and on the right when it is the initial circle) and at the height of the circle center.

2
  • 2
    What's a serpentine curve? Your sketch looks more like a square wave than a serpentine curve. Should the circles be centered at the start/end or should they touch those points? (Ref. Q11871) How is the connection between circles and line defined? Your sketch shows very different ways. Dec 12, 2022 at 1:29
  • @Qrrbrbirlbel serpentine curve = similar to sin wave. Circles be centered at the start/end. The connection of the serpentine to the border should be at the border of the circle (on the left when it is the final circle and on the right when it is the initial circle) and at the height of the circle center. Dec 12, 2022 at 1:40

3 Answers 3

4
+200

You can use the built-in coil decoration (requires the decorations.pathmorphing library. The resulting style can be applied to any path, even curves.

You can adjust pre length, post length and line thickness in the tikzset. You can also adjust the size of the circles by adjusting filled and open (but also change shorten to half the length). The other aspects can be adjusted using the syntax

\draw[serp=<amplitude>, <color>, segment length=<length>, opacity=<percent>, <tip>-<tip>]

where <tip> can be open or filled. Default amplitude is set to 1mm.

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing, arrows.meta}

\tikzset{serp/.style={thick, decorate, decoration={coil, aspect=0, pre length=1mm, post length=1mm, amplitude=#1}, shorten <=-1.5pt, shorten >=-1.5pt},
    serp/.default=1mm,
    filled/.tip={Circle[length=3pt]},
    open/.tip={Circle[open, length=3pt]}
}

\begin{document}

\begin{tikzpicture}
\draw(0,0)grid(3,2);
\draw[serp=1.5mm, blue, segment length=1mm, opacity=.3, open-filled](0,0)--(3,2);
\end{tikzpicture}

\end{document}
2
  • +1 @Sandy G I tested the code. But I noticed that the control for the circles at the ends ( for open or filled circles ) does not include a streamer with a filled start circle and open end circle ( or the other way around, an open start circle and filled finish circle ). The options for the extremities include only both filled circles or both open circles. Dec 10, 2022 at 21:20
  • 1
    @MathOverview: I edited my response.
    – Sandy G
    Dec 10, 2022 at 21:36
4

If I well understand I have a new, more complete solution (where You can choose the opacity, the space occupied, the colors, see comments added):

\documentclass[tikz,margin=20pt]{standalone}
\pgfmathsetmacro{\op}{.5} %<-- set the opacity of the picture
\pgfmathsetmacro{\h}{.3} % <-- set the space occupied by serpentine
\begin{document}
    \begin{tikzpicture}
        \draw[cyan!15,step=.5] (-6.7,-6.7) grid (6.7,6.7);
        \draw[-latex] (-6.7,0)--(7,0) node[right] () {$x$};
        \draw[-latex] (0,-6.7)--(0,7) node[right] () {$y$}; 
        % it is possible to change to colours (\c) of the 4 serpentines
        \foreach \ang/\c in {0/red,90/blue,180/violet,270/green}
            {
            \draw[\c,line width=2pt,rotate=\ang,opacity=\op] (1,0) circle(\h);
            \draw[\c,line width=2pt,opacity=\op] plot[domain=1:6.3,smooth,rotate=\ang] (\x,{\h*sin(6*\x r)});
            \fill[\c,line width=2pt,rotate=\ang,opacity=\op] (6.3,0) circle(\h);
            }
    \end{tikzpicture}
\end{document}

Output:

enter image description here

LAST VERSION:

\documentclass[tikz,margin=20pt]{standalone}
\pgfmathsetmacro{\op}{.3} %<-- set the opacity of the picture
\pgfmathsetmacro{\h}{.5} % <-- set the space occupied by serpentine
\begin{document}
    \begin{tikzpicture}
        \draw[cyan!15,step=.5] (-7.5,-7.5) grid (7.5,7.5);
        \draw[-latex,line width=.5pt] (-7.7,0)--(7.7,0) node[right] () {$x$};
        \draw[-latex,line width=.5pt] (0,-7.7)--(0,7.7) node[right] () {$y$}; 
        % it is possible to change to colours (\c) of the 4 serpentines
        \foreach \ang/\c in {0/red,90/blue,180/violet,270/green}
        {
            \draw[\c,line width=4pt,rotate=\ang,opacity=\op] (1,0) circle(\h) ({1+\h},0)--(2.05,0);
            \draw[\c,line width=4pt,opacity=\op] plot[domain=2:5.8,smooth,rotate=\ang] (\x,{\h*sin(15*pi*\x r)});
            \draw[\c,line width=4pt,rotate=\ang,opacity=\op];
            \filldraw[\c,line width=4pt,rotate=\ang,opacity=\op](5.75,0)--({6.3},0);
            \shade[ball color=\c,rotate=\ang,opacity=\op] ({6.3+\h},0) circle(\h) ;
        }
    \end{tikzpicture}
\end{document}

OUTPUT:

enter image description here

Note: My preference is to view the graph without grid and axis (for this comment the first 3 lines inside tikzpicture environment).

3
  • Nicely done, all I would add would be a control on [domain=2:5.8] to have it use either macros or variables start:end. Clap clap clap Raffaele
    – anis
    Dec 14, 2022 at 8:53
  • 1
    @anis In the preamble add the lines: \pgfmathsetmacro{\start}{2} %<-- set the start domain for serpentine \pgfmathsetmacro{\end}{5.8} % <-- set the end domain for serpentine Then change the line: \draw[\c,line width=4pt,opacity=\op] plot[domain=\start:end,smooth,rotate=\ang] (\x,{\h*sin(15*pi*\x r)}); Dec 14, 2022 at 9:27
  • In my last comment read: \end. Dec 14, 2022 at 15:10
3

Like this (it is not clear if You want point/circle at start/end points):

enter image description here

Code:

\documentclass[tikz,margin=20pt]{standalone}
\begin{document}
    \begin{tikzpicture}
        \draw[cyan!15,step=.5] (-6.7,-6.7) grid (6.7,6.7);
        \draw[-latex] (-6.7,0)--(6.7,0) node[right] () {$x$};
        \draw[-latex] (0,-6.7)--(0,6.7) node[right] () {$y$};
        \draw[red,line width=2pt] plot[domain=-6.5:-1,smooth] (\x,{.2*sin(6*\x r)});
        \draw[blue,line width=2pt] plot[domain=1:6.5,smooth] (\x,{.2*sin(6*\x r)});
        \draw[violet,line width=2pt] plot[domain=1:6.5,smooth] ({.2*sin(6*\x r)},\x);
        \draw[green,line width=2pt] plot[domain=-6.5:-1,smooth] ({.2*sin(6*\x r)},\x);
    \end{tikzpicture}

\end{document}
4
  • It is interesting to use the sine function. But this approach does not satisfy items 1 and 2 of the requirements to answer the question. Nov 28, 2022 at 19:48
  • 6
    I make the start of the work. Now I think you can continue yourself. Nov 28, 2022 at 20:30
  • 1
    +1 (Not sure why there was a -1). Dec 10, 2022 at 19:59
  • @Dr.ManuelKuehner Me too. Thanks. Dec 10, 2022 at 21:49

You must log in to answer this question.

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