function xy=adj_edpt(modindex,rawxy)

% function xy=adj_edpt(modindex,rawxy)
%
% This function is used in conjuction with FD3.  If the user clicks on [x, y]
% coordinate 'rawxy' and that click registers as occurring on the module
% designate by FD3MODULES(:,modindex), this program is used to adjust that
% [x, y] coordinate so that it lies exactly on the outer edge of the module.
% This adjusted coordinate is returned in 'xy'.  Note:  this program is only
% called when the user is creating horizontal/vertical streams, not when the
% user is creating single, straight line streams.
%
% ACP

% Last update:  July 7, 1993
% Author:       Adrian C. Penisson

global FD3SCALE FD3MODULES

% First, read in xdata and ydata for PRIMARY OBJECT.

xdata=get(FD3MODULES(1,modindex),'Xdata');
ydata=get(FD3MODULES(1,modindex),'Ydata');
len=length(xdata);

% Next, determine on which side of the module the user clicked.

side=get_side(modindex,rawxy);

if side==1 | side==3

   % FOLLOW THIS BRANCH IF THE USER CLICKED ON THE LEFT OR RIGHT SIDE OF THE
   % MODULE.  First, round the y coordinate so that it lies on the nearest
   % horizontal grid line.

   xy(1,2)=round(rawxy(1,2)/FD3SCALE)*FD3SCALE;

   % If the adjusted y coordinate is larger than the largest y coordinate of
   % the PRIMARY OBJECT, move down one grid line.  If the adjusted y
   % coordinate is less than the smallest y coordinate of the PRIMARY OBJECT,
   % move up one grid line.

   if xy(1,2)>=max(ydata)
      xy(1,2)=xy(1,2)-FD3SCALE;
   elseif xy(1,2)<=min(ydata)
      xy(1,2)=xy(1,2)+FD3SCALE;
   end

   % Determine all possible x coordinates

   pos=find(((ydata(1:len-1)<=xy(1,2))&(ydata(2:len)> xy(1,2)))| ...
            ((ydata(1:len-1)> xy(1,2))&(ydata(2:len)<=xy(1,2))));
   xmaybe=xdata(pos)+(xdata(pos+1)-xdata(pos)).*(xy(1,2)-ydata(pos))./ ...
      (ydata(pos+1)-ydata(pos));
 
   if side==1

      % If it is the right side, the x coordinate should be the maximum
      % possible x coordinate.

      xy(1,1)=max(xmaybe);

   else

      % If it is the left side, the x coordinate should be the minimum
      % possible x coordinate.

      xy(1,1)=min(xmaybe);

   end

else

   % FOLLOW THIS BRANCH IF THE USER CLICKED ON THE TOP OR BOTTOM SIDE OF THE
   % MODULE.  First, round the x coordinate so that it lies on the nearest
   % vertical grid line.

   xy(1,1)=round(rawxy(1,1)/FD3SCALE)*FD3SCALE;

   % If the adjusted x coordinate is larger than the largest x coordinate of
   % the PRIMARY OBJECT, move left one grid line.  If the adjusted x
   % coordinate is less than the smallest x coordinate of the PRIMARY OBJECT,
   % move right one grid line.

   if xy(1,1)>=max(xdata)
      xy(1,1)=xy(1,1)-FD3SCALE;
   elseif xy(1,1)<=min(xdata)
      xy(1,1)=xy(1,1)+FD3SCALE;
   end

   % Determine all possible y coordinates

   pos=find(((xdata(1:len-1)<=xy(1,1))&(xdata(2:len)> xy(1,1)))| ...
            ((xdata(1:len-1)> xy(1,1))&(xdata(2:len)<=xy(1,1))));
   ymaybe=ydata(pos)+(ydata(pos+1)-ydata(pos)).* (xy(1,1)-xdata(pos))./ ...
      (xdata(pos+1)-xdata(pos));

   if side==2

      % If it is the top side, the y coordinate should be the maximum
      % possible y coordinate.

      xy(1,2)=max(ymaybe);

   else

      % If it is the bottom side, the y coordinate should be the minimum
      % possible y coordinate.

      xy(1,2)=min(ymaybe);

   end

end
