🔗Asymptote Gallery Tagged by “Struct” #145
đź”—graph-fig022

Show graph/fig0230.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Examples 2D | Graph.asy
Tags : #Graph | #Function (drawing) | #Fraction | #Label | #Tex/latex features | #Struct
import graph; unitsize(x=1cm,y=2cm); struct rational { int p; int q; real ep=1/10^5; }; rational operator init() {return new rational;} rational rational(real x, real ep=1/10^5) { rational orat; int q=1; while (abs(round(q*x)-q*x)>ep) { ++q; } orat.p=round(q*x); orat.q=q; orat.ep=ep; return orat; } int pgcd(int a, int b) { int a_=abs(a), b_=abs(b), r=a_; if (b_>a_) {a_=b_; b_=r; r=a_;} while (r>0) { r=a_%b_; a_=b_; b_=r; } return a_; } string texfrac(int p, int q, string factor="", bool signin=false, bool factorin=true, bool displaystyle=false, bool zero=true) { if (p==0) return (zero ? "$0$" : ""); string disp= displaystyle ? "$\displaystyle " : "$"; int pgcd=pgcd(p,q); int num= round(p/pgcd), den= round(q/pgcd); string nums; if (num==1) if (factor=="" || (!factorin && (den !=1))) nums="1"; else nums=""; else if (num==-1) if (factor=="" || (!factorin && (den !=1))) nums="-1"; else nums="-"; else nums= (string) num; if (den==1) return "$" + nums + factor + "$"; else { string dens= (den==1) ? "" : (string) den; if (signin || num>0) if (factorin) return disp + "\frac{" + nums + factor + "}{" + (string) dens + "}$"; else return disp + "\frac{" + nums + "}{" + (string) dens + "}"+ factor + "$"; else { if (num==-1) if (factor=="" || !factorin) nums="1"; else nums=""; else nums=(string)(abs(num)); if (factorin) return disp + "-\frac{" + nums + factor + "}{" + (string) dens + "}$"; else return disp + "-\frac{" + nums + "}{" + (string) dens + "}"+ factor + "$"; } } } string texfrac(rational x, string factor="", bool signin=false, bool factorin=true, bool displaystyle=false, bool zero=true) { return texfrac(x.p, x.q, factor, signin, factorin, displaystyle, zero); } ticklabel labelfrac(real ep=1/10^5, real factor=1.0, string symbol="", bool signin=false, bool symbolin=true, bool displaystyle=false, bool zero=true) { return new string(real x) { return texfrac(rational(x/factor), symbol, signin, symbolin, displaystyle, zero); }; } ticklabel labelfrac=labelfrac(); xlimits( -2pi, 2pi); ylimits( -1, 1); yaxis("y",LeftRight , Ticks(labelfrac,Step=.5,step=.25, ptick=grey, extend=true)); xaxis("$\theta$",BottomTop, Ticks(labelfrac(factor=pi,symbol="\pi",symbolin=false), Step=pi/2, step=pi/4, ptick=grey, extend=true)); draw(graph(new real(real x){return sin(x);},-2pi,2pi)); draw(graph(new real(real x){return cos(x);},-2pi,2pi), .8red);
đź”—graph-fig023

Show graph/fig0240.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Examples 2D | Graph.asy
Tags : #Graph | #Function (drawing) | #Fraction | #Label | #Tex/latex features | #Struct
import graph; // public real xunit=1cm,yunit=1cm; void graphicrules(picture pic=currentpicture, string prefix=defaultfilename, real unit=1cm, real xunit=unit != 0 ? unit : 0, real yunit=unit != 0 ? unit : 0, real xmin, real xmax, real ymin, real ymax) { xlimits(xmin, xmax); ylimits(ymin, ymax); unitsize(x=xunit, y=yunit); } struct rational { int p; int q; real ep=1/10^5; }; rational operator init() {return new rational;} rational rational(real x, real ep=1/10^5) { rational orat; int q=1; while (abs(round(q*x)-q*x)>ep) { ++q; } orat.p=round(q*x); orat.q=q; orat.ep=ep; return orat; } int pgcd(int a, int b) { int a_=abs(a), b_=abs(b), r=a_; if (b_>a_) {a_=b_; b_=r; r=a_;} while (r>0) { r=a_%b_; a_=b_; b_=r; } return a_; } string texfrac(int p, int q, string factor="", bool signin=false, bool factorin=true, bool displaystyle=false, bool zero=true) { if (p==0) return (zero ? "$0$" : ""); string disp= displaystyle ? "$\displaystyle " : "$"; int pgcd=pgcd(p,q); int num= round(p/pgcd), den= round(q/pgcd); string nums; if (num==1) if (factor=="" || (!factorin && (den !=1))) nums="1"; else nums=""; else if (num==-1) if (factor=="" || (!factorin && (den !=1))) nums="-1"; else nums="-"; else nums= (string) num; if (den==1) return "$" + nums + factor + "$"; else { string dens= (den==1) ? "" : (string) den; if (signin || num>0) if (factorin) return disp + "\frac{" + nums + factor + "}{" + (string) dens + "}$"; else return disp + "\frac{" + nums + "}{" + (string) dens + "}"+ factor + "$"; else { if (num==-1) if (factor=="" || !factorin) nums="1"; else nums=""; else nums=(string)(abs(num)); if (factorin) return disp + "-\frac{" + nums + factor + "}{" + (string) dens + "}$"; else return disp + "-\frac{" + nums + "}{" + (string) dens + "}"+ factor + "$"; } } } string texfrac(rational x, string factor="", bool signin=false, bool factorin=true, bool displaystyle=false, bool zero=true) { return texfrac(x.p, x.q, factor, signin, factorin, displaystyle, zero); } ticklabel labelfrac(real ep=1/10^5, real factor=1.0, string symbol="", bool signin=false, bool symbolin=true, bool displaystyle=false, bool zero=true) { return new string(real x) { return texfrac(rational(x/factor), symbol, signin, symbolin, displaystyle, zero); }; } ticklabel labelfrac=labelfrac(); void grid(picture pic=currentpicture, real xmin=pic.userMin().x, real xmax=pic.userMax().x, real ymin=pic.userMin().y, real ymax=pic.userMax().y, real xStep=1, real xstep=.5, real yStep=1, real ystep=.5, pen pTick=nullpen, pen ptick=grey, bool above=true) { xaxis(pic, BottomTop, xmin, xmax, Ticks("%",extend=true,Step=xStep,step=xstep,pTick=pTick,ptick=ptick), above=above); yaxis(pic, LeftRight, ymin, ymax, Ticks("%",extend=true,Step=yStep,step=ystep,pTick=pTick,ptick=ptick), above=above); } void cartesianaxis(picture pic=currentpicture, Label Lx=Label("$x$",align=S), Label Ly=Label("$y$",align=W), real xmin=pic.userMin().x, real xmax=pic.userMax().x, real ymin=pic.userMin().y, real ymax=pic.userMax().y, real extrawidth=1, real extraheight=extrawidth, pen p=currentpen, ticks xticks=Ticks("%",pTick=nullpen, ptick=grey), ticks yticks=Ticks("%",pTick=nullpen, ptick=grey), bool above=true, arrowbar arrow=Arrow) { extraheight= cm*extraheight/(2*pic.yunitsize); extrawidth = cm*extrawidth/(2*pic.xunitsize); yequals(pic, Lx, 0, xmin-extrawidth, xmax+extrawidth, p, above, arrow=arrow); yequals(pic, 0, xmin, xmax, p, xticks, above); xequals(pic, Ly, 0, ymin-extraheight, ymax+extraheight, p, above, arrow=arrow); xequals(pic, 0, ymin, ymax, p, yticks, above); } void labeloij(picture pic=currentpicture, Label Lo=Label("$O$",NoFill), Label Li=Label("$\vec{\imath}$",NoFill), Label Lj=Label("$\vec{\jmath}$",NoFill), pair diro=SW, pair diri=S, pair dirj=W, pen p=scale(2)*currentpen, filltype filltype=NoFill, arrowbar arrow=Arrow(2mm)) { if (Lo.filltype==NoFill) Lo.filltype=filltype; if (Li.filltype==NoFill) Li.filltype=filltype; if (Lj.filltype==NoFill) Lj.filltype=filltype; labelx(pic, Lo, 0, diro, p); draw(pic, Li, (0,0)--(1,0), diri, p, arrow); draw(pic, Lj, (0,0)--(0,1), dirj, p, arrow); dot(pic, (0,0), dotsize(p)+p); } void labeloIJ(picture pic=currentpicture, Label Lo=Label("$O$",NoFill), Label LI=Label("$I$",NoFill), Label LJ=Label("$J$",NoFill), pair diro=SW, pair dirI=S, pair dirJ=W, pen p=currentpen, filltype filltype=NoFill, arrowbar arrow=Arrow) { if (Lo.filltype==NoFill) Lo.filltype=filltype; if (LI.filltype==NoFill) LI.filltype=filltype; if (LJ.filltype==NoFill) LJ.filltype=filltype; labelx(pic, LI, 1, dirI, p); labely(pic, LJ, 1, dirJ, p); labelx(pic, Lo, 0, diro, p); dot(pic, (0,0), dotsize(p)+p); } graphicrules(xunit=1cm, yunit=3cm, xmin=-2pi, xmax=2pi, ymin=-1, ymax=1); grid(xStep=pi/2, xstep=pi/4, yStep=.5, ystep=.25); cartesianaxis(xticks=Ticks(Label(UnFill),labelfrac(factor=pi,symbol="\pi",symbolin=true, zero=false),Step=pi/2, step=pi/4, ptick=grey), yticks=Ticks(Label(UnFill),labelfrac(zero=false),Step=.5,step=.25, ptick=grey), arrow=None); dot("$O$",(0,0),2SW);
đź”—randomwalk-fig002

Show randomwalk/fig0020.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Random Walk (3D)
Tags : #Random | #Struct
import three; settings.render=0; // The available directions of steps triple[] dirs={X,-X,Y,-Y,Z,-Z}; dirs.cyclic=true; struct walk { triple[] nodes; pen[] p; } // Comput the nodes of the path walk randWalk(real Srnd(), int n, pen[] p={currentpen}) { p.cyclic=true; walk ow; triple pos=O, tpos; for (int i=0; i < n; ++i) { int R=round(Srnd()); tpos=pos+dirs[R]; ow.nodes.push(tpos); ow.p.push(p[R]); pos=tpos; } return ow; } walk randWalk(int Srnd(), int n, pen[] p={currentpen}) { real R(){ return Srnd();} return randWalk(R,n,p); } void drawWalk(walk walk) { triple camera=currentprojection.camera; if(currentprojection.infinity) camera *= max(abs(minbound(walk.nodes)),abs(maxbound(walk.nodes))); real[][] depth; for(int i=0; i < walk.nodes.length-1; ++i) { real d=abs(camera-0.5*(walk.nodes[i]+walk.nodes[i+1])); depth.push(new real[] {d,i}); } depth=sort(depth); triple M=walk.nodes[round(depth[0][1])]; triple m=walk.nodes[round(depth[depth.length-1][1]+1)]; // Draw from farthest to nearest while(depth.length > 0) { real[] a=depth.pop(); int i=round(a[1]); // dot(walk.nodes[i],walk.p[i]); draw(walk.nodes[i]--walk.nodes[i+1],abs(walk.nodes[i]-m)/abs(M-m)*(walk.p[i]+walk.p[i+1])); } } size(18cm); currentprojection=orthographic((0.5,0.5,1)); drawWalk(randWalk(rand,50000,new pen[]{red, blue, green, yellow, purple})); shipout(bbox(3mm,Fill));
đź”—various-fig003

Show various/fig0300.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Miscellaneous
Tags : #Struct
// On définit ce qu'est l'objet 'rectangle' // On peut comparer ça à la définition d'une voiture: // elle a des propriètés: la couleur, le nombre de portes... // elle a aussi des actions (ou méthodes en info): ouvrir la porte, tourner le volant, avancer... struct rectangle { pair A,B,C,D; // Ce sont des propriétés (au sens informatique): ses sommets. path path(){return A--B--C--D--cycle;} // Ceci est une action du rectangle (renvoyer le chemin du rectangle) }; // Le constructeur de l'objet (une action qui renvoie l'objet). // Chaque fois que l'on va définir une variable 'X' de type 'rectangle' // c'est le retour de l'action rectangle.init qui est affecté à X. rectangle operator init() {return new rectangle;} // La fonction rectangle qui prend en argument le coin inférieur gauche // et le coin supérieur droit retourne un rectangle. // On notera comment l'on récupére une propriété ou une action d'un // objet grâce au '.' en écrivant objet.propriété ou // objet.action(les_paramètres). rectangle rectangle(pair gb, pair dh) { rectangle retour; retour.A=(dh.x,gb.y); retour.B=dh; retour.C=(gb.x,dh.y); retour.D=gb; return retour; } // On peut alors définir tous les opérateurs que l'on veut sur les // rectangles. // par exemple la multiplication d'un réal par un rectangle (x*rectangle): rectangle operator *(real x, rectangle rectangle) { rectangle retour; retour.A=rectangle.A+x*(rectangle.B-rectangle.A); retour.B=rectangle.B+x*(rectangle.C-rectangle.B); retour.C=rectangle.C+x*(rectangle.D-rectangle.C); retour.D=rectangle.D+x*(rectangle.A-rectangle.D); return retour; } // Ici, cette opération est commutative (rectangle*x=x*rectangle): rectangle operator *(rectangle rectangle,real x){return x*rectangle;} // On dit ici comment "Asymptote" doit convertir, si besoin est, un type 'rectangle' // en 'path' puis en 'path[]' (tableau de 'path'). path operator cast(rectangle rectangle){return rectangle.path();} path[] operator cast(rectangle rectangle){return rectangle.path();} //Par exemple, dans le code: /* rectangle SQ=rectangle((0,0), (1,1)); draw(SQ); */ // 'draw' attend un type 'path' donc 'SQ' sera automatiquement converti. // Maintenant on peut travailler avec cette structure size(10cm); rectangle SQ=rectangle((0,0), (1,1)); pen [] col= new pen[]{gray,yellow}; int nb=10; for (int i=0; i<nb; ++i) { filldraw(SQ,col[i%2]); SQ=.25*SQ; }