//https://www.shadertoy.com/view/Wtt3Dl
#define PI  3.14159265359
#define TWO_PI (2.0*PI)
#define HALF_PI (0.5*PI)

float smst(float val, float width, float sharp)
{
    return smoothstep(width/2.+(sharp/2.), width/2.-(sharp/2.), val);
}

mat2 rot2d(float deg)
{
    float rad = radians(deg);
    return mat2(cos(rad), -sin(rad),
                sin(rad), cos(rad));
}

vec2 Polar(vec2 p)
{
    float rho = length(p) * 2.;
    float phi = (atan(p.y, p.x) + PI) / (PI * 2.);
    
    return vec2(rho, phi);
}

vec2 Sectors(vec2 p, float amount)
{
    // polar coordinates
    vec2 polarUv = Polar(p);

    // each sector its own distinct (center) angle
    float sectorId = floor(polarUv.y * amount) / amount + (0.5 / amount);
    
    // rotate incoming grid (+90 to stay in quadrant I & II)
    return rot2d(sectorId * 360. + 90.) * p;
}

vec2 Grid(vec2 p, float scale, float thickness)
{
    vec2 frct = abs(fract(p * scale)-0.5) * 2.;
    float lineThick = thickness / (1. / scale) / iResolution.y;
    
    float x = 1. - smst(frct.x, 2.-lineThick, lineThick);
    float y = 1. - smst(frct.y, 2.-lineThick, lineThick);
    
    return vec2(x, y);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    vec3 col = vec3(0.);
    vec2 mouse = iMouse.xy / iResolution.xy;
    
    float nrofSectors = 5.; // mouse.x * 36.;
    
    vec2 euclSectUv = Sectors(uv, nrofSectors);
        	
    // add coordinate grid
    vec2 coords = max(euclSectUv, Grid(euclSectUv, 20., 2.5));
    
    // draw
    col = vec3(coords, 0.);
    // col = vec3(uv, 0.);
    // col = vec3(sectorId);
    // col = vec3(polarUv, 0.);
    
    // Output to screen
    fragColor = vec4(col,1.0);
    
/*
	vec2 R = iResolution.xy, U = (fragCoord - .5 *R ) / R.y;
	float s = iMouse.x/R.x * 1e2, p = 3.*s/R.y;
	R = abs( fract(U*s)*2. - 1. );
     
	fragColor = vec4( smoothstep(-p, p, R - 1.+.001*s) ,0,1);
*/
}