function dist = vquaddist(bot_coefs,top_coefs,left,right)
% VQUADDIST Measure the average vertical distance between two quadratics
% 
% dist = VQUADDIST(bot_coefs,top_coefs,left,right) where bot_coefs and
% top_coefs are vectors of the form [c b a] for quadratic curves
% y=a*x^2+b*x+c, left and right are scalars representing the
% horizontal extent of the points to use.

% Jerod Weinman
% jerod@acm.org
% (c) 2011
    
% $Id: vquaddist.m 119 2012-01-25 18:07:59Z weinman $
 
% Jerod Weinman, Zach Butler, Dugan Knoll, Alexander Turner, Yirong Jing
% jerod@acm.org
% Copyright 2011, 2012, 2013, 2014
 
%    This file is part of PIONEER WordNorm.
%
%    PIONEER WordNorm is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    PIONEER WordNorm is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with PIONEER WordNorm.  If not, see <http://www.gnu.org/licenses/>.


% Extract coordinates for easier reading

a0 = bot_coefs(3);
b0 = bot_coefs(2);
c0 = bot_coefs(1);


a1 = top_coefs(3);
b1 = top_coefs(2);
c1 = top_coefs(1);

x0 = left;
x1 = right;

% Integral of difference, averaged
% Note the negative sign is employed because we assume an IJ
% coordinate system, where top has a LOWER offset than bottom.

dist = -( 1/3 * (a1 - a0) * ( x1^3 - x0^3 ) + ...
          1/2 * (b1 - b0) * ( x1^2 - x0^2 ) + ...
                (c1 - c0) * ( x1   - x0   )) / (x1 - x0);
