summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/examples/dragon.asy
blob: 08fcb61f5c0f0faeef1ffb26b659a07179869ed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
pair crease(pair z1, pair z2, bool left)
{
  pair dz = z2 - z1;

  if (left)
    return z1 + dz * (0.5, 0.5);
  else
    return z1 + dz * (0.5, -0.5);
}

pair[] fold(pair[] oldz)
{
  int n = oldz.length;
  pair[] newz = new pair[2n-1];

  for (int i = 0; i < n-1; ++i)
    {
      newz[2i] = oldz[i];
      newz[2i+1] = crease(oldz[i], oldz[i+1], i%2==0);
    }

  newz[2(n-1)] = oldz[n-1];

  return newz;
}

pair[] dragon(int n, pair[] base={})
{
  if (base.length == 0)
    if (n%2 == 0)
      base = new pair[] {(0,0), (1,1) };
    else
      base = new pair[] {(0,0), (1,0) };

  pair[] z = base;

  for (int i = 1; i < n; ++i)
    z = fold(z);

  return z;
}

void drawtris(pair[] z, pen p = currentpen)
{
  int n = z.length;

  for (int i = 0; i < n-2; i+=2)
    fill(z[i]--z[i+1]--z[i+2]--cycle, p);
}

void drawtris(pair[] z, pen p1, pen p2)
{
  int n = z.length;

  for (int i = 0; i < n-2; i+=2)
    fill(z[i]--z[i+1]--z[i+2]--cycle, 2i < n-1 ? p1 : p2);
}

size(500,0);

int n = 10;

drawtris(dragon(n, new pair[] {(0,0), (1,0)}), black);
drawtris(dragon(n, new pair[] {(0,0), (0,-1)}), blue);
drawtris(dragon(n, new pair[] {(0,0), (-1,0)}), red);
drawtris(dragon(n, new pair[] {(0,0), (0,1)}),  green);