PIPRIME.FR BLOG

Live fully to honor life

Fractals with the Asymptote drawing software

Fractales produced with the Asymptote drawing and programming software

🔗fractales-fig001

Figure fractales 001 Generated with Asymptote

Show fractales/fig0010.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Function (creating) | #Fractals | #Recursion | #Transform | #Picture

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

🔗fractales-fig002

Figure fractales 002 Generated with Asymptote

Show fractales/fig0020.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Recursion | #Transform | #Function (creating)

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

🔗fractales-fig003

Figure fractales 003 Generated with Asymptote

Show fractales/fig0030.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Recursion | #Transform | #Picture | #Function (creating)

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

🔗fractales-fig004

Figure fractales 004 Generated with Asymptote

Show fractales/fig0040.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Transform | #Picture

// Barnsley's fern
// Fougère de Barnsley
size(10cm,0);

real ab=72, ac=-7;
real rc=0.85, rb=0.35;
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);
transform td=shift(0,1)*rotate((ab+ac)/2)*scale(rb);
transform te=shift(0,1)*rotate(-(ab+ac)/2)*scale(rb);

picture pic;
draw(pic,trk,red+.8green);

//Construct a fern branch as atractor
int nbit=7;
for(int i=1; i<=nbit; ++i) {
  picture pict;
  add(pict,ta*pic);
  add(pict,tb*pic);
  add(pict,tc*pic);
  draw(pict,(0,0)--(0,1), (2*(i/nbit)^2)*bp+((1-i/nbit)*green+i/nbit*brown));
  pic=pict;
}

//Use the fern branch to construct... a fern branch
picture pict;
add(pict,ta*pic);
add(pict,tb*pic);

pair x=(0,1);
nbit=23;
for(int i=1; i<=nbit; ++i) {
  add(shift(x)*rotate(ac*i)*scale(rc^i)*pict);
  draw(tc^i*((0,0)--(0,1)), 2*(1.5-i/nbit)^2*bp+brown);
  x=tc*x;
}

shipout(bbox(3mm, 2mm+black, FillDraw(paleyellow)));

🔗fractales-fig005

Figure fractales 005 Generated with Asymptote

Show fractales/fig0050.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Transform | #Picture

// Barnsley's fern
// Fougère de Barnsley
size(5cm,0);

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

transform [] t;
t[1] =shift(0,1)*rotate(ab)*scale(rb);
t[2] =shift(0,1)*rotate(-ab)*scale(rb);
t[3] =shift(0,1)*rotate(ac)*scale(rc);
real sum=0;

for(int i=0; i<100; ++i) sum+=(rc*cos(ac*pi/180))^i;
t[4] =xscale(0.01)*yscale(1/sum);

picture pic;
draw(pic,trk);
pair pt=(0,0);

for(int i=0; i < 1000; ++i) {
  pt=t[ 1+floor((3.0*rand()/randMax)) ]*pt;
}

int nbt;
for(int i=0; i < 200000; ++i) {
  nbt=1+floor((4.0*rand()/randMax));
  pt=t[nbt]*pt;
  draw(pt);
}

🔗fractales-fig006

Figure fractales 006 Generated with Asymptote

Show fractales/fig0060.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Recursion | #Random | #Function (creating)

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

🔗fractales-fig007

Figure fractales 007 Generated with Asymptote

Translate from http://zoonek.free.fr/LaTeX/Metapost/metapost.html

Show fractales/fig0070.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Recursion

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

🔗fractales-fig008

Figure fractales 008 Generated with Asymptote

Show fractales/fig0080.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Loop/for/while | #Picture

size(10cm,0);

real mandelbrot(pair c, real r, int count=100) {
  int i=0;
  pair z=c;
  do {
    ++i;
    z=z^2+c;
  } while (length(z) <= r && i<count);

  return (i<count) ? i/count : 0;
}

real r=4;
real step=.01;
real xmin=-2.25, xmax=.75;
real ymin=-1.3, ymax=0;

real x=xmin, y=ymin;
int xloop=round((xmax-xmin)/step);
int yloop=round((ymax-ymin)/step);
pen p;
path sq=scale(step)*unitsquare;

for(int i=0; i < xloop; ++i) {
  for(int j=0; j < yloop; ++j) {
    p=mandelbrot((x,y),r,20)*red;
    filldraw(shift(x,y)*sq,p,p);
    y += step;
  }
  x += step;
  y=ymin;
}

add(reflect((0,0),(1,0))*currentpicture);

🔗fractales-fig009

Figure fractales 009 Generated with Asymptote

Show fractales/fig0090.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Recursion | #Transform | #Picture | #Array

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

🔗fractales-fig010

Figure fractales 010 Generated with Asymptote

Show fractales/fig0100.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Recursion | #Transform | #Picture | #Array

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

🔗fractales-fig011

Figure fractales 011 Generated with Asymptote

Show fractales/fig0110.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Recursion | #Geometry | #Triangle | #Array | #Loop/for/while

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

🔗fractales-fig012

Figure fractales 012 Generated with Asymptote

Show fractales/fig0120.asy on Github.
Generated with Asymptote 3.00-0.
Categories : Surveys | Fractals
Tags : #Fractals | #Recursion | #Geometry | #Triangle | #Array | #Loop/for/while

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

0%