Fractals
List of pictures

figure 0001

figure 0002

figure 0003

figure 0004

figure 0005

figure 0006

figure 0007

figure 0008

figure 0009

figure 0010

figure 0011

figure 0012
 Figure 0001: fig0010.asy(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);
return pic;
}

```
 Figure 0002: fig0020.asy(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);

```
 Figure 0003: fig0030.asy(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);
return opic;
}

```
 Figure 0004: fig0040.asy(Compiled with Asymptote version 1.87svn-r4652)
```
// 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;
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;

pair x=(0,1);
nbit=23;
for(int i=1; i<=nbit; ++i) {
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)));

```
 Figure 0005: fig0050.asy(Compiled with Asymptote version 1.87svn-r4652)
```
// 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);
}

```
 Figure 0006: fig0060.asy(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);

```
 Figure 0007: fig0070.asy(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 );

```
 Figure 0008: fig0080.asy(Compiled with Asymptote version 1.87svn-r4652)
```
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;
}

```
 Figure 0009: fig0090.asy(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;
}

```
 Figure 0010: fig0100.asy(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});

```
 Figure 0011: fig0110.asy(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)));

```
 Figure 0012: fig0120.asy(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)));

```