A spring decoration

View: New views
4 Messages — Rating Filter:   Alert me  

A spring decoration

by Christophe Jorssen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I've done a spring decoration for my mechanics lectures. But I have a
problem when the spring it too "pulled" compared to the "rest length".
To see the problem, remove the comment in the tikzpicture.

By the way, any other comments on the code are welcome.

Thanks in advance.

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings}
% Mechanics
% Adapted from zigzag decoration
% (from pgflibrarydecorations.pathmorphing.code.tex)
\def\pgfdecorationspringstraightlinelength{.5cm}
\def\pgfdecorationspringnumberofzigzag{8}
\def\pgfdecorationspringrestlength{5cm}
%
\pgfkeys{%
   /pgf/decoration/.cd,
   spring straight line length/.code={%
     \pgfmathsetlengthmacro\pgfdecorationspringstraightlinelength{#1}},
   spring rest length/.code={%
     \pgfmathsetlengthmacro\pgfdecorationspringrestlength{#1}},
   spring number of zigzag/.store in=\pgfdecorationspringnumberofzigzag
}
%
\pgfdeclaredecoration{spring}{initial computations}{%
   \state{initial computations}[width=0pt,next state=straight line]{%
     % Compute the effective length of the spring (without the length
     % of the two straight lines): \pgfdecorationspringeffectivelength
     \pgfmathsetlengthmacro{\pgfdecorationspringeffectivelength}%
       {\pgfdecoratedpathlength-2*\pgfdecorationspringstraightlinelength}
     \xdef\pgfdecorationspringeffectivelength{%
       \pgfdecorationspringeffectivelength}%
     % Compute the effective length of one zigzag pattern:
     % \pgfdecorationspringeffectivelengthofonezigzag
     \pgfmathsetlengthmacro{\pgfdecorationspringeffectivelengthofonezigzag}%
 
{\pgfdecorationspringeffectivelength/\pgfdecorationspringnumberofzigzag}
     \xdef\pgfdecorationspringeffectivelengthofonezigzag{%
       \pgfdecorationspringeffectivelengthofonezigzag}%
     % Compute the effective length of one forth of zigzag pattern:
     % \pgfdecorationspringeffectivelengthofoneforthofzigzag
     \pgfmathsetlengthmacro{%
       \pgfdecorationspringeffectivelengthofoneforthofzigzag}%
       {\pgfdecorationspringeffectivelengthofonezigzag/4}
     \xdef\pgfdecorationspringeffectivelengthofoneforthofzigzag{%
       \pgfdecorationspringeffectivelengthofoneforthofzigzag}%
     % Compute the length of a forth of one zigzag (fixed by spring
     % constants)
     % 4=1/4
     % 0.707=cos(45°)
     \pgfmathsetlengthmacro{\pgfdecorationspringlengthofaforthzigzag}%
       {\pgfdecorationspringrestlength
         /(\pgfdecorationspringnumberofzigzag*4*0.707)}
     \xdef\pgfdecorationspringlengthofaforthzigzag{%
       \pgfdecorationspringlengthofaforthzigzag}%
     % Compute the (semi) heigth
     \pgfmathsetlengthmacro{\pgfdecorationspringeffectivesemiheigth}%
       {sqrt(\pgfdecorationspringlengthofaforthzigzag^2-
         \pgfdecorationspringeffectivelengthofoneforthofzigzag^2)}%
     \xdef\pgfdecorationspringeffectivesemiheigth{%
       \pgfdecorationspringeffectivesemiheigth}%
   }
   \state{straight line}%
     [width=\pgfdecorationspringstraightlinelength,next state=draw spring]{%
       \pgfpathlineto{%
         \pgfqpoint{%
           \pgfdecorationspringstraightlinelength}{0pt}}
   }
   \state{draw spring}%
     [width=\pgfdecorationspringeffectivelengthofonezigzag,
      repeat state=\pgfdecorationspringnumberofzigzag-1,next state=final]{%
       \pgfpathlineto{%
         \pgfqpoint{%
           \pgfdecorationspringeffectivelengthofoneforthofzigzag}{%
           \pgfdecorationspringeffectivesemiheigth}}
        \pgfpathlineto{%
         \pgfpoint{%
           3*\pgfdecorationspringeffectivelengthofoneforthofzigzag}{%
           -\pgfdecorationspringeffectivesemiheigth}}
        \pgfpathlineto{%
         \pgfpoint{%
           4*\pgfdecorationspringeffectivelengthofoneforthofzigzag}{0pt}}
   }
   \state{final}{%
     \pgfpathlineto{\pgfpointdecoratedpathlast}
   }
}
\tikzset{%
   Spring/.style = {%
     decoration = {%
       spring,
       spring straight line length = .5cm,
       spring rest length = #1,
       spring number of zigzag = 5},
     decorate,
     very thick},
   Spring/.default = {4cm}}

\begin{document}

\begin{tikzpicture}
   \draw[Spring] (0,0) -- (5,0);
   \draw[Spring,red] (0,0) -- (3,0);
   \draw[Spring,green] (0,0) -- (6,0);
% \draw[Spring,blue] (0,0) -- (10,0);
\end{tikzpicture}

\end{document}

--
Christophe

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Parent Message unknown Re: A spring decoration

by Christophe Jorssen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Nick Papior Andersen a écrit :
> Hi Christophe
>
> If you add an abs() under the sqrt you won't get the error and it works out
> perfectly.

Ok thanks a lot.

--
Christophe

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Re: A spring decoration

by Mark Wibrow-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

You can also get rid of all those nasty \xdef's if you use the
'persistent precomputation' key as an option to the 'initial
computations' state. The code you give to this key is executed outset
the TeX-group that the state code is executed within.

Regards

Mark

2009/10/15 Christophe Jorssen <jorssen.leraincy@...>:

> Nick Papior Andersen a écrit :
>> Hi Christophe
>>
>> If you add an abs() under the sqrt you won't get the error and it works out
>> perfectly.
>
> Ok thanks a lot.
>
> --
> Christophe
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> pgf-users mailing list
> pgf-users@...
> https://lists.sourceforge.net/lists/listinfo/pgf-users
>

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Re: A spring decoration

by Christophe Jorssen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Mark Wibrow a écrit :
> Hi,
>
> You can also get rid of all those nasty \xdef's if you use the
> 'persistent precomputation' key as an option to the 'initial
> computations' state. The code you give to this key is executed outset
> the TeX-group that the state code is executed within.
>

Ok. Thanks for the tip. I'll have a look at it this week-end and post
the altered version.

--
Christophe

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users