function drawBoard(x0,y0,L,h,rwa)
% drawBoard  Draw the profile of a skateboard
%
% Synopsis:  drawBoard
%            drawBoard(x0)
%            drawBoard(x0,y0)
%            drawBoard(x0,y0,L)
%            drawBoard(x0,y0,L,h)
%            drawBoard(x0,y0,L,h,rwa)
%
% Input:  x0, y0 = Location of the (x,y) origin of the image
%                  Default: x0 = 0, y0 = 0
%         L = length of the board.  Default: L = 80 cm
%             All x-direction coordinates of the skateboard and
%             truck profile are scaled by L before drawing
%         h = length of the board.  Default: h = 3.8 cm
%             All y-direction coordinates of the skateboard and
%             truck profile are scaled by h before drawing
%         rwa = absolute size of the wheels.  Default: rwa = 2.25 cm
% 
% Output:  Profile of the skateboard in the current figure window.

if nargin<1, x0=0;      end
if nargin<2, y0=0;      end
if nargin<3, L=80;      end   %  Absolute length, cm
if nargin<4, h=3.8;     end   %  Absolute height, cm
if nargin<5, rwa=2.25;  end   %  Absolute wheel radius, cm

% -- Save the hold state so that it can be restored at the end of function
startHold = ishold;

% -- Get normalized (x,y) coordinates of the board and one truck
[xbn,ybn] = dataBoard;
[xtn,ytn] = dataTruck;

% -- Store absolute dimensions
xwa = [0.23, 0.77]*L;
hwa = 3.5;             % vertical distance from board to wheel center, cm
dyb = hwa + rwa;       % vertical distance from ground to board, cm

% -- Add the truck
gray = [0.7 0.7 0.7];
xtn = xtn - 0.02;
fill(x0 + xtn*L, y0 + dyb + ytn*hwa, gray);
axis('equal');    %  Set x and y scales to be equal

% -- Set "hold('on')" unless it was already 'on'.  That way, additional
%    graphical components are added to the plot, not replacing the plot
if ~startHold, hold('on');  end

% -- Add second truck
fill(x0 + (1-xtn)*L, y0 + dyb + ytn*hwa, gray);

% -- Add filled circles for the wheels
fillCircle(rwa, x0+xwa(1), y0+rwa, [1,1,1]);
fillCircle(rwa, x0+xwa(2), y0+rwa, [1,1,1]);

% -- Compute absolute size and position of the board, and plot it
plot(x0+xbn*L, y0+dyb+ybn*h,'k-','Linewidth',3.5)

% -- Restore the hold state
if ~startHold, hold('off');  end

end

% --------------------------
function [x,y] = dataBoard
% dataBoard  Dimensionless (x,y) coordinates of a skateboard outline
%
% Input: none
%
% Output:  x,y = normalized coordinates of the board profile in (x,y) plane
%                Normalized means 0 <= x <= 1, 0 <= y <= 1.  Get physical
%                dimensions by multiplying x by physical length and
%                multiplying y by the physical height of the board
%
%          x = 0 at the nose
%          y = 0 at plane of the skateboard

% -- Store board dimensions.  x and y are normalized coordinates
%    of the board: 0 <= x <= 1, 0 <= y <= 1.
x = [0.0000, 0.0289, 0.1010, 0.1363, 0.1431, 0.1491, ...
       0.2875, 0.3500, 0.4500, 0.5500, 0.6500, 0.7125, ...
       0.8509, 0.8569, 0.8637, 0.8990, 0.9711, 1.0000];
     
y = [1.0000, 0.7788, 0.2540, 0.0447, 0.0152, 0.0000, ...
       0.0000, 0.0263, 0.0526, 0.0526, 0.0263, 0.0000, ...
       0.0000, 0.0152, 0.0447, 0.2540, 0.7788, 1.0000];   
end

% --------------------------
function [x,y] = dataTruck
% dataTruck  Dimensionless (x,y) coordinates of a skateboard truck outline
%
% Input: none
%
% Output:  x,y = normalized coordinates of the profile of a single truck
%                in (x,y) plane.  
%                Normalized means 0 <= x <= 1, 0 <= y <= 1.  Get physical
%                dimensions by multiplying x by physical length and
%                multiplying y by the physical height of the board
%
%          x = 0 at the nose
%          y = 0 at plane of the skateboard

x = [ 0.1840, 0.1837, 0.1847, 0.1856, 0.1962, 0.1979, 0.2012, ...
      0.2078, 0.2090, 0.2108, 0.2316, 0.2829, 0.2879, 0.2940, ...
      0.2935, 0.2907, 0.2893, 0.2945, 0.2945, 0.2931, 0.2907, ...
      0.2945, 0.3067, 0.3088, 0.3091];

y = [ 0.0000,-0.0748,-0.0858,-0.0880,-0.3234,-0.3322,-0.3366, ...
     -0.3190,-0.3267,-0.4092,-0.9064,-0.8503,-0.7799,-0.7722, ...
     -0.6534,-0.6347,-0.4796,-0.4741,-0.4510,-0.4279,-0.4169, ...
     -0.4147,-0.1188,-0.0572, 0.0000];
end