% dog catching mailman problem
% discrete time event simulation
% cda 6530 
% project 5
% PID: j2966213
% name: Jun Ye

maxIter = 1000;% maximum iteration
timeStep = 0.2;% time step



%type 1 mailman
dog_x = zeros(maxIter);
dog_y = 20*ones(maxIter);
mailman_x = 10* ones(maxIter);
%type 2 mailman

dog_x2 = zeros(maxIter);
dog_y2 = 20*ones(maxIter); 
mailman_x2 = 10*ones(maxIter);  


mailman_y = zeros(maxIter);% it is constant, actually not useful


%type 1 dog and type 1 mailman
%main loop

stepCnt = 0;
for i=2:maxIter
    stepCnt = stepCnt+1;
    curTime = timeStep*(i-1); % current time
    if curTime < 5
        dog_speed = 2*curTime;
    else
        dog_speed = 10;
    end
    
    if curTime <= 3
        mailman_speed = 0; % stand still to tie shoe lance
    else
        mailman_speed = 5;
    end
    
    
    %current distance
    % type 1
    distance = sqrt((abs(dog_x(i-1) - mailman_x(i-1)))^2 +(dog_y(i-1))^2);
    delta_dog_x = dog_speed*(mailman_x(i-1)-dog_x(i-1))/distance*timeStep;
    delta_dog_y = dog_speed*dog_y(i-1)/distance*timeStep;
    delta_mailman_x = mailman_speed*timeStep;
       
    %update the new position after the iteration
    dog_x(i) = dog_x(i-1) + delta_dog_x;
    dog_y(i) = dog_y(i-1) - delta_dog_y;
    
    if dog_y(i) < 0  % cross the x-axis
        dog_y(i) = 0;
        dog_x(i) = mailman_x(i-1);
    end
    mailman_x(i) = mailman_x(i-1) + delta_mailman_x;
    
    
    if (dog_y(i) == 0) && (dog_x(i)>= mailman_x(i))
        dog_x(i) = mailman_x(i);
        break;
    end
          
end

dog_x = dog_x(1: stepCnt+1);
dog_y = dog_y(1: stepCnt+1);
mailman_x = mailman_x(1: stepCnt+1);
mailman_y = mailman_y(1: stepCnt+1);


% type 1 dog and type 2 mailman
%main loop

stepCnt2 = 0;
for i=2:maxIter
    stepCnt2 = stepCnt2+1;
    curTime = timeStep*(i-1); % current time
    if curTime < 5
        dog_speed = 2*curTime;
    else
        dog_speed = 10;
    end
    
    mailman_speed2 = 2.5;
    
    %current distance
    % type 2 mailman
    distance = sqrt((abs(dog_x2(i-1) - mailman_x2(i-1)))^2 +(dog_y2(i-1))^2);
    delta_dog_x = dog_speed*(mailman_x2(i-1)-dog_x2(i-1))/distance*timeStep;
    delta_dog_y = dog_speed*dog_y2(i-1)/distance*timeStep;
    delta_mailman_x = mailman_speed2*timeStep;
       
    %update the new position after the iteration
    dog_x2(i) = dog_x2(i-1) + delta_dog_x;
    dog_y2(i) = dog_y2(i-1) - delta_dog_y;
    
    if dog_y2(i) < 0  % cross the x-axis
        dog_y2(i) = 0;
        dog_x2(i) = mailman_x2(i-1);
    end
    mailman_x2(i) = mailman_x2(i-1) + delta_mailman_x;
    
    if (dog_y2(i) == 0) && (dog_x2(i)>= mailman_x2(i))
        dog_x2(i) = mailman_x2(i);
        break;
    end
          
end

%statistics
finalCatchTime = stepCnt2*timeStep;
finalCatchPos = dog_x2(stepCnt2+1);
mid=(stepCnt2+1)/2;
if mid - floor(mid) < 0.00001  

    halfCatchDog_x = dog_x2(floor(mid));
    halfCatchDog_y = dog_y2(floor(mid));
    halfCatchMan_x = mailman_x2(floor(mid));
else
    halfCatchDog_x = (dog_x2(floor(mid))+dog_x2(ceil(mid)))/2;
    halfCatchDog_y = (dog_y2(floor(mid))+dog_y2(ceil(mid)))/2;
    halfCatchMan_x = (mailman_x2(floor(mid))+mailman_x2(ceil(mid)))/2;     
end


dog_x2 = dog_x2(1: stepCnt2+1);
dog_y2 = dog_y2(1: stepCnt2+1);
mailman_x2 = mailman_x2(1: stepCnt2+1);
mailman_y2 = mailman_y(1: stepCnt2+1);


plot(dog_x, dog_y, 'b+-', mailman_x, mailman_y, 'ro-');
hold;
plot(dog_x2, dog_y2, 'bs--', mailman_x2, mailman_y2, 'r^-');
xlabel('y(t)');
ylabel('x(t)');
title('Dog Chasing Mailman--Discrete Time Simulation (Type 1 dog)');
legend('dog Type 1','mailman Type 1','dog Type 1','mailman Type 2','location','northeast');
