1

I want to draw a conical spring using latex in the 2D space.

I used the following command to draw a cyndrical spring between two nodes a and b:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{decorations.pathmorphing,patterns}
\begin{document}
\begin{tikzpicture}
\node[circle,fill=blue,inner sep=2.5mm] (a) at (0,1.5) {};
\draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=3mm,zigzag},decorate] (0,4) -- (a);
\draw[decoration={aspect=0.3, segment length=2mm, amplitude=3mm,zigzag},decorate] (0,-4) -- (a);
\draw[decoration={aspect=0.3, segment length=1.8mm, amplitude=3mm,zigzag},decorate] (4,0) -- (a);
\draw[decoration={aspect=0.3, segment length=1.8mm, amplitude=3mm,zigzag},decorate] (-4,0) -- (a);
\fill [pattern = north east lines] (-1,4) rectangle (1,4.3);
\draw[thick] (-1,4) -- (1,4);
\end{tikzpicture}
\end{document}

but I could not use the zigzag options to draw a conical spring with various diameters.

Can anybody help me?

Thanks in advance.

  • Maybe https://tex.stackexchange.com/questions/133183/draw-spiral-cone-tikz can help you or ctan.math.utah.edu/ctan/tex-archive/graphics/pstricks/contrib/.../pst-solides3d-doc.pdf page 81 – albert Feb 08 '19 at 16:56
  • Welcome to TeX.SE! Please provide us with a complete example that starts with \documentclass, ends with \end{document}, can be compiled and shows what you've tried. And please explain better a bit better what you're after. –  Feb 08 '19 at 16:56
  • Welcome! Perhaps good to know: you can format a block of code by indenting it by four spaces (like I've just done). This can be accomplished by either pressing the {}-button at the top or by pressing ctrl-K (or cmd-K in MacOS) while the text is selected, as demonstrated here. I was going to add something, about minimal working examples, but apparently marmot was faster. – Circumscribe Feb 08 '19 at 16:58
  • Thanks @albert, i want a simple 2D spring. – Oussama Braydi Feb 08 '19 at 17:28

1 Answers1

2

This is a quickly written code, not a decoration (in the usual sense), so this works only for straight lines. If allows you to draw zigzags of varying amplitude. In this implementation, you can specify the number of zigs, not the segment length.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,decorations.pathreplacing}
\tikzset{
varying zigzag/.style={
decorate,decoration={show path construction,
lineto code={
\draw 
(\tikzinputsegmentfirst) 
foreach \XX in {1,...,\vzgigzagsteps}
{ -- 
($($(\tikzinputsegmentfirst)!{(2*\XX-1)/(2*\vzgigzagsteps+2)}!(\tikzinputsegmentlast)$)!
{(\pgfkeysvalueof{/tikz/vzigzag/left diameter}+(\pgfkeysvalueof{/tikz/vzigzag/right
diameter}-\pgfkeysvalueof{/tikz/vzigzag/left
diameter})*(2*\XX-1)/(2*\vzgigzagsteps))*0.5cm}!-90:(\tikzinputsegmentlast) $)
--
($($(\tikzinputsegmentfirst)!{(2*\XX)/(2*\vzgigzagsteps+2)}!(\tikzinputsegmentlast)$)!
{(\pgfkeysvalueof{/tikz/vzigzag/left diameter}+(\pgfkeysvalueof{/tikz/vzigzag/right
diameter}-\pgfkeysvalueof{/tikz/vzigzag/left
diameter})*(2*\XX)/(2*\vzgigzagsteps))*0.5cm}!90:(\tikzinputsegmentlast) $)
} -- (\tikzinputsegmentlast);
}}},
vzigzag/.cd,
left diameter/.initial=1,
right diameter/.initial=0,
steps/.store in=\vzgigzagsteps,
steps=10}
\begin{document}
\begin{tikzpicture}
 \node[circle,fill=blue,inner sep=2.5mm] (a) at (0,1.5) {};
 \draw[varying zigzag] (0,4) -- (a);
 \draw[varying zigzag] (0,-4) -- (a);
 \draw[varying zigzag] (4,0) -- (a);
 \draw[varying zigzag] (-4,0) -- (a);
\begin{scope}[,xshift=9cm,vzigzag/left diameter=0,vzigzag/right diameter=1.5]
 \node[circle,fill=blue,inner sep=2.5mm] (a) at (0,1.5) {};
 \draw[varying zigzag] (0,4) -- (a);
 \draw[varying zigzag] (0,-4) -- (a);
 \draw[varying zigzag] (4,0) -- (a);
 \draw[varying zigzag] (-4,0) -- (a);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here