%PLOTCUBICBEZ   Plot Cubic Bezier Curve
% This function treats the matrix of control points as a sequence of
% groupings of 4 control points at a time.  Each group of 4 control
% points forms a cubic Bezier curve.  The function simply plots each
% cubic curve in sequence.
%
% Syntax:  plotCubicBez(dt, CP)
%
% Inputs:
%     dt - the parameter increment size that you want points along the 
%          curve to be plotted at.  Remember the parameter varies from 
%          0 - 1 along the curve, dt = .01 will give 100 points per curve
%     CP - a nxm vector of n control points in 1,2, or 3-space.  The  number 
%          of control points must be a multiple of 4.
%
% Outputs:
%   - a plot of the characteristic polygon
%   - a plot of the curve as a series of equi-spaced points in terms of
%     the curve parameter t.
%   - a separate plot of velocity variation as a function of the parameter t
%
% Other m-files required: bezier.m
%
% See also: STARTCUBICBEZ, GOTHROUGH, STOPCUBICBEZ, BEZIER

% Author: Travis Hydzik
% Last revision: 20 October 2004

function plotCubicBez(dt, CP)

    [N, m] = size(CP);

    if rem(N,4) ~= 0
        error('the number of control points must be a multiple of 4');
        else 
            v = [];
            p = CP(1,:);
            x = p;
            
            for i = 1:4:N
                for t = dt:dt:1
                    pNew = bezier(t, CP(i:i+3,:));
                    v = [v norm(pNew - p)];
                    p = pNew;
                    x = [x; p];
                end
            end
            
            [N, m] = size(x);
            
            clf
            subplot(1,2,1)
            if m == 1
                line(CP(:,1), zeros(1,m), 'Marker', '+', 'MarkerEdgeColor','b' );
                hold on
                line(x(:,1), zeros(1,m), 'LineStyle','none', 'Marker', '.', 'MarkerEdgeColor','r' );
            elseif m == 2
                line(CP(:,1),CP(:,2), 'Marker', '+', 'MarkerEdgeColor','b' );
                hold on
                line(x(:,1),x(:,2),'LineStyle','none', 'Marker', '.', 'MarkerEdgeColor','r' );
            elseif m == 3
                line(CP(:,1),CP(:,2),CP(:,3), 'Marker', '+', 'MarkerEdgeColor','b' );
                hold on
                line(x(:,1),x(:,2),x(:,3),'LineStyle','none', 'Marker', '.', 'MarkerEdgeColor','r' );
                cosmetics(3)
            end
            hold off
        
            subplot(1,2,2)
            plot(v)
    end