#define res iResolution.xy
#define tau 6.283

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

vec4 _mul(vec4 a, vec4 b) { //multiply 2 dual-complex numbers
    return vec4(
        cmul(a.xy, b.xy),
        cmul(a.xy, b.zw) + cmul(a.zw, b.xy)
        //(a.x*b.z + a.z*b.x) - (a.y*b.w + a.w*b.y),
        //(a.x*b.w + a.z*b.y) + (a.y*b.z + a.w*b.x)
    );
}

vec2 csqr(vec2 z) {
    return vec2(z.x*z.x - z.y*z.y, 2.*z.x*z.y);
}

vec4 _sqr(vec4 z) {
    return vec4(csqr(z.xy), cmul(z.zw, z.xy * 2.));
}

vec2 cinv(vec2 z) {
    return z / dot(z,z) * vec2(1,-1);
}

vec4 _inv(vec4 z) {
    return vec4(
        cinv(z.xy),
        cmul(z.zw, -cinv(cmul(z.xy, z.xy)))
    );
}

vec2 cexp(vec2 z) {
    return vec2(cos(z.y), sin(z.y)) * exp(z.x);
}

vec4 _exp(vec4 z) {
    vec2 tmp = cexp(z.xy);
    return vec4(tmp, cmul(z.zw, tmp));
}

vec4 _log(vec4 z) {
    return vec4(
        log(dot(z.xy,z.xy)) * .5,
        atan(z.y,z.x),
        cmul(z.zw, cinv(z.xy))
    );
}

// i havent tested these (or maybe i have and i forgot, i probably made this before even the world event)
vec4 _sin(vec4 z) {
    return _mul((_exp(_mul(z, vec4(0,1,0,0))) - _exp(_mul(z, vec4(0,-1,0,0)))), vec4(0,-.5,0,0));
}

vec4 _cos(vec4 z) {
    return (_exp(_mul(z, vec4(0,1,0,0))) + _exp(_mul(z, vec4(0,-1,0,0)))) * .5;
}

vec4 _tan(vec4 z) {
    return _mul(_sin(z), _inv(_cos(z)));
}

void mainImage(out vec4 col, in vec2 coord) {
    col = vec4(0);
    for(int smpl = 0; smpl < 3; smpl++) {
        vec4 c = vec4(((coord + fract(vec2(.754877, .56984) * float(smpl)) - .5) * 2. - res) / res.y, 1, 0);
        c = c * exp(-iTime) + vec4(-1.7465226,0.0000280,0,0);
        vec4 z = vec4(0);
        int i;
        // lcm(1,2...8) is better known for other properties
        for(i = 0; i < 420; i++) {
            if(dot(z.xy,z.xy) > 1e3) break;
            z = _sqr(z) + c;
        }
        float l = dot(z.xy, z.xy);
        vec2 dl = mat2(z.zw * vec2(1,-1), z.wz) * z.xy;
        dl /= l;
        l = log(l);
        dl /= l*.5;
        l = log(l*.5);
        col.rgb += (sqrt(cos((log(length(dl)) * .3 + iTime - vec3(0,1,2)/3.) * tau) * .5 + .5));
        
        //i dont know what these comments are for
        
        //* (max(dot(normalize(vec3(cos(iTime*tau),sin(iTime*tau),1)), vec3(normalize(dl), 1.) * sqrt(.5)), 0.) * .6 + .4);
        //col = vec4(float(i) / 1000.);
    }
    col /= 3.;
}
