function url = plotLineDrawing(points,graphicsType,dirName,fileName)
%plotLineDrawing   plot points as a line drawing on the web
%
% consumes: 
%   points, a matrix of points to plot 
%                   each column is one point (x,y)
%                   the first row is all the x values
%                   the second row is all the y values
%
%                   if there are more then 2 rows, the rest of the
%                   rows are ignored; if fewer, an empty matrix is returned
%
%                   if a column is repeated, it means pick up the pen
%
%   graphicsType: one of the following '-djpeg', '-dpng'
%                 (or other legal arg to the function print())
%
%   dirName: directory name, relative to ~/public_html
%   fileName: a filename in which to store the drawing
%
% produces:
%   url: the url at which the graphic can be found, or
%        an empty matrix if the graphic cannot be produced.
%  
% Example: Plot the capital letter A in the box with 0,0 at lower
%          left and 4,8 at upper right.
%
%  >> points = [ 0 0 ; 2 8 ; 4 0 ; 4 0 ; 1 4; 3 4 ]';
%  >> plotLineDrawing(points,'-djpeg','cisc106/10.18','letterA.jpg') 
%  ans = 
%
%    http://copland.udel.edu/~pconrad/cisc106/10.18/letterA.jpg
%
%  >> 
% P. Conrad   for CISC106, 10/16/2007

  result = [];

  [rows, cols] = size(points);

  % make sure there are at least two rows.  Ignore any extra rows.

  if (rows < 2)
    return;
  end

  % Throw away all rows except rows 1 and 2

  points = points(1:2,:);

  % Get ready to plot by closing the current graphics figure

  close gcf;

  % Start the first line as an empty matrix.
  % We'll accumulate points until we
  % find a duplicate row, or we run out of rows.  Each time that
  % happens, we'll plot these points and start a new line.
  
  thisLinesPoints = [];

  % We have to keep track of the previous column to make it possible
  % to detect duplicates.  We'll start out with an empty prevCol
  % because there isn't one the first time around.
  
  prevCol = [];
  
  % Process all the columns

  for i=1:cols;

    thisCol = points(:,i);
    
    % if there _is_ a previous column, and it matches this one...
    % note that "short circuit" evaluation of the && is used 

    if ( ~isempty(prevCol) && ...
	 thisCol(1)==prevCol(1) && thisCol(2)==prevCol(2) )

      % we have a match, so plot all the points up until now
      % the x value are  all rows, the first column
      % the y values are all rows, the second column
      % after you plot, keep the hold on so we can 
      %  plot another line on the same figure.

      plot(thisLinesPoints(1,:),thisLinesPoints(2,:));
      hold on;

      % now start over again accumulating points for the next line

      thisLinesPoints = [];
      prevCol = [];

    else 
      
      % row does not match
      % so add it to the list of points to be plotted for this line
      
      thisLinesPoints = [ thisLinesPoints thisCol ];
      
      % prepare for next time thru loop 
      % by copying thisCol into prevCol

      prevCol = thisCol;  

    end  % checking whether there is a match

  end
  
  % if there are still points left, plot them

  if (~isempty(thisLinesPoints))
      plot(thisLinesPoints(1,:),thisLinesPoints(2,:));
      hold on;
  end;

  % Make the aspect ratio equal, and the plot axes tight
  
  axis equal tight;  

  % Now create a directory for the plot if needed

  fullDirectoryName = ['~/public_html/' dirName];
  fullFileName = [fullDirectoryName '/' fileName];
  system(['mkdir -p ' fullDirectoryName]);

  % print the figure to the file and make file readable on the web
  
  print(graphicsType,fullFileName);
  system(['chmod -R 755 ' fullDirectoryName]);
      
  % get the userid to use in the URL

  [status, userid] = system('whoami');
  userid = strtrim(userid);

  % compute the URL

  urlPrefix = ['http://copland.udel.edu/~' userid];
  url = strrep(fullFileName,'~/public_html',urlPrefix);

end % plotLineDrawing.m
