aboutsummaryrefslogtreecommitdiff
path: root/sketches/02/newton.frag
blob: ac31d737632ba2b383e821d77549050948aa1ccc (plain) (blame)
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
varying vec4 v_position;
uniform vec2 u_resolution;
uniform float u_time;
uniform float u_mouse;

#define MAX_ITERATIONS 100

vec2 cmul(vec2 a, vec2 b) {
  return vec2(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x);
}

vec2 cdiv(vec2 a, vec2 b) {
  float denominator = b.x*b.x + b.y*b.y;
  if (denominator == 0.) return a;
  return cmul(a, vec2(b.x, -b.y)) / denominator;
}

vec2 cpow(vec2 z, float n) {
  float r = length(z);
  float theta = atan(z.y, z.x);
  return pow(r, n) * vec2(cos(n * theta), sin(n * theta));
}

void main() {
  float time = mod(1.1 + mod(u_time * 1000.0, 100000.0) / 10000.0, 10.);
  // float time = 2.0;

  float ratio = u_resolution.x / u_resolution.y;
  vec2 pos = v_position.xy/vec2(1.0, ratio);

  vec2 z = 2.0 * (pos - vec2(0., 0.));
  int i;

  vec2 prevZ = z;
  for (i = 0; i < MAX_ITERATIONS; i++) {
    prevZ = z;
    vec2 value = cpow(z, 3.0) + vec2(1., 0.);
    vec2 value_der = 3.0 * cpow(z, 2.0);
    z = z - time * cdiv(value, value_der);

    if (length(z - prevZ) > 30.0) {
      break;
    }
  }

  gl_FragColor = vec4(0., 0., 0., 1.);

  float diff = length(z - prevZ);

  if (diff > 10.0) {
    gl_FragColor = vec4(97., 42., 191., 255.0) / 255.;
  } else if (diff > 1.0) {
    gl_FragColor = vec4(150., 191., 69., 255.0) / 255.;
  } else if (diff > 0.0) {
    gl_FragColor = vec4(27., 14., 50., 255.0) / 255.;
  }
}