28 oct. 2007

Asymptote using tube.asy – fig0080

Figure 0008
(Compiled with Asymptote version 2.14svn-r5318)
    
import tube;
import graph3;
size(12cm,0);
currentprojection=perspective((-1,1,1));

int p=7, q=3;
real n=p/q;
real a=1, b=1;
real x(real t){return a*cos(t);}
real y(real t){return a*sin(t);}
real z(real t){return b*cos(n*t);}

real R(real t){
  real st2=(n*sin(n*t))^2;
  return a*(1+st2)^(1.5)/sqrt(1+st2+n^4*cos(n*t)^2);
  // return -a*(1+st2)^(1.5)/sqrt(1+st2+n^4*cos(n*t)^2); // Signed radius curvature
}

real mt=q*2*pi;
path3 p=graph(x,y,z,0,mt,operator ..)..cycle;

real m=R(0), M=R(0.5*pi/n);

// Define a pen depending to the radius curvature of graph(x,y,z) at reltime t
pen curvaturePen(real t){
  real r=abs(R(t*mt)-m)/(M-m);
  return interp(red,blue,r);
}

// Draw the tube, colors depend of the radius curvature R.
draw(tube(p,coloredpath(scale(0.1)*unitcircle, curvaturePen)));

  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Twitter
  • Yahoo! Bookmarks

Mots-clefs : , ,

2 Réponses à “Asymptote using tube.asy – fig0080”

Flux RSS de ces commentaires rss

  1. alfC a dit:

    How would you do the same but from a path read from a file. Basically, I don't know how to read a file (with triples) and make a path from it.

    Thank you for the blog it is amazing.

    • Admin
      Ph. Ivaldi a dit:

      Assuming you have a file "data.txt" containing the triples in this
      format:

      1 1 1
      2 3 1
      3 6 7
      0 1 -5
      

      you can convert it in an array of "triple" with this code:

      file in=input("data.txt").line();
      real[][] P=in.dimension(0,0);
      
      triple[] T;
      for(int i=0; i < P.length; ++i) {
        T.push((P[i][0],P[i][1],P[i][2]));
      }
      

      The array T of "triple" can be converted in "path3" with the operators
      .. or — like this:

      guide3 join(... guide3[]) = operator ..;
      path3 Pt=join(...T);
      

      A complete code may be:

      import tube;
      size(10cm);
      
      file in=input("data.txt").line();
      real[][] P=in.dimension(0,0);
      
      triple[] T;
      for(int i=0; i < P.length; ++i) {
        T.push((P[i][0],P[i][1],P[i][2]));
      }
      
      // Use join=.. or join=--
      guide3 join(... guide3[]) = operator ..;
      // Convert the array of triple in path3
      path3 Pt=join(...T);
      
      draw(tube(Pt,unitcircle),0.8*red);