import math


def arrowplot(xst, yst, xend, yend, scale=1.0, headangle=15, headscale=0.2):    
    """Plot arrows to denote the direction and magnitude of the displacement. 
    Direction is indicated by the bearing of the arrow, and the magnitude is 
    indicated by the length of the arrow.
    
    :param x0: X coordinates for pt0
    :type x0: arr
    :param y0: Y coordinates for pt0
    :type y0: arr
    :param x1: X coordinates for pt1
    :type x1: arr
    :param y1: Y coordinates for pt1
    :type y1: arr
    :param scale: Arrow scale, defaults to 1.0
    :type scale: int, optional
    :param headangle: Plotting angle, defaults to 15
    :type headangle: int, optional
    :param headscale: Arrow head scale, defaults to 0.2
    :type headscale: int, optional
    :returns: Arrow plots as two arrays denoting x and y coordinates 
    :rtype: arr
    """    
    #Define plotting angle
    angle=math.pi*headangle/180.

    xs=[]
    ys=[]
    
    #Iterate through xy point data
    for i in range(len(xst)):
        #Get xy start position
        x1=xst[i]
        y1=yst[i]
        
        #Get xy end position
        x2=xend[i]
        y2=yend[i]
        
        #Calculate point displacement
        xd=x2-x1
        yd=y2-y1
        dist=math.sqrt(xd*xd+yd*yd)
        
        #Determine plotting angle
        sinTheta = xd / dist
        cosTheta = yd / dist
        sinTheta=min(sinTheta,1.0)
        cosTheta=min(cosTheta,1.0)
        sinTheta=max(sinTheta,-1.0)
        cosTheta=max(cosTheta,-1.0)
        aSinTheta = math.asin(sinTheta)
        
        #Determine length based on a given scale
        x2=x1+(xd*scale)
        y2=y1+(yd*scale)
        hs=dist*headscale

        #These conditions give an angle between 0 and 2 Pi radians
        #You should test them to make sure they are correct
        if (sinTheta >= 0.0 and cosTheta >= 0.0):
           theta = aSinTheta
        elif (cosTheta < 0.0):
           theta = math.pi - aSinTheta
        else:
           theta = (2.0 * math.pi + aSinTheta)
           
        theta=theta+math.pi
        x3=x2+hs*math.sin(theta+angle)
        x4=x2+hs*math.sin(theta-angle)
        y3=y2+hs*math.cos(theta+angle)
        y4=y2+hs*math.cos(theta-angle)
        
        #Append arrow plotting information
        xs.append(x1)
        ys.append(y1)
        xs.append(x2)
        ys.append(y2)
        xs.append(x3)
        ys.append(y3)
        xs.append(float('NaN'))
        ys.append(float('NaN'))
        xs.append(x2)
        ys.append(y2)
        xs.append(x4)
        ys.append(y4)
        xs.append(float('NaN'))
        ys.append(float('NaN'))
    
    #Return xy arrow plotting information
    return xs,ys