// The MIT License
// Copyright ? 2019 Inigo Quilez
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// Distance to a 2D joint. Also doable in 3D: https://www.shadertoy.com/view/3ld3DM

// List of some other 2D distances:
//
// Triangle:             https://www.shadertoy.com/view/XsXSz4
// Isosceles Triangle:   https://www.shadertoy.com/view/MldcD7
// Regular Triangle:     https://www.shadertoy.com/view/Xl2yDW
// Regular Pentagon:     https://www.shadertoy.com/view/llVyWW
// Regular Octogon:      https://www.shadertoy.com/view/llGfDG
// Rounded Rectangle:    https://www.shadertoy.com/view/4llXD7
// Rhombus:              https://www.shadertoy.com/view/XdXcRB
// Trapezoid:            https://www.shadertoy.com/view/MlycD3
// Polygon:              https://www.shadertoy.com/view/wdBXRW
// Hexagram:             https://www.shadertoy.com/view/tt23RR
// Regular Star:         https://www.shadertoy.com/view/3tSGDy
// Star5:                https://www.shadertoy.com/view/wlcGzB
// Ellipse 1:            https://www.shadertoy.com/view/4sS3zz
// Ellipse 2:            https://www.shadertoy.com/view/4lsXDN
// Quadratic Bezier:     https://www.shadertoy.com/view/MlKcDD
// Uneven Capsule:       https://www.shadertoy.com/view/4lcBWn
// Vesica:               https://www.shadertoy.com/view/XtVfRW
// Cross:                https://www.shadertoy.com/view/XtGfzw
// Pie:                  https://www.shadertoy.com/view/3l23RK
// Arc:                  https://www.shadertoy.com/view/wl23RK
// Horseshoe:            https://www.shadertoy.com/view/WlSGW1
// Parabola:             https://www.shadertoy.com/view/ws3GD7
// Rounded X:            https://www.shadertoy.com/view/3dKSDc
// Joint:                https://www.shadertoy.com/view/WldGWM

//
// and many more here:   http://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm

vec3 sdJoint2DSphere( in vec2 p, in float l, in float a, float w)
{
    // if perfectly straight
    if( abs(a)<0.001 )
    {
        float v = p.y;
        p.y -= clamp(p.y,0.0,l);
		return vec3( length(p), p.x, v );
    }
    
    // parameters
    vec2  sc = vec2(sin(a),cos(a));
    float ra = 0.5*l/a;
    
    // recenter
    p.x -= ra;
    
    // reflect
    vec2 q = p - 2.0*sc*max(0.0,dot(sc,p));

	// distance
    float u = abs(ra)-length(q);
    float d = (q.y<0.0) ? length( q+vec2(ra,0.0) ) : abs(u);

    // parametrization (optional)
    float s = sign(a);
    float v = ra*atan(s*p.y,-s*p.x);
    u = u*s;
    if( v<0.0 )
    {
        if( s*p.x>0.0 ) { v = abs(ra)*6.283185 + v; }
        else { v = p.y; u = q.x + ra; }
    }
    
    return vec3( d-w, u, v );
}

vec3 sdJoint2DFlat( in vec2 p, in float l, in float a, float w)
{
    // if perfectly straight
    if( abs(a)<0.001 )
    {
        float v = p.y;
        p.y -= clamp(p.y,0.0,l);
		return vec3( length(p), p.x, v );
    }
    
    // parameters
    vec2  sc = vec2(sin(a),cos(a));
    float ra = 0.5*l/a;
    
    // recenter
    p.x -= ra;
    
    // reflect
    vec2 q = p - 2.0*sc*max(0.0,dot(sc,p));

	// distance
    float u = abs(ra)-length(q);
    float d = max(length( vec2(q.x+ra-clamp(q.x+ra,-w,w), q.y) )*sign(-q.y),abs(u) - w);

    // parametrization (optional)
    float s = sign(a);
    float v = ra*atan(s*p.y,-s*p.x);
    u = u*s;
    
    return vec3( d, u, v );
}

float checker( in vec2 p )
{
    // filter kernel
    vec2 w = fwidth(p) + 0.01;  
    // analytical integral (box filter)
    vec2 i = 2.0*(abs(fract((p-0.5*w)/2.0)-0.5)-abs(fract((p+0.5*w)/2.0)-0.5))/w;
    // xor pattern
    return 0.5 - 0.5*i.x*i.y;                  
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // normalized pixel coordinates
    vec2 uv = (fragCoord*2.0-iResolution.xy)/iResolution.y;
    
    //uv = 0.5*(uv-vec2(0.0,0.5));
    
    // animation
    float le = 1.0;
    float an = 1.5*sin(iTime);
	float wi = 0.1;
    
    // distance and parametrization
    vec3 duv1 = sdJoint2DSphere(uv-vec2(-0.6,-0.3),le,an,wi);
    vec3 duv2 = sdJoint2DFlat(  uv-vec2( 0.6,-0.3),le,an,wi);
        
    vec3 duv = (duv1.x<duv2.x) ? duv1 : duv2;

    // round
    float d = duv.x;
    
    // coloring
    vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
    col *= 0.8 + 0.2*cos(128.0*d);
	col *= 1.0 - exp(-3.0*abs(d));
    if( d<0.0 )
    {
        col = checker(duv.yz*20.0)*vec3(1);
        //col = sqrt(texture(iChannel0,duv.yz).xyz);
        col *= mix(vec3(0,0.6,1),vec3(1,1,0),clamp(duv.z/le,0.0,1.0));
    }
	
	col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.015,abs(d)) );

	fragColor = vec4(col, 1.0);
}