3

I wish to know how to draw a spring along an arc using Asymptote
Using import spring and drawspring(length,label) can only draw a spring along a straight line.
In TikZ this drawing an spring along an arc is achieved by using coil decorate and drawing the arc. Thanks in Advance

0

1 Answer 1

5

The coil function below seems to do the trick.

abstraction of a coiled spring

settings.outformat = "pdf";
import graph;
size(5cm);

guide coil(path g, real width=0.1, real margin = 2*width) {
  real L = arclength(g);
  real r = width / 2;

  pair startpoint = arcpoint(g, margin);
  real[][] isectiontimes = intersections(g, circle(c=startpoint,r=r));
  real initialcirclecentertime = (isectiontimes.length == 1 ?
                      isectiontimes[0][0] : isectiontimes[1][0]);
  pair startdir = dir(startpoint - point(g,initialcirclecentertime));
  real startangle = atan2(startdir.y, startdir.x);
  real startarctime = arclength(subpath(g, 0, initialcirclecentertime));
  write(startarctime);

  pair endpoint = arcpoint(g, L - margin);
  real finalcirclecentertime = intersections(g, circle(c=endpoint,r=r))[0][0];
  pair enddir = dir(endpoint - point(g,finalcirclecentertime));
  real endangle = atan2(enddir.y, enddir.x);
  real endarctime = arclength(subpath(g, 0, finalcirclecentertime));
  write(endarctime);

  real coillength = 2r;
  real lengthalongcoils = L - 2*margin;
  int numcoils = ceil(lengthalongcoils / coillength);
  real anglesubtended = 2pi * numcoils - startangle + endangle;

  real angleat(real arctime) {
    return (arctime - startarctime) * (anglesubtended / (endarctime - startarctime)) + startangle;
  }

  pair f(real t) {
    return arcpoint(g,t) + r * expi(angleat(t));
  }
 return subpath(g, 0, arctime(g, margin)) & graph(f, startarctime, endarctime, n=max(length(g), 10*numcoils+2), operator..) & subpath(g, arctime(g, L-margin), length(g));
}

draw(coil(unitcircle));

You must log in to answer this question.

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