Fractals with Asymptote – fig0010

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

Figure 0001
(Compiled with Asymptote version 1.87svn-r4652)
    
// From documentation of Asymptote
size(250);

real a=3;
real b=4;
real c=hypot(a,b);

transform ta=shift(c,c)*rotate(-aCos(a/c))*scale(a/c)*shift(-c);
transform tb=shift(0,c)*rotate(aCos(b/c))*scale(b/c);

picture Pythagorean(int n) {
  picture pic;
  fill(pic,scale(c)*unitsquare,1/(n+1)*green+n/(n+1)*brown);
  if(n == 0) return pic;
  picture branch=Pythagorean(--n);
  add(pic,ta*branch);
  add(pic,tb*branch);
  return pic;
}

add(Pythagorean(12));

Étiquettes : , , , ,


Fractals with Asymptote – fig0020

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

Figure 0002
(Compiled with Asymptote version 1.87svn-r4652)
    
size(10cm,0);

transform scale(pair center, real k) {
  return shift(center)*scale(k)*shift(-center);
}

path trk=(0,0)--(0,1);

void tree(path p, int n, real a=30, real b=40, real r=.75) {
  if (n!=0) {
    pair h=point(p,length(p));
    transform tb=rotate(180-b,h)*scale(h,r);
    transform ta=rotate(-180+a,h)*scale(h,r);
    draw(p,n/3+1/(n+1)*green+n/(n+1)*brown);
    tree(tb*reverse(p),n-1,a,b,r);
    tree(ta*reverse(p),n-1,a,b,r);
  }
}

tree(trk,12,a=25,b=40,r=.75);

Étiquettes : , , ,


Fractals with Asymptote – fig0030

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

Figure 0003
(Compiled with Asymptote version 1.87svn-r4652)
    
// Barnsley's fern
// Fougère de Barnsley
size(5cm,0);

real ab=85, ac=-5;
real rc=.85, rb=-.31;
path trk=(0,0)--(0,1);

transform ta=shift(0,1)*rotate(ab)*scale(rb);
transform tb=shift(0,1)*rotate(-ab)*scale(rb);
transform tc=shift(0,1)*rotate(ac)*scale(rc);

picture fern(int n) {
  picture opic;
  draw(opic,trk^^ta*trk^^tb*trk^^tc*trk);
  if (n==0) return opic;
  picture branch=fern(n-1);
  add(opic,branch);
  add(opic,ta*branch);
  add(opic,tb*branch);
  add(opic,tc*branch);
  return opic;
}

add(fern(6));

É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 : , , ,


Fractals with Asymptote – fig0070

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

Figure 0007
(Compiled with Asymptote version 1.87svn-r4652)
    
//Translate from http://zoonek.free.fr/LaTeX/Metapost/metapost.html
size(8cm);
void koch(pair A, pair B, int n) {
  pair C;
  C =rotate(120, point(A--B,1/3))*A;
  if (n>0) {
    koch( A,        point(A--B,1/3), n-1);
    koch( point(A--B,1/3), C,        n-1);
    koch( C,        point(A--B,2/3), n-1);
    koch( point(A--B,2/3), B,        n-1);
  } else draw(A--point(A--B,1/3)--C--point(A--B,2/3)--B);
}

pair z0=(1,0);
pair z1=rotate(120)*z0;
pair z2=rotate(120)*z1;
koch( z0, z1, 3 );
koch( z1, z2, 3 );
koch( z2, z0, 3 );

Étiquettes : ,


Fractals with Asymptote – fig0090

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

Figure 0009
(Compiled with Asymptote version 1.87svn-r4652)
    
size(10cm,0);

real a=-1.5, b=2a/3;

picture H(pen p=currentpen) {
  picture H;
  draw(H,(-a,0)--(a,0)^^(-a,-b)--(-a,b)^^(a,-b)--(a,b),p);
  return H;
}

transform sc=scale(0.5);
transform[] t={identity(),
               shift(-a,b)*sc, shift(-a,-b)*sc,
               shift(a,b)*sc,  shift(a,-b)*sc};

picture Hfractal(int n, pen p=currentpen)
{
  picture pic;
  if(n == 0) return H(p);
  picture Ht=Hfractal(n-1,p);
  for (int i=0; i < 5; ++i) add(pic,t[i]*Ht);
  return pic;
}

add(Hfractal(4, bp+0.5*red));

Étiquettes : , , , ,


Fractals with Asymptote – fig0100

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

Figure 0010
(Compiled with Asymptote version 1.87svn-r4652)
    
size(10cm,0);

real a=-1.5, b=2a/3;

path[] H=(-a,0)--(a,0)^^(-a,-b)--(-a,b)^^(a,-b)--(a,b);

transform sc=scale(0.5);
transform[] t={shift(-a,b)*sc, shift(-a,-b)*sc,
               shift(a,b)*sc,  shift(a,-b)*sc};

void Hfractal(path[] g, int n, pen[] p=new pen[]{currentpen})
{
  p.cyclic=true;
  if(n == 0) draw(H,p[0]); else {
    for (int i=0; i < 4; ++i) {
      draw(t[i]*g,p[n]);
      Hfractal(t[i]*g,n-1,p);
    }
  }
}

Hfractal(H, 5, new pen[] {0.8*red, 0.8*green, 0.8*blue, black, blue+red});

Étiquettes : , , , ,


Fractals with Asymptote – fig0110

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

Figure 0011
(Compiled with Asymptote version 1.87svn-r4652)
    
/* Explanations HERE */
import geometry;
size(10cm,0);

triangle[] dissect(triangle T, int n)
{
  if(n <= 0) return new triangle[]{T};
  triangle[] OT;
  point M=midpoint(T.BC);
  triangle[] Tp=dissect(triangle(M,T.A,T.B),n-1);
  for(triangle t : Tp) OT.insert(0,t);
  triangle[] Tp=dissect(triangle(M,T.C,T.A),n-1);
  for(triangle t : Tp) OT.insert(0,t);
  return OT;
}

triangle T=rotate(45)*triangle((1,1),(0,0),(2,0));
triangle[] DT=dissect(T,9);
path g;
transform R=reflect(T.BC);

for(int i : DT.keys) {
  draw(DT[i],miterjoin+0.9*red);
  draw(R*DT[i],miterjoin+0.9*red);
  g=g--centroid(DT[i]);
}

draw(scale(sqrt(2))*unitsquare,bp+miterjoin+0.8*blue);
draw(g--reverse(R*g)--cycle,bp+miterjoin+yellow);

shipout(bbox(sqrt(2)*mm, Fill(black)));

Étiquettes : , , , , ,


Fractals with Asymptote – fig0120

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

Figure 0012
(Compiled with Asymptote version 1.87svn-r4652)
    
/* Explanations HERE */
size(12cm,0);

import geometry;

triangle T=triangleAbc(90,Tan(30),1);

triangle[] reverse(triangle[] arr)
{
  triangle[] or;
  int l=arr.length;
  for(int i=0; i < l; ++i) {
    or.push(arr[l-i-1]);
  }
  return or;
}

triangle[] dissect(triangle T, int n, bool reverse=false)
{
  if(n <= 0) return new triangle[]{T};
  triangle[] OT;

  point M=curpoint(T.AB,T.b()*Tan(30));
  point H=projection(T.BC)*M;
  triangle[] OT1, OT2, OT3;
  OT.append(dissect(triangle(H,T.B,M),n-1,!reverse));
  OT.append(reverse((dissect(triangle(H,T.C,M),n-1,!reverse))));
  OT.append(dissect(triangle(T.A,T.C,M),n-1,!reverse));
  return OT;
}

triangle[] DT=dissect(T,5);
point O=centroid(DT[0]);
path g;
transform Ro=rotate(30,T.B), Re=reflect(T.BC), Roj;

for(int i : DT.keys) {
  O=incenter(DT[i]);
  g=g--O;
}

g=reverse(g);
path G, g=g--Re*reverse(g) ;
for (int j=0; j < 12; j += 2) G=G--Ro^(-j)*g;

fill(G--cycle,0.3*blue);

for(int i : DT.keys) {
  for (int j=0; j < 12; j += 2) {
    Roj=Ro^j;
    draw(Roj*DT[i],miterjoin+0.8*red);
    draw(Roj*(Re*DT[i]),miterjoin+0.8*red);
  }
}

draw(G--cycle,bp+miterjoin+0.9*yellow);

shipout(bbox(2mm, FillDraw(black, 1mm+miterjoin+deepblue)));

Étiquettes : , , , , ,


Official Asymptote example – fractaltree

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

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

path ltrans(path p,int d)
{
  path a=rotate(65)*scale(0.4)*p;
  return shift(point(p,(1/d)*length(p))-point(a,0))*a;
}
path rtrans(path p, int d)
{
  path a=reflect(point(p,0),point(p,length(p)))*rotate(65)*scale(0.35)*p;
  return shift(point(p,(1/d)*length(p))-point(a,0))*a;
}

void drawtree(int depth, path branch)
{
  if(depth == 0) return;
  real breakp=(1/depth)*length(branch);
  draw(subpath(branch,0,breakp),deepgreen);
  drawtree(depth-1,subpath(branch,breakp,length(branch)));
  drawtree(depth-1,ltrans(branch,depth));
  drawtree(depth-1,rtrans(branch,depth));
  return;
}

path start=(0,0)..controls (-1/10,1/3) and (-1/20,2/3)..(1/20,1);
drawtree(6,start);


Étiquettes : ,