//Complex utility functions from https://harrisonclarke.wordpress.com/2010/02/07/fractals-on-the-gpu-glsl/ 
//and http://www.codeproject.com/Articles/492355/Domain-Coloring-Method-on-GPU 

float c_abs(vec2 z)
{
    return sqrt(z.x * z.x + z.y * z.y);
}

float c_arg(vec2 z)
{
    return atan(z.y, z.x);
}

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

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

vec2 c_pow_4(vec2 z)
{
    vec2 y = z;
    for (int i=1; i<4; i++){
    	y = c_mult(y, z);
    }
    return y;
}

vec2 c_pow_3(vec2 z)
{
    vec2 y = z;
    for (int i=1; i<2; i++){
    	y = c_mult(y, z);
    }
    return y;
}


void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
	vec2 xy = ((fragCoord.xy - (iResolution.xy / 2.0)) / iResolution.xy);
    vec2 uv = xy *6.0;
    vec2 a = c_pow_3(uv) - vec2(uv.y, uv.x);
    vec2 b = (c_pow_4(uv)* iTime*0.01) - vec2(uv.x);
    vec2 result = c_div(a,b);
    fragColor = vec4(vec3(c_arg(result), mod(c_arg(result), 2.0) / 2.0, mod(c_arg(result), 2.0)) / 4.0, 1.0);
	//fragColor = vec4(vec3(c_arg(result)), 1.0);
}
