I am new to Mathematica and have recently been exploring graphical animations. So I was experimenting with simple concepts in periodic motion and hence wondered how can I draw a simple spring? Is there an easier/better way to do this?
The solution I arrived at was a simple function to draw a spring between two $x$ and $y$ coordinates.
hspring[a0_, x10_, x20_] :=
Module[{a = a0, x1 = x10, x2 = x20, n = 100},
h = (x2 - x1)/n;
xvalues = Table[k, {k, x1, x2, h}];
yvalues = Table[a Sin[m Pi/2], {m, 0, n}];
Line[Transpose @ {xvalues, yvalues}]
];
vspring[a0_, y10_, y20_] :=
Module[{a = a0, y1 = y10, y2 = y20, n = 100},
h = (y2 - y1)/n;
yvalues = Table[k, {k, y1, y2, h}];
xvalues = Table[a Sin[m Pi/2], {m, 0, n}];
Line[Transpose @ {xvalues, yvalues}]
];
Manipulate[
Graphics[{hspring[0.2, 0, Abs[2 Sin[x]]], Red, PointSize[0.03],
Point[{Abs[2 Sin[x]], 0}]}, PlotRange -> {{0, 3}, {-1, 1}}],
{x, 0.01, 2 Pi, 0.1}]
Comments
Post a Comment