PIPRIME.FR BLOG

Live fully to honor life

Random walk with the Asymptote drawing software

Random walk in the space with the Asymptote drawing and programming software

đź”—randomwalk-fig001

Figure randomwalk 001 Generated with Asymptote

Show randomwalk/fig0010.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Random Walk (3D)
Tags : #Random | #Function (creating) | #Array | #Loop/for/while

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));

đź”—randomwalk-fig002

Figure randomwalk 002 Generated with Asymptote

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));

đź”—randomwalk-fig003

Figure randomwalk 003 Generated with Asymptote

Show randomwalk/fig0030.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Random Walk (3D)
Tags : #Random

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));

đź”—randomwalk-fig004

Figure randomwalk 004 Generated with Asymptote

Show randomwalk/fig0040.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Random Walk (3D)
Tags : #Random

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));

đź”—randomwalk-fig005

Figure randomwalk 005 Generated with Asymptote

Show randomwalk/fig0050.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Random Walk (3D)
Tags : #Random

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));

đź”—randomwalk-fig006

Figure randomwalk 006 Generated with Asymptote

Show randomwalk/fig0060.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Random Walk (3D)
Tags : #Random

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));

đź”—randomwalk-fig007

Figure randomwalk 007 Generated with Asymptote

Show randomwalk/fig0070.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Random Walk (3D)
Tags : #Random

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));

0%