package math;

/**
 * The rectilinear metric (i.e., the sum of the absolute values of the
 * differences between the elements).  This is sometimes also
 * called the Manhattan metric (because it is the distance you have to walk
 * between two points in a city that is layed out on a grid).
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class      RectilinearMetric
       implements Metric
{
    /**
     * Calculate the distance between two n-dimensional points
     * (required by Metric)
     *
     * Note: For simplicity, this method does not confirm that the
     * two arrays are the same size. It uses the smaller size.
     *
     * @param a   One n-dimensional point
     * @param b   Another n-dimensional point
     * @return    The distance
     */
    public double distance(double[] a, double[] b)
    {
       double  result;       
       int     n;
       
       result = 0.0;       
       n      = Math.min(a.length, b.length);

       for (int i=0; i<n; i++)
       {
          result += Math.abs(a[i]-b[i]);          
       }

       return result;       
    }
    

}
