function [xc,yc] = camprofile(Ro,y,yp,theta,dir)
%
% FUNCTION [xc,yc] = camprofile(Ro,y,yp,theta,dir_flag)
%
% This function finds the cam profile for a disk cam with an reciprocating
% flat-face follower.  It accepts the base circle radius Ro, vectors of y
% and yp (all in displacement units), and cam angle theta (RAD).  These
% vectors should all correspond to a full cycle of cam rotation from 0 to
% 2*PI.  Obviously the smaller the stepsize the more accurate and smoother
% the final plot of cam profile.
%
% Vectors xc and yc containing the coordinates of the cam profile are
% returned.  The cam profile points are generated so the follower axis is
% vertical (i.e. pi/2 has been added to follower angle).
%
% NOTE: The "dir" flag is either 'CW' or 'CCW' to denote the direction of
% cam rotation.  The algorithm is written for 'CW' rotation, but if the
% direction is 'CCW' all that must be done is to change the sign of the x
% coordinates of the cam profile.

N = length(theta);  % Find number of points in motion
xc = zeros(N,1);
yc = zeros(N,1);

for i = 1:N
    xc(i) = (Ro+y(i))*cos(theta(i)+pi/2)+yp(i)*cos(theta(i)+pi);
    yc(i) = (Ro+y(i))*sin(theta(i)+pi/2)+yp(i)*sin(theta(i)+pi);
end

% Finally, the above equations were for CW rotation.  If the cam rotates
% CCW, all we have to do is negate the xc vector.

if strcmp(dir,'CCW')
    xc = -xc;   % Flip the profile around the Y axis for CCW rotation
end