# P-1 MCS 572 due Wed 28 Sep 2016 : predprey.py
"""
A prey is moving at constant speed one along the x-axis.
Starting at position (0, 1), a predator moves with constant speed
towards the prey.   The predator catches the prey if the distance
between predator and prey becomes less than 1.0e-1.
The trajectory of the predator is computed with a time stepping method.
Run this script with Python 2.6 or 2.7.  Needs matplotlib for the plot.
"""
from math import sqrt
import matplotlib.pyplot as plt

def prey(time, vlc):
    """
    The prey is moving with constant velocity vlc
    on the horizontal x-axis.
    Returns the tuple of x and y coordinates of
    the position of the prey at the time.
    """
    xpos = vlc*time
    ypos = 0
    return xpos, ypos

def predator(step, xprey, yprey, xpred, ypred, vlc):
    """
    Computes the new position for the predator,
    given the coordinates for the prey in (xprey, yprey),
    the coordinates for the predator in (xpred, ypred),
    and the velocity of the predator in vlc.
    The value for timestep is in step.
    """
    xdir = xprey - xpred
    ydir = yprey - ypred
    nrm = sqrt(xdir*xdir + ydir*ydir)
    if nrm < 1.0e-1:
        return xprey, yprey
    else:
        xdir = xdir/nrm
        ydir = ydir/nrm
        xpred = xpred + step*vlc*xdir
        ypred = ypred + step*vlc*ydir
        return xpred, ypred

def trajectory(endtime, dim, vlc):
    """
    Returns four lists with the coordinates
    of the prey and predator,
    computed for predator velocity equal to vlc.
    """
    step = endtime/dim
    xprey, yprey = ([0], [0])
    preyvel = 1
    (xpos, ypos) = (0, 10)
    xpred, ypred = ([xpos], [ypos])
    timestep = 0
    for k in range(dim):
        timestep = timestep + step
        x1p, x2p = prey(timestep, preyvel)
        xprey.append(x1p)
        yprey.append(x2p)
        xpos, ypos = predator(step, x1p, x2p, xpos, ypos, vlc)
        xpred.append(xpos)
        ypred.append(ypos)
        if (xpos == x1p and ypos == x2p): break
    return xprey, yprey, xpred, ypred, timestep

def main():
    """
    Plots one trajectory.
    """
    endtime = 30.0
    dim = 1000
    xprey, yprey, xpred, ypred, time = trajectory(endtime, dim, 1.05)
    print 'catch time :', time, 'index :', len(xprey)
    print 'xprey : %f yprey : %f' % (xprey[-1], yprey[-1])
    print 'xpred : %f ypred : %f' % (xpred[-1], ypred[-1])
    fig = plt.figure()
    fig.add_subplot(221)
    plt.plot(xprey[:250], yprey[:250], 'r', xpred[:250], ypred[:250], 'g')
    plt.plot(xprey[250], yprey[250], 'ro', xpred[250], ypred[250], 'go')
    plt.axis([0, endtime, -1, 11])
    fig.add_subplot(222)
    plt.plot(xprey[:500], yprey[:500], 'r', xpred[:500], ypred[:500], 'g')
    plt.plot(xprey[500], yprey[500], 'ro', xpred[500], ypred[500], 'go')
    plt.axis([0, endtime, -1, 11])
    fig.add_subplot(223)
    plt.plot(xprey[:750], yprey[:750], 'r', xpred[:750], ypred[:750], 'g')
    plt.plot(xprey[750], yprey[750], 'ro', xpred[750], ypred[750], 'go')
    plt.axis([0, endtime, -1, 11])
    fig.add_subplot(224)
    lst = len(xprey)-1
    plt.plot(xprey, yprey, 'r', xpred, ypred, 'g')
    plt.plot(xprey[lst], yprey[lst], 'ro', xpred[lst], ypred[lst], 'go')
    plt.axis([0, endtime, -0.5, 11])
    fig.show()
    raw_input('Hit enter to continue.')

main()
