%CLOSEDCYLINDER  Closed Cylinder
% Generates a cylinder bewteen pt1 and pt2
%
% Syntax:  [X, Y, Z] = closedCylinder(pt1, pt2, radius)
%
% Inputs:
%    pt1 - start point of cylinder
%    pt2 - end point of cylinder
%    radius - cylinder radius
%
% Outputs:
%    [X, Y, Z] - matrices defining a parametric surface
%
% Other m-files required: transSurf.m, trans.m, roty.m, rotz.m
%
% See also: TRANSSURF

% Author: Travis Hydzik
% Last revision: 19 October 2004

function [X, Y, Z] = closedCylinder(pt1, pt2, radius)

    v = pt2 - pt1;
    
	[theta,phi,R] = cart2sph(v(1), v(2), v(3));
    
	radius = [0 radius radius 0];  
	
	[X,Y,Z] = cylinder(radius, 20); 
	
	% first and last rows respectively to give squared ends
	rows = size(Z,1);
	Z(2,:) = Z(1,:);                    
	Z(rows-1,:) = Z(rows,:);
	
	Z = Z * R;    % scale to length
	
	T = trans(pt1(1), pt1(2), pt1(3))*rotz(theta)*roty(pi/2 - phi);
	[X, Y, Z] = transSurf(T,X,Y,Z);