Asymptote Generalities – fig1870

Category: Asymptote,Examples 2D,GeneralitiesPh. Ivaldi @ 16 h 39 min

Figure 0185
(Compiled with Asymptote version 2.14svn-r5318)
    
size(15cm,0);

srand(rand());
path p1 = randompath(9);
path p2 = randompath(8);
real Minx=min(min(p1).x,min(p2).x);
real Maxx=max(max(p1).x,max(p2).x);
real Miny=min(min(p1).y,min(p2).y);

pair[] inter=intersectionpoints(p1,p2);
int nb=inter.length;
for (int i=0 ; i<nb; ++i)
  {
    dot(inter[i]);
    label("$" + (string) i +"$", inter[i],N);
  }

draw(p1,.8red);
draw(p2,.8green);
label("I found " + (string) nb + " points of intersection.",((Maxx+Minx)/2,Miny),2S);

Étiquettes : , , ,


Asymptote Generalities – fig1880

Category: Asymptote,Examples 2D,GeneralitiesPh. Ivaldi @ 17 h 39 min

Figure 0186
(Compiled with Asymptote version 2.14svn-r5318)
    
size(8cm,0);

pair[] self_intersection(path p, int n=100)
{
  pair[] rpair=new pair[];
  path tpath;
  real [] tpoint;
  real l=length(p);
  int i=1;
  for (real t1=0; t1<l ; t1+=l/n)
    {
      for (real t2=t1+2*l/n; t2<l; t2+=l/n)
        {
          tpoint=intersect(subpath(p,t1,t1+l/n),
                           subpath(p,t2,t2+l/n));
          if (tpoint.length == 2)
            {
              rpair[i]=point(subpath(p,t1,t1+l/n),tpoint[0]);
              ++i;
            }
        }
    }
  return rpair;
}

void dott(pair[] pt, pen p)
{
  for (int i=1 ; i<pt.length; ++i)
    {
      dot(pt[i], p);
    }
}

srand(rand());
path p = randompath(15);

pair[] inter=self_intersection(p);
dott(inter, .8red);
draw(p);


Étiquettes : , ,


Fractals with Asymptote – fig0060

Category: Asymptote,Examples 2D,FractalsPh. Ivaldi @ 2 h 53 min

Figure 0006
(Compiled with Asymptote version 1.87svn-r4652)
    
//From documentation of Asymptote
size(10cm);

// Draw Sierpinski triangle with top vertex A, side s, and depth q.
void Sierpinski(pair A, real s, int q,
                bool top=true, bool randcolor=false) {
  pair B=A-(1,sqrt(2))*s/2;
  pair C=B+s;
  if(top) draw(A--B--C--cycle);
  if (randcolor) {
    filldraw((A+B)/2--(B+C)/2--(A+C)/2--cycle,
             (.33*rand()/randMax*red+.33*rand()/randMax*green+.33*rand()/randMax*blue));
  } else draw((A+B)/2--(B+C)/2--(A+C)/2--cycle);
  if(q > 0) {
    Sierpinski(A,s/2,q-1,false,randcolor);
    Sierpinski((A+B)/2,s/2,q-1,false,randcolor);
    Sierpinski((A+C)/2,s/2,q-1,false,randcolor);
  }
}

Sierpinski((0,1), 1, 5, randcolor=true);

Étiquettes : , , ,


Random walk in the space – fig0010

Category: Asymptote,Examples 3D,Random Walk (3D)Ph. Ivaldi @ 11 h 46 min

Figure 0001
(Compiled with Asymptote version 1.84svn-r4619)
    
import three;
settings.render=0;

// The available directions of steps
triple[] dirs={X,-X,Y,-Y,Z,-Z};
dirs.cyclic=true;

// Return the nodes of the path
triple[] randWalk(real Srnd(), int n)
{
  triple[] randPath;
  triple camera=1e10*currentprojection.camera;
  triple pos=O, tpos;
  int R;
  for (int i=0; i < n; ++i) {
    R=round(Srnd());
    tpos=pos+dirs[R];
    randPath.push(tpos);
    pos=tpos;
  }
  return randPath;
}
triple[] randWalk(int Srnd(), int n)
{
  real R(){ return Srnd();}
  return randWalk(R,n);
}

void drawWalk(triple[] nodes, pen p=white)
{
  triple camera=currentprojection.camera;
  if(currentprojection.infinity)
    camera *= max(abs(minbound(nodes)),abs(maxbound(nodes)));
  real[][] depth;
  for(int i=0; i < nodes.length-1; ++i) {
    real d=abs(camera-0.5*(nodes[i]+nodes[i+1]));
    depth.push(new real[] {d,i});
  }
  depth=sort(depth);
  triple M=nodes[round(depth[0][1])];
  triple m=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]);
    draw(nodes[i]--nodes[i+1],abs(nodes[i]-m)/abs(M-m)*p);
  }
}


size(18cm);
currentprojection=orthographic((1,1,1));

drawWalk(randWalk(rand,50000),cyan);
shipout(bbox(3mm,Fill));

Étiquettes : , , ,


Random walk in the space – fig0020

Category: Asymptote,Examples 3D,Random Walk (3D)Ph. Ivaldi @ 12 h 46 min

Figure 0002
(Compiled with Asymptote version 1.84svn-r4619)
    
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));

Étiquettes : ,


Random walk in the space – fig0030

Category: Asymptote,Examples 3D,Random Walk (3D)Ph. Ivaldi @ 13 h 46 min

Figure 0003
(Compiled with Asymptote version 1.84svn-r4619)
    
import three;
import stats;
settings.render=0;

string[] dirs={"U","D","B","F","R","L"};
// U=up, D=down, B=backward, F=forward, R=right, L=left
dirs.cyclic=true;

// Comput the nodes of the path
triple[] randWalk(real Srnd(), int n, real angle=90)
{
  triple[] randPath;
  triple udir=Z, vdir=X, kdir=cross(udir,vdir);
  triple pos=O, tpos;
  void changedir(real angle, triple axe)
  {
    transform3 T=rotate(angle,axe);
    udir=T*udir;
    vdir=T*vdir;
    kdir=T*kdir;
  }
  void nextdir()
  {
    string R=dirs[round(Srnd())];
    if(R == "R") changedir(-angle,kdir);
    else if(R == "L") changedir(angle,kdir);
    else if(R == "U") changedir(angle,vdir);
    else if(R == "D") changedir(-angle,vdir);
    else if(R == "B") changedir(180,udir);
  }
  for (int i=0; i < n; ++i) {
    tpos=pos+udir;
    randPath.push(tpos);
    pos=tpos;
    nextdir();
  }
  return randPath;
}

triple[] randWalk(int Srnd(), int n, real angle=90)
{
  real R(){ return Srnd();}
  return randWalk(R,n,angle);
}

void drawWalk(triple[] nodes, pen p=white)
{
  triple camera=currentprojection.camera;
  if(currentprojection.infinity)
    camera *= max(abs(minbound(nodes)),abs(maxbound(nodes)));
  real[][] depth;
  for(int i=0; i < nodes.length-1; ++i) {
    real d=abs(camera-0.5*(nodes[i]+nodes[i+1]));
    depth.push(new real[] {d,i});
  }
  depth=sort(depth);
  triple M=nodes[round(depth[0][1])];
  triple m=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]);
    draw(nodes[i]--nodes[i+1],abs(nodes[i]-M)/abs(M-m)*p);
  }
}


size(18cm);
currentprojection=orthographic((0.5,0.5,1));

drawWalk(randWalk(Gaussrand,50000));

Étiquettes :


Random walk in the space – fig0040

Category: Asymptote,Examples 3D,Random Walk (3D)Ph. Ivaldi @ 14 h 46 min

Figure 0004
(Compiled with Asymptote version 1.84svn-r4619)
    
import three;
import stats;
settings.render=0;

struct walk
{
  triple[] nodes;
  pen[] p;
}

string[] dirs={"U","D","B","F","R","L"};
// U=up, D=down, B=backward, F=forward, R=right, L=left
dirs.cyclic=true;

// Comput the nodes of the path
walk randWalk(real Srnd(), int n, real angle=90, pen[] p={currentpen})
{
  p.cyclic=true;
  walk ow;
  triple udir=Z, vdir=X, kdir=cross(udir,vdir);
  triple pos=O, tpos;
  void changedir(real angle, triple axe)
  {
    transform3 T=rotate(angle,axe);
    udir=T*udir;
    vdir=T*vdir;
    kdir=T*kdir;
  }
  void nextdir()
  {
    int rd=round(Srnd());
    ow.p.push(p[rd]);
    string R=dirs[rd];
    if(R == "R") changedir(-angle,kdir);
    else if(R == "L") changedir(angle,kdir);
    else if(R == "U") changedir(angle,vdir);
    else if(R == "D") changedir(-angle,vdir);
    else if(R == "B") changedir(180,udir);
  }
  for (int i=0; i < n; ++i) {
    tpos=pos+udir;
    ow.nodes.push(tpos);
    pos=tpos;
    nextdir();
  }
  return ow;
}

walk randWalk(int Srnd(), int n, real angle=90, pen[] p={currentpen})
{
  real R(){ return Srnd();}
  return randWalk(R,n,angle,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]);
    draw(walk.nodes[i]--walk.nodes[i+1],
         abs(walk.nodes[i]-m)/abs(M-m)*walk.p[i]);
  }
}


size(18cm);
currentprojection=orthographic((0.5,0.5,1));

drawWalk(randWalk(Gaussrand,50000,new pen[] {red,yellow,blue}));
shipout(bbox(3mm,Fill));

Étiquettes :


Random walk in the space – fig0050

Category: Asymptote,Examples 3D,Random Walk (3D)Ph. Ivaldi @ 15 h 46 min

Figure 0005
(Compiled with Asymptote version 1.84svn-r4619)
    
import three;
import stats;
settings.render=0;

struct walk
{
  triple[] nodes;
  pen[] p;
}

string[] dirs={"U","D","B","F","R","L"};
// U=up, D=down, B=backward, F=forward, R=right, L=left
dirs.cyclic=true;

// Comput the nodes of the path
walk randWalk(real Srnd(), int n, real angle=90, pen[] p={currentpen})
{
  p.cyclic=true;
  walk ow;
  triple udir=Z, vdir=X, kdir=cross(udir,vdir);
  triple pos=O, tpos;
  void changedir(real angle, triple axe)
  {
    transform3 T=rotate(angle,axe);
    udir=T*udir;
    vdir=T*vdir;
    kdir=T*kdir;
  }
  void nextdir()
  {
    int rd=round(Srnd());
    ow.p.push(p[rd]);
    string R=dirs[rd];
    if(R == "R") changedir(-angle,kdir);
    else if(R == "L") changedir(angle,kdir);
    else if(R == "U") changedir(angle,vdir);
    else if(R == "D") changedir(-angle,vdir);
    else if(R == "B") changedir(180,udir);
  }
  for (int i=0; i < n; ++i) {
    tpos=pos+udir;
    ow.nodes.push(tpos);
    pos=tpos;
    nextdir();
  }
  return ow;
}

walk randWalk(int Srnd(), int n, real angle=90, pen[] p={currentpen})
{
  real R(){ return Srnd();}
  return randWalk(R,n,angle,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]);
    draw(walk.nodes[i]--walk.nodes[i+1],
         abs(walk.nodes[i]-m)/abs(M-m)*walk.p[i]);
  }
}


size(18cm);
currentprojection=orthographic((0.5,0.5,1));

drawWalk(randWalk(Gaussrand,1000,60,new pen[] {red,yellow,blue}));
shipout(bbox(3mm,Fill));

Étiquettes :


Random walk in the space – fig0060

Category: Asymptote,Examples 3D,Random Walk (3D)Ph. Ivaldi @ 16 h 46 min

Figure 0006
(Compiled with Asymptote version 1.84svn-r4619)
    
import three;
import stats;
settings.render=0;

struct walk
{
  triple[] nodes;
  pen[] p;
}

string[] dirs={"U","D","B","F","R","L"};
// U=up, D=down, B=backward, F=forward, R=right, L=left
dirs.cyclic=true;

// Comput the nodes of the path
walk randWalk(real Srnd(), int n, real angle=90, pen[] p={currentpen})
{
  p.cyclic=true;
  walk ow;
  triple udir=Z, vdir=X, kdir=cross(udir,vdir);
  triple pos=O, tpos;
  void changedir(real angle, triple axe)
  {
    transform3 T=rotate(angle,axe);
    udir=T*udir;
    vdir=T*vdir;
    kdir=T*kdir;
  }
  void nextdir()
  {
    int rd=round(Srnd());
    ow.p.push(p[rd]);
    string R=dirs[rd];
    if(R == "R") changedir(-angle,kdir);
    else if(R == "L") changedir(angle,kdir);
    else if(R == "U") changedir(angle,vdir);
    else if(R == "D") changedir(-angle,vdir);
    else if(R == "B") changedir(180,udir);
  }
  for (int i=0; i < n; ++i) {
    tpos=pos+udir;
    ow.nodes.push(tpos);
    pos=tpos;
    nextdir();
  }
  return ow;
}

walk randWalk(int Srnd(), int n, real angle=90, pen[] p={currentpen})
{
  real R(){ return Srnd();}
  return randWalk(R,n,angle,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]);
    draw(walk.nodes[i]--walk.nodes[i+1],
         abs(walk.nodes[i]-m)/abs(M-m)*walk.p[i]);
  }
}


size(18cm);
currentprojection=orthographic((0.5,0.5,1));

drawWalk(randWalk(Gaussrand,10000,60,new pen[] {red,yellow,blue}));
shipout(bbox(3mm,Fill));

Étiquettes :


Random walk in the space – fig0070

Category: Asymptote,Examples 3D,Random Walk (3D)Ph. Ivaldi @ 17 h 46 min

Figure 0007
(Compiled with Asymptote version 1.84svn-r4619)
    
import three;
settings.render=0;

struct walk
{
  triple[] nodes;
  pen[] p;
}

string[] dirs={"U","D","B","F","R","L"};
// U=up, D=down, F=forward, R=right, L=left
dirs.cyclic=true;

// Comput the nodes of the path
walk randWalk(real Srnd(), int n, real angle=90, pen[] p={currentpen})
{
  p.cyclic=true;
  walk ow;
  triple udir=Z, vdir=X, kdir=cross(udir,vdir);
  triple pos=O, tpos;
  void changedir(real angle, triple axe)
  {
    transform3 T=rotate(angle,axe);
    udir=T*udir;
    vdir=T*vdir;
    kdir=T*kdir;
  }
  void nextdir()
  {
    int rd=round(Srnd());
    ow.p.push(p[rd]);
    string R=dirs[rd];
    if(R == "R") changedir(-angle,kdir);
    else if(R == "L") changedir(angle,kdir);
    else if(R == "U") changedir(angle,vdir);
    else if(R == "D") changedir(-angle,vdir);
    else if(R == "B") changedir(180,udir);
  }
  for (int i=0; i < n; ++i) {
    tpos=pos+udir;
    ow.nodes.push(tpos);
    pos=tpos;
    nextdir();
  }
  return ow;
}

walk randWalk(int Srnd(), int n, real angle=90, pen[] p={currentpen})
{
  real R(){ return Srnd();}
  return randWalk(R,n,angle,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]);
    draw(walk.nodes[i]--walk.nodes[i+1],
         abs(walk.nodes[i]-m)/abs(M-m)*walk.p[i]);
  }
}


size(18cm);
currentprojection=orthographic((0.5,0.5,1));

drawWalk(randWalk(rand,10000,60,new pen[] {red,yellow,blue}));
shipout(bbox(3mm,Fill));

Étiquettes :


Official Asymptote example – delu

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

Figure 0046
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
size(7cm,0);

pair z1=(1,-0.25);
pair v1=dir(45);
pair z2=-z1;
pair v2=0.75*dir(260);
pair z3=(z1.x,-3);

// A centered random number
real crand() {return unitrand()-0.5;}

guide g;
pair lastz;
for(int i=0; i < 60; ++i) {
  pair z=0.75*lastz+(crand(),crand());
  g=g..2.5*z;
  lastz=z;
}
g=shift(0,-.5)*g..cycle;

draw(g,gray(0.7));

draw("$r$",z1--z2,RightSide,red,Arrows,DotMargins);
draw(z1--z1+v1,Arrow);
draw(z2--z2+v2,Arrow);
draw(z3--z3+v1-v2,green,Arrow);

dot("1",z1,S,blue);
dot("2",z2,NW,blue);

Étiquettes : ,


Official Asymptote example – irregularcontour

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

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

size(200);

int n=100;

real f(real a, real b) {return a^2+b^2;}

srand(1);

real r() {return 1.1*(rand()/randMax*2-1);}

pair[] points=new pair[n];
real[] values=new real[n];

for(int i=0; i < n; ++i) {
  points[i]=(r(),r());
  values[i]=f(points[i].x,points[i].y);
}

draw(contour(points,values,new real[]{0.25,0.5,1},operator ..),blue);

Étiquettes : ,


Official Asymptote example – randompath3

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

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

size(300);
draw(randompath3(100),red,currentlight);

Étiquettes : ,