You may view all the posts of the category "tube.asy"
|
|
| (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)));









11 février 2010 11 h 26 min
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.
12 février 2010 2 h 36 min
Assuming you have a file "data.txt" containing the triples in this
format:
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:
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);