Official Asymptote example – bars3

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 23 h 13 min

Figure 0007
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import three;
import palette; 
import graph3;

size(300);

currentprojection=perspective(-30,-30,30,up=Z);

surface s;

for(int i = 0; i < 10; ++i) {
  for(int j = 0; j < 10; ++j) {
    s.append(shift(i,j,0)*scale(1,1,i+j)*unitcube);
  }
}

s.colors(palette(s.map(zpart),Rainbow()));
draw(s,meshpen=black+thick(),nolight,render(merge=true));

xaxis3("$x$",Bounds,InTicks(endlabel=false,Label,2,2));
yaxis3(YZ()*"$y$",Bounds,InTicks(beginlabel=false,Label,2,2));
zaxis3(XZ()*"$z$",Bounds,InTicks);


Official Asymptote example – SierpinskiSponge

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 22 h 13 min

Figure 0201
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
size(200);
import palette;
import three;

currentprojection=orthographic(1,1,1);

triple[] M={
  (-1,-1,-1),(0,-1,-1),(1,-1,-1),(1,0,-1),
  (1,1,-1),(0,1,-1),(-1,1,-1),(-1,0,-1),
  (-1,-1,0),(1,-1,0),(1,1,0),(-1,1,0),
  (-1,-1,1),(0,-1,1),(1,-1,1),(1,0,1),(1,1,1),(0,1,1),(-1,1,1),(-1,0,1)
};

surface[] Squares={
  surface((1,-1,-1)--(1,1,-1)--(1,1,1)--(1,-1,1)--cycle),
  surface((-1,-1,-1)--(-1,1,-1)--(-1,1,1)--(-1,-1,1)--cycle),
  surface((1,1,-1)--(-1,1,-1)--(-1,1,1)--(1,1,1)--cycle),
  surface((1,-1,-1)--(-1,-1,-1)--(-1,-1,1)--(1,-1,1)--cycle),
  surface((1,-1,1)--(1,1,1)--(-1,1,1)--(-1,-1,1)--cycle),
  surface((1,-1,-1)--(1,1,-1)--(-1,1,-1)--(-1,-1,-1)--cycle),
};

int[][] SquaresPoints={
  {2,3,4,10,16,15,14,9},
  {0,7,6,11,18,19,12,8},
  {4,5,6,11,18,17,16,10},
  {2,1,0,8,12,13,14,9},
  {12,13,14,15,16,17,18,19},
  {0,1,2,3,4,5,6,7}
};

int[][] index={
  {0,2,4},{0,1},{1,2,4},{2,3},{1,3,4},{0,1},{0,3,4},{2,3},
  {4,5},{4,5},{4,5},{4,5},
  {0,2,5},{0,1},{1,2,5},{2,3},{1,3,5},{0,1},{0,3,5},{2,3}
};

int[] Sponge0=array(n=6,value=1);

int[] eraseFaces(int n, int[] Sponge0) {
  int[] temp=copy(Sponge0);
  for(int k : index[n]) {
    temp[k]=0;
  }
  return temp;
}

int[][] Sponge1=new int[20][];
for(int n=0; n < 20; ++n) {
  Sponge1[n]=eraseFaces(n,Sponge0);
}

int[][] eraseFaces(int n, int[][] Sponge1) {
  int[][] temp=copy(Sponge1);
  for(int k : index[n])
    for(int n1 : SquaresPoints[k])
      temp[n1][k]=0;
  return temp;
}

int[][][] Sponge2=new int[20][][];
for(int n=0; n < 20; ++n)
  Sponge2[n]=eraseFaces(n,Sponge1);

int[][][] eraseFaces(int n, int[][][] Sponge2) {
  int[][][] temp=copy(Sponge2);
  for(int k : index[n])
    for(int n2: SquaresPoints[k])
      for(int n1: SquaresPoints[k])
        temp[n2][n1][k]=0;
  return temp;
}

int[][][][] Sponge3=new int[20][][][];
for(int n=0; n < 20; ++n)
  Sponge3[n]=eraseFaces(n,Sponge2);

surface s3;
real u=2/3;
for(int n3=0; n3 < 20; ++n3) {
  surface s2;
  for(int n2=0; n2 < 20; ++n2) {
    surface s1;
    for(int n1=0; n1 < 20; ++n1) {
      for(int k=0; k < 6; ++k){
        transform3 T=scale3(u)*shift(M[n1])*scale3(0.5);
        if(Sponge3[n3][n2][n1][k] > 0) {
          s1.append(T*Squares[k]);
        }
      }
    }
    transform3 T=scale3(u)*shift(M[n2])*scale3(0.5);
    s2.append(T*s1);
  }
  transform3 T=scale3(u)*shift(M[n3])*scale3(0.5);
  s3.append(T*s2);
}
s3.colors(palette(s3.map(abs),Rainbow()));
draw(s3);


Official Asymptote example – SierpinskiGasket

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 21 h 13 min

Figure 0200
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
size(200);
import palette;
import three;
currentprojection=perspective(8,2,1);
    
triple[] M={(0,0,1),1/3*(sqrt(8),0,-1),
            1/3*((sqrt(8))*Cos(120),(sqrt(8))*Sin(120),-1),
            1/3*((sqrt(8))*Cos(240),(sqrt(8))*Sin(240),-1)};
    
int level=5;
    
surface s;
    
void recur(triple p, real u, int l) {
  if(l < level)
    for(triple V : M)
      recur(p+u*V,u/2,l+1);
  else
    for(triple V : M) {
      s.append(surface((p+u*(V+M[0]))--(p+u*(V+M[1]))--(p+u*(V+M[2]))--cycle));
      s.append(surface((p+u*(V+M[0]))--(p+u*(V+M[2]))--(p+u*(V+M[3]))--cycle));
      s.append(surface((p+u*(V+M[0]))--(p+u*(V+M[3]))--(p+u*(V+M[1]))--cycle));
      s.append(surface((p+u*(V+M[3]))--(p+u*(V+M[2]))--(p+u*(V+M[1]))--cycle));
    }
}
    
recur(O,0.5,1);
    
s.colors(palette(s.map(zpart),Rainbow()));
    
draw(s,render(merge=true));


Official Asymptote example – NURBSsphere

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 20 h 13 min

Figure 0150
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import three;

/* Reference:
@article{Qin97,
  title={{Representing quadric surfaces using NURBS surfaces}},
  author={Qin, K.},
  journal={Journal of Computer Science and Technology},
  volume={12},
  number={3},
  pages={210--216},
  year={1997},
  publisher={Springer}
}
*/

size(10cm);
currentprojection=perspective(5,4,2,autoadjust=false);

// udegree=2, vdegree=3, nu=3, nv=4;

real[] W={2/3,1/3,1};
real[] w={1,1/3,1/3,1};

// 10 distinct control points
triple[][] P={{(0,0,1),(-2,-2,1),(-2,-2,-1),(0,0,-1)},
              {(0,0,1),(2,-2,1),(2,-2,-1),(0,0,-1)},
              {(0,0,1),(2,2,1),(2,2,-1),(0,0,-1)},
              {(0,0,1),(-2,2,1),(-2,2,-1),(0,0,-1)}};

P.cyclic=true;

real[][] weights=new real[3][4];
for(int i=0; i < 3; ++i)
for(int j=0; j < 4; ++j)
  weights[i][j]=W[i]*w[j];

real[] uknot={0,0,1/3,1/2,1,1};
real[] vknot={0,0,0,0,1,1,1,1};

int N=1;

for(int k=0; k < N; ++k)
for(int i=0; i < 4; ++i)
  draw(shift(k*Z)*P[i:i+3],uknot,vknot,weights,blue);

// draw(unitsphere,red+opacity(0.1));


Official Asymptote example – NURBScurve

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 19 h 13 min

Figure 0149
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import three;

size(10cm);

currentprojection=perspective(50,80,50);

// Nonrational curve:
// udegree=3, nu=6;
real[] knot={0,0,0,0,0.4,0.6,1,1,1,1};

triple[] P={
  (-31.2061,12.001,6.45082),
  (-31.3952,14.7353,6.53707),
  (-31.5909,21.277,6.70051),
  (-31.4284,25.4933,6.76745),
  (-31.5413,30.3485,6.68777),
  (-31.4896,32.2839,6.58385)
  };

draw(P,knot,green);

// Rational Bezier curve:
// udegree=3, nu=4;
real[] knot={0,0,0,0,1,1,1,1};
path3 g=scale3(20)*(X{Y}..{-X}Y);
triple[] P={point(g,0),postcontrol(g,0),precontrol(g,1),point(g,1)};

// Optional weights:
real[] weights=array(P.length,1.0);
weights[2]=5;

draw(P,knot,weights,red);



Official Asymptote example – yingyang

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 6 h 57 min

Figure 0268
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
size(0,25cm);
guide center=(0,1){W}..tension 0.8..(0,0){(1,-.5)}..tension 0.8..{W}(0,-1); 

draw((0,1)..(-1,0)..(0,-1));
filldraw(center{E}..{N}(1,0)..{W}cycle);
unfill(circle((0,0.5),0.125));
fill(circle((0,-0.5),0.125));

Mots-clefs : , , , ,


Official Asymptote example – xxsq01y

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 5 h 57 min

Figure 0267
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import solids;
size(0,150);
currentprojection=perspective(0,0,10,up=Y);
pen color=green;
real alpha=240;

real f(real x) {return x^2;}
pair F(real x) {return (x,f(x));}
triple F3(real x) {return (x,f(x),0);}

path p=graph(F,0,1,n=10,operator ..)--cycle;
path3 p3=path3(p);

render render=render(compression=0,merge=true);

draw(surface(revolution(p3,Y,0,alpha)),color,render);

surface s=surface(p);
draw(s,color,render);
draw(rotate(alpha,Y)*s,color,render);

draw(p3,blue);

xaxis3(Label("$x$",1),Arrow3);
yaxis3(Label("$y$",1),ymax=1.25,dashed,Arrow3);

dot("$(1,1)$",(1,1,0),X);
arrow("$y=x^{2}$",F3(0.7),X,0.75cm,red); 
arrow("$y=x$",(0.8,0.8,0),Y,1cm,red); 

real r=0.4;
draw((r,f(r),0)--(r,r,0),red);
draw("$r$",(0,(f(r)+r)*0.5,0)--(r,(f(r)+r)*0.5,0),N,red,Arrows3,PenMargins3);
draw(arc(1.1Y,0.3,90,0,7.5,180),Arrow3);

Mots-clefs : , , , ,


Official Asymptote example – xxsq01x-1

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 4 h 57 min

Figure 0266
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph3;
import solids;
size(300);
currentprojection=perspective(0,2,10,up=Y);
currentlight=Viewport;

pen color=green;

real f(real x) {return x^2;}
pair F(real x) {return (x,f(x));}
triple F3(real x) {return (x,f(x),0);}

path p=graph(F,0,1,n=10,operator ..)--cycle;
path3 p3=path3(p);

revolution a=revolution(-X,p3,Y,0,180);
render render=render(merge=true);
draw(surface(a),color);
surface s=surface(p);
draw(s,color);
transform3 t=shift(-2X)*rotate(180,Y);
draw(t*s,color);
draw(p3);
draw(t*p3);

draw((-1,0,0)--(-1,1,0),dashed);
xaxis3(Label("$x$",1),Arrow3);
yaxis3(Label("$y$",1),Arrow3);
dot(Label("$(1,1)$"),(1,1,0));
dot(Label("$(-1,1)$"),(-1,1,0),W);
arrow("$y=x^{2}$",F3(0.7),X,1cm,red); 
arrow("$y=x$",(0.3,0.3,0),X,1.5cm,red); 
draw(circle((-1,1,0),2,Y),dashed);
draw((-1,1,0)--(1,1,0),dashed);
draw(shift(-X)*arc(0.02Y,0.3,90,0,0,0,CW),Arrow3);

Mots-clefs : , , , ,


Official Asymptote example – xxsq01

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 3 h 57 min

Figure 0265
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph3;
import solids;
size(0,150);
currentprojection=perspective(0,0,10,up=Y);

pen color=green;
real alpha=250;

real f(real x) {return x^2;}
pair F(real x) {return (x,f(x));}
triple F3(real x) {return (x,f(x),0);}

path p=graph(F,0,1,n=10,operator ..)--cycle;
path3 p3=path3(p);

revolution a=revolution(p3,X,-alpha,0);
render render=render(compression=0,merge=true);
draw(surface(a),color,render);
surface s=surface(p);
draw(s,color,render);
draw(rotate(-alpha,X)*s,color,render);

draw(p3,blue);

xaxis3(Label("$x$",1),xmax=1.25,dashed,Arrow3);
yaxis3(Label("$y$",1),Arrow3);
dot(Label("$(1,1)$"),(1,1,0),X+Y);
arrow("$y=x$",(0.7,0.7,0),Y,0.75cm,red);
arrow("$y=x^2$",F3(0.7),X,0.75cm,red);
draw(arc(1.1X,0.3,90,90,3,-90),Arrow3);

Mots-clefs : , , , ,


Official Asymptote example – xsin1x

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 2 h 57 min

Figure 0263
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
size(300,0);

real f(real x) {return (x != 0.0) ? x * sin(1.0 / x) : 0.0;}
pair F(real x) {return (x,f(x));}

xaxis("$x$",red);
yaxis(red);
draw(graph(f,-1.2/pi,1.2/pi,1000));
label("$x\sin\frac{1}{x}$",F(1.1/pi),NW);

picture pic;
size(pic,50,IgnoreAspect);
xaxis(pic,red);
yaxis(pic,red);
draw(pic,graph(pic,f,-0.1/pi,0.1/pi,1000));

add(new void(frame f, transform t) {
    frame G=shift(point(f,N+0.85W))*align(bbox(pic,blue),10SE);
    add(f,G);
    draw(f,t*box(min(pic,user=true),max(pic,user=true)),blue);
    draw(f,point(G,E)--t*point(pic,W),blue);
  });


Mots-clefs : , ,