function interpshow(XData, YData)

if nargin < 2
    XData = linspace(.1, .9, 6);
    YData = .5*ones(1, 6);
end;

fig = figure( ...
    'Name', 'Interp Show', ...
    'MenuBar', 'none', ...
    'Resize', 'Off', ...
    'DoubleBuffer', 'on', ...
    'NumberTitle', 'off', ...
    'WindowButtonUpFcn', @ButtonUp, ...
    'Position', [200, 200, 600, 500]);

ax = axes('ButtonDownFcn', @NewPoint,...
    'XLim', [0, 1], 'YLim', [0, 1]);

cmenu = uicontextmenu;
uimenu(cmenu, 'Label', 'Delete', 'Callback', @DeletePoint);

Points = line( ...
    'XData', XData, ...
    'YData', YData, ...
    'ButtonDownFcn', @ButtonDown, ...
    'Marker', 'o', ...
    'MarkerFaceColor', 'w', ...
    'MarkerEdgeColor', 'b', ...
    'Color', 'k', ...
    'UIContextMenu', cmenu, ...
    'Visible', 'On', ...
    'LineStyle', '-.');

Pchip = line('HitTest', 'Off', 'Color', [1/2 1/2 0], 'Visible' , 'off');
Spline = line('HitTest', 'Off', 'Color', 'r', 'Visible' , 'off');
Poly = line('HitTest', 'Off', 'Color', [0 1/2 1/2], 'Visible' , 'off');
Linear = line('HitTest', 'Off', 'Color', [1/2 0 1/2], 'Visible' , 'off');
Quadratic = line('HitTest', 'Off', 'Color', 'g', 'Visible' , 'off');
Cubic = line('HitTest', 'Off', 'Color', 'b', 'Visible' , 'off');

xc = 445;
yc = 280;

uicontrol('Position', [xc, yc, 90, 173], ...
    'Style', 'Frame', ...
    'BackGroundColor', 'w');
uicontrol('Position', [xc + 10, yc + 150, 70, 10], ...
    'Style', 'Check', 'String', 'Points', 'Value', 1, ...
    'BackGroundColor', 'w', ...
    'CallBack', @PointsVisible);
uicontrol('Position', [xc + 10, yc + 130, 70, 10], ...
    'Style', 'Check', 'String', 'Line', 'Value', 1, ...
    'BackGroundColor', 'w', ...
    'CallBack', @LineVisible);
control(110, 'Pchip', Pchip);
control(90, 'Spline', Spline);
control(70, 'Poly', Poly);
control(50, 'Linear', Linear);
control(30, 'Quadratic', Quadratic);
control(10, 'Cubic', Cubic);

redraw;

    function control(pos, string, obj)
        uicontrol('Position', [xc + 10 yc + pos 70 10], ...
            'Style', 'Check', 'String', string, 'Value', 0, ...
            'ForegroundColor', get(obj, 'Color'), ...
            'BackGroundColor', 'w', ...
            'CallBack', {@MakeVisible, obj});
    end;

    function PointsVisible(h, eventdata)
        if get(h, 'Value') == 1
            set(Points, 'Marker', 'o');
        else
            set(Points, 'Marker', 'None');
        end;
        drawnow;
    end;

    function LineVisible(h, eventdata)
        if get(h, 'Value') == 1
            set(Points, 'LineStyle', '-.');
        else
            set(Points, 'LineStyle', 'None');
        end;
        drawnow;
    end;

    function MakeVisible(h, eventdata, obj)
        if get(h, 'Value') == 1
            set(obj, 'Visible', 'On');
        else
            set(obj, 'Visible', 'Off');
        end;
        drawnow;
    end;

    function redraw
        x = get(Points, 'XData');
        y = get(Points, 'YData');
        [x, i] = sort(x);
        y = y(i);
        
        n = length(XData);
        try
            xx = linspace(x(1), x(end), 100);
            SplineY = spline(x, y, xx);
            PchipY = pchip(x, y, xx);
            P = polyfit(x, y, n - 1); 
            PolyY = polyval(P, xx);
            LinearY = polyval(polyfit(x, y, 1), xx);
            QuadraticY = polyval(polyfit(x, y, 2), xx);
            CubicY = polyval(polyfit(x, y, 3), xx);
        catch
            xx = [];
            SplineY = [];
            PchipY = [];
            PolyY = [];
            LinearY = [];
            QuadraticY = [];
            CubicY = [];
        end;
        set(Points, 'XData', x, 'YData', y);
        set(Pchip, 'XData', xx, 'YData', PchipY);
        set(Spline, 'XData', xx, 'YData', SplineY);
        set(Poly, 'XData', xx, 'YData', PolyY);
        set(Linear, 'XData', xx, 'YData', LinearY);
        set(Quadratic, 'XData', xx, 'YData', QuadraticY);
        set(Cubic, 'XData', xx, 'YData', CubicY);
    end;

    function NewPoint(h, eventdata)
         p = get(ax, 'CurrentPoint');

        XData = get(Points, 'XData');
        YData = get(Points, 'YData');
        XData = [XData, p(1,1)];
        YData = [YData, p(1,2)];
        set(Points, 'XData', XData, 'YData', YData);

        redraw;
    end;


    function ButtonDown(h, eventdata)
         p = get(ax, 'CurrentPoint');
        p = p(1, 1:2);
        XData = get(Points, 'XData');
        YData = get(Points, 'YData');

        moving_point = FindPoint(p, XData, YData);

        set(fig, 'WindowButtonMotionFcn', @MovePoint, ...
            'Pointer', 'Circle');

        function MovePoint(h, eventdata)
            p = get(ax, 'CurrentPoint');
            p = p(1, 1:2);

            XData(moving_point) = p(1);
            YData(moving_point) = p(2);
            set(Points, 'XData', XData, 'YData', YData);

            redraw;
        end;
     end;

    function ButtonUp(h, eventdata)
        set(fig, 'WindowButtonMotionFcn', '', ...
            'Pointer', 'Arrow');
    end;


    function DeletePoint(h, eventdata)
        p = get(ax,'CurrentPoint');
        p = p(1, 1:2);
        XData = get(Points, 'XData');
        YData = get(Points, 'YData');

        i = FindPoint(p, XData, YData);
        XData(i) = [];
        YData(i) = [];
        set(Points, 'XData', XData, 'YData', YData);

        redraw;
    end;

    function i = FindPoint(p, XData, YData);
        [m, i] = min((XData - p(1)).^2 + (YData - p(2)).^2);
    end;

end



