%% Mighty_Polyshape  
% 23-August-2022

% set(gcf,'pos',[20 20 240 240])  % gif
while 1
    shg
    cheeze = [243 200 48]/255;
    mousec = [47 177 186]/255;
    
    puzzle = imread('Cheese_puzzle.png');
    
    a = 150;
    b = 1132;
    c = 744;
    d = 215;
    r = 93;
    x = [623 825 a b c-d a+d c];
    y = [a b 865 663 c-d a+d c];
    z = [ 465 976; 965 924; 915 424; 415 475; 415 475];
    zm = [c-b-20 c-b+20];
    
    theta = -atand((y(4)-y(3))/(x(4)-x(3)));
    
    Cheese = repmat(polyshape(),5,1);
    for k = 1:5
        t = theta;
        e = 225;
        switch k
           case 1
               s0 = [a b];
               s1 = [x(2) y(2)] + r*arc(180,270-t);
               s2 = [x(7) y(7)] + r*arc(90-t,180-t);
               s3 = [x(3) y(3)] + r*arc(0-t,90);
           case 2
               s0 = [b b];
               s1 = [x(4) y(4)] + r*arc(90,180-t);
               s2 = [x(7) y(7)] + r*arc(0-t,90-t);
               s3 = [x(2) y(2)] + r*arc(270-t,360);
           case 3
               s0 = [b a];
               s1 = [x(1) y(1)] + r*arc(0,90-t);
               s2 = [x(7) y(7)] + r*arc(270-t,360-t);
               s3 = [x(4) y(4)] + r*arc(180-t,270);
           case 4
               s0 = [a a];
               s1 = [x(3) y(3)] + r*arc(270,360-t);
               s2 = [x(7) y(7)] + r*arc(180-t,e);
               s3 = [x(6) y(6)] + r*arc(e-180,e);
           case 5
               s0 = [a a];
               s1 = [x(5) y(5)] + r*arc(e,e+180);
               s2 = [x(7) y(7)];
               s3 = [x(1) y(1)] + r*arc(90-t,180);
        end

        s = [s0; s1; s2; s3; s0];
        Cheese(k) = polyshape(s);

        if k <= 3
            % holes
            s = z(k,:) + 0.6*r*arc(0,360);
            Cheese(k) = addboundary(Cheese(k),s);
        end
    end

    s = [b b] + r*arc(0,360);
    Mouse = polyshape(s);
    
    frame(a,b)
    hold on
    poly_plot(Cheese,cheeze)
    poly_plot(Mouse,mousec)
    hold off
    pause(4)
    
    % gif_frame('MCsolution.gif',inf);
    % gif_frame(5);
    % frames = 12;
    frames = 36;
    
    C = Cheese;
    M = Mouse;
    thetac = theta - 180;
    for f = 0:1/frames:1
        for k  = 1:5
            C(k) = rotate(Cheese(k),f*thetac,z(k,:));
        end
        M = translate(Mouse,f*zm);
        
        cla
        frame(a,b)
        hold on
        poly_plot(C,cheeze);
        poly_plot(M,mousec)
        hold off
        drawnow
        % gif_frame;
    end
    % gif_frame(10)
    % gif_frame('wrap')
    pause(4)
end

% ------------------------------------------------------------

function xy = arc(s,t)
    m = ceil(abs(t-s)/10);
    del = (t-s)/m;
    dt = (s:del:t)';
    xy = [cosd(dt) sind(dt)];
end

function frame(a,b)
    orange = [227 110 4]/255;
    % w = 30; lw = 8; ms = 30;
    w = 40; lw = 16; ms = 54;
    al = a-w;
    bl = b+w;
    plot([al bl bl al al],[al al bl bl al], ...
        'linewidth',lw,'color',orange)
    line(al,al,'marker','.', ...
        'markersize',ms,'color',orange)
end

function poly_plot(X,color)
    % lw = 1;
    lw = 2;
    plot(X,'facealpha',1.0,'facecolor',color, 'linewidth',lw);
    axis([0 1280 0 1280])
    axis square
    set(gca,'ydir','rev')
    axis off
end