% % Theresa Sawi, 11/20/2016
% % EPS109 Final Project: Cobwebbing with a Spider
% % 6 versions with varying r's and initial positions


function cobweb

 hold on 
 rTotal = [2.85 2.85 2.85 3 3.8 3.9]; %loop through these r values
 xxTotal = [0.5 0.1  0.9  0.1 0.1 0.1]; %and these initial x positions
 iterTotal = [10 10 10 20 30 30];        %and these # of iterations
 
 rtitle = {'r = 2.85, starting at (0.5,0.5)', 'r = 2.85, starting at (0.1,0.1)', ...
     'r = 2.85, starting at (0.9,0.9)', 'r = 3; starting at (0.1,0.1)', ...
     'r = 3.8, starting at (0.1,0.1)', 'r = 3.9, starting at (0.1,0.1)'};
 
 sf = 0.5; %scaling factor; spider traverses this much web per movement

  
global Lx Lx2 Ly1 Ly2  %%%%%Leg Parameters (Upper1, upper2, lower1, lower2)
Lx = 0.04;
Lx2 = 0.03;
Ly1 = 0.055;
Ly2 = 0.02;

for ri = 1 : length(rTotal)
    
clf
hold on
r = rTotal(ri);
xx = xxTotal(ri);
iter = iterTotal(ri);

% % BRANCH 1
x1 = 0 : .01 : 100;     %Branch
y = logistic_map(x1,r);   %Branch
x2 = 0 : .05 : 100;     %Leaves
y2 = logistic_map(x2,r);  %Leaves

plot(x1,x1,'k', 'Color', [0.6 0.4 0.2], 'LineWidth',3);
plot(x2,x2,'go', 'MarkerFaceColor','g', 'MarkerSize', 9,'MarkerEdgeColor',[.01 .9 .01]);
plot(x2,x2,'+', 'MarkerFaceColor',[.01 .7 .01], 'MarkerSize', 9,'MarkerEdgeColor',[.01 .7 .01]);
plot(x2,x2,'*', 'MarkerFaceColor',[.01 .7 .01], 'MarkerSize', 7,'MarkerEdgeColor',[.01 .7 .01]);

% % BRANCH 2

plot(x1,y,'k', 'Color', [0.6 0.4 0.2],  'LineWidth',3);
plot(x2,y2,'go', 'MarkerFaceColor','g', 'MarkerSize', 7,'MarkerEdgeColor',[.01 .9 .01]);
plot(x2,y2,'+', 'MarkerFaceColor',[.01 .7 .01], 'MarkerSize', 7,'MarkerEdgeColor',[.01 .7 .01]);
plot(x2,y2,'*', 'MarkerFaceColor',[.01 .7 .01], 'MarkerSize', 5,'MarkerEdgeColor',[.01 .7 .01]);

%% %axis settings

axis([0 1 0 1])
xlabel('x')
ylabel('f(x)')
set(gca,'Color',[.5 .5 1]);
title(rtitle{ri}) 
fig = gcf;
fig.InvertHardcopy = 'off';
   


%% %initial fly position and all wing positions

flyBody = plot(1-1/r, 1-1/r,'ko', 'MarkerFaceColor','k');

w1u = plot(1-1/r+.02, 1-1/r+.01,'ko', 'MarkerFaceColor','w','MarkerSize',9);
w2u = plot(1-1/r-.02, 1-1/r+.01,'ko','MarkerFaceColor','w','MarkerSize',9);
w1u.Visible = 'off'; w2u.Visible = 'off';

w1d = plot(1-1/r+.02, 1-1/r,'ko','MarkerFaceColor','w','MarkerSize',9);
w2d = plot(1-1/r-.02, 1-1/r,'ko','MarkerFaceColor','w','MarkerSize',9);
w1d.Visible = 'off'; w2d.Visible = 'off';


w1 = plot(1-1/r+.02, 1-1/r,'ko','MarkerFaceColor','w','MarkerSize',9);
w2 = plot(1-1/r-.02, 1-1/r,'ko','MarkerFaceColor','w','MarkerSize',9);

 for i = 2:iter
        yy = logistic_map(xx,r);    %calculate new yy
       
        if yy > xx                  % choose orientation, alpha
            alpha = 1;
        else alpha = -1;
        end

    %%   plot spider/web segment 1a
        x1 = xx; x2 = xx;
        y1 = xx; y2 = xx+alpha*sf*abs(xx-yy);

        plot([x1,x2], [y1,y2], 'LineStyle', '-','Color', [0.9 0.9 0.9], 'LineWidth',1); %1
        s1a = plot(x2,y2, 'ko', 'MarkerFaceColor','k');

        L = legUpper1(x2,y2);
        L2 = legLower1(x2,y2);

        for j = 1 : 8
            leg2_new(j) = plot([L2(j,1) L2(j,2)],[L2(j,3) L2(j,4)], 'k');
            leg_new(j) = plot([L(j,1) L(j,2)],[L(j,3) L(j,4)], 'k');
             
        end

        pause(.06)
        
        
        %% %%%wings moves up
        w1.Visible = 'off'; w2.Visible = 'off'; 
        w1d.Visible = 'off'; w2d.Visible = 'off';
        w1u.Visible = 'on'; w2u.Visible = 'on';
        
        
      %% plot spider/web segment 1b
        x1 = xx; x2 = xx;
        y1 = xx+alpha*sf*abs(xx-yy); y2 = yy;

        plot([x1,x2], [y1,y2], 'LineStyle', '-','Color', [0.9 0.9 0.9], 'LineWidth',1); 
        s1b = plot(x2,y2, 'ko', 'MarkerFaceColor','k');
        s1a.Visible = 'off';
        
        L = legUpper1(x2,y2);
        L2 = legLower1(x2,y2);

        for j = 1 : 8
            leg_new(j).Visible = 'off';
            leg2_new(j).Visible = 'off';
        end


        for j = 1 : 8
            leg_new(j) = plot([L(j,1) L(j,2)],[L(j,3) L(j,4)], 'k');
            leg2_new(j) = plot([L2(j,1) L2(j,2)],[L2(j,3) L2(j,4)], 'k');
             
        end


        pause(.06)
    %%  wings moves down
        w1u.Visible = 'off'; w2u.Visible = 'off';
        w1d.Visible = 'on'; w2d.Visible = 'on';

        
     %%  plot spider/web segment 2a
        x1 = xx; x2 = xx+alpha*sf*abs(xx-yy);
        y1 = yy; y2 = yy;   
          
        L = legUpper2(x2,y2);
        L2 = legLower2(x2,y2);
        
        for j = 1 : 8
            leg_new(j).Visible = 'off';
            leg2_new(j).Visible = 'off';
        end


        plot([x1,x2], [y1,y2], 'LineStyle', '-','Color', [0.9 0.9 0.9], 'LineWidth',1); 
        s2a = plot(x2,y2, 'ko', 'MarkerFaceColor','k');
        s1b.Visible = 'off';


        for j = 1 : 8
            leg_new(j) = plot([L(j,1) L(j,2)],[L(j,3) L(j,4)], 'k');
            leg2_new(j) = plot([L2(j,1) L2(j,2)],[L2(j,3) L2(j,4)], 'k');
             
        end

       pause(.06) 

       
        %% wings moves up
        w1.Visible = 'off'; w2.Visible = 'off'; 
        w1d.Visible = 'off'; w2d.Visible = 'off';
        w1u.Visible = 'on';  w2u.Visible = 'on';

        
    %%   plot spider/web segment 2b
        x1 = xx+alpha*sf*abs(xx-yy); x2 = yy;
        y1 = yy; y2 = yy;    
     
        L = legUpper2(x2,y2);
        L2 = legLower2(x2,y2);
   
        for j = 1 : 8
            leg_new(j).Visible = 'off';
            leg2_new(j).Visible = 'off';
        end

        plot([x1,x2], [y1,y2], 'LineStyle', '-','Color', [0.9 0.9 0.9], 'LineWidth',1); 
        s2b = plot(x2,y2, 'ko', 'MarkerFaceColor','k');
        s2a.Visible = 'off';

        for j = 1 : 8
            leg_new(j) = plot([L(j,1) L(j,2)],[L(j,3) L(j,4)], 'k');
            leg2_new(j) = plot([L2(j,1) L2(j,2)],[L2(j,3) L2(j,4)], 'k');
        end
      
        pause(.06)
        
        
        s2b.Visible = 'off';

        for j = 1 : 8
            leg_new(j).Visible = 'off';
            leg2_new(j).Visible = 'off';
        end
        

    %%  wings moves down
        w1u.Visible = 'off'; w2u.Visible = 'off';
        w1d.Visible = 'on'; w2d.Visible = 'on';

    
        
   %%
       xx = logistic_map(xx,r);
    

 end
 
end

%%plot final spider
x2 = 1-1/r; y2 = 1-1/r;
plot(x2,y2, 'ko', 'MarkerFaceColor','k');
L = legUpper1(x2,y2);
L2 = legLower1(x2,y2);

for j = 1 : 8
    leg_new(j) = plot([L(j,1) L(j,2)],[L(j,3) L(j,4)], 'k');
    leg2_new(j) = plot([L2(j,1) L2(j,2)],[L2(j,3) L2(j,4)], 'k');    
end


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

function y = logistic_map(x,r) 

y = r.*x.*(1-x);

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

function L = legUpper1(x2,y2)
global Lx Ly2 Ly1
    L = [x2 x2+Lx y2 y2+Ly1
     x2 x2+Lx y2 y2+Ly2
     x2 x2+Lx y2 y2-Ly1
     x2 x2+Lx y2 y2-Ly2
     x2 x2-Lx y2 y2+Ly1
     x2 x2-Lx y2 y2+Ly2
     x2 x2-Lx y2 y2-Ly1
     x2 x2-Lx y2 y2-Ly2];

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

function L2 = legLower1(x2,y2)
global Lx Lx2 Ly2 Ly1

  L2 = [x2+Lx x2+2*Lx y2+Ly1 y2+Ly2
         x2+Lx x2+2*Lx y2+Ly2 y2-Ly2
         x2+Lx x2+2*Lx   y2-Ly2 y2-3*Ly2
         x2+Lx x2+Lx2  y2-Ly1 y2-2*Ly1              
         x2-Lx x2-Lx2  y2-Ly1 y2-2*Ly1
         x2-Lx x2-2*Lx   y2-Ly2 y2-3*Ly2
         x2-Lx x2-2*Lx y2+Ly2 y2-Ly2
         x2-Lx x2-2*Lx y2+Ly1 y2+Ly2];      
 

% ------------------------------------
function L = legUpper2(x2,y2)
global Lx Lx2 Ly1

    L = [x2 x2+Lx  y2 y2+Ly1
         x2 x2+Lx2 y2 y2+Ly1
         x2 x2-Lx2 y2 y2+Ly1
         x2 x2-Lx  y2 y2+Ly1
         x2 x2+Lx  y2 y2-Ly1
         x2 x2+Lx2 y2 y2-Ly1
         x2 x2-Lx2 y2 y2-Ly1
         x2 x2-Lx  y2 y2-Ly1];
         
% ------------------------------------
function L2 = legLower2(x2,y2)
global Lx Lx2 Ly1

     L2 = [x2+Lx  x2+2*Lx  y2+Ly1 y2+Ly1
           x2+Lx2 x2+3*Lx2 y2+Ly1 y2+2*Ly1
           x2-Lx2 x2-Lx2   y2+Ly1 y2+2*Ly1 
           x2-Lx  x2+Lx2   y2+Ly1 y2+2*Ly1
           x2+Lx  x2+2*Lx  y2-Ly1 y2-Ly1
           x2+Lx2 x2+3*Lx2 y2-Ly1 y2-2*Ly1
           x2-Lx2 x2-Lx2   y2-Ly1 y2-2*Ly1
           x2-Lx  x2+Lx2   y2-Ly1 y2-2*Ly1];
       
       
     
       
       
       
       
       
       
       
       
       
       
         