function[samplePoints] = calculatePointsAlongLine_Reverse (startPointPos, startPointNeg, sampleNumPos, sampleNumNeg,slope, samplingDistance,StreamNumber,transectNumber,edgeEffect,sampleSquare)


%inputs = slope of (perpendicular to stream) line [m_perp] and slope in
%radians (alpha_perp)
%edgeEffect is the amount of buffer from the edge working in to account for
%canopy overhang... this may be variable from location to location however
%based upon crude measurements using imagery this may be sufficient...

%-----------------------BEGIN turn this on if i want to test as a non function 
%{
clear
clc
samplingDistance=5;
numberOfSamples=2; % number of samples to be taken along the transect in each direction
load m_perp
slope=m_perp(1); %slope of the line
slope=NaN;
load bufferEdgeLocNeg 
load bufferEdgeLocPos 
startPointPos=bufferEdgeLocPos;
startPointNeg=bufferEdgeLocNeg;
sampleNumPos=3;
sampleNumNeg=0;
StreamNumber=4;
transectNumber=3; 
type=1; 

%1=sample from the stream out to the edge
%2 = sample from the edge back towards the stream
%}
%-----------------------END turn this on if i want to test as a non function 

perp_seg=samplingDistance; %distance b/w points along line

%move first box up to account for the size of the box, also account for the
%edgeEffect
boxSizeOffset=(sampleSquare/2);
halfDistance=(boxSizeOffset+edgeEffect)*3.2808399;
edgeOffset=(edgeEffect-boxSizeOffset)*3.2808399;

% convert to radians
%alpha_perp(j) = atan(m_perp(j)); 
slopeRadians = atan(slope);
i=0;

% now it is skipping the point right after the starting point... and also
% remember it will calculate from the point center so i'll need to offset
% it sampling distance / 2 

% in the positive direction
% this was set to 0 but really it should be if it is greater than...

    if sampleNumPos >0
        
    for i = 1:sampleNumPos 
       
    if i==1
        distanceOut=0; 
        %offset first point to fix square sampling box within buffer
        %boundary, first point is the edge of the buffer - assign it then
        %calculate the rest (hence the i-1 below
        
        %need to add if slope ==0 or is NaN
        if (slope > 0),     
            px_perp_pos = startPointPos(1) - (halfDistance)*cos(slopeRadians);
            py_perp_pos = startPointPos(2) - (halfDistance)*sin(slopeRadians);
            
            edge_px_pos = startPointPos(1) - edgeOffset*cos(slopeRadians);
            edge_py_pos = startPointPos(2) - edgeOffset*sin(slopeRadians);
        
        elseif isnan(slope)
            %this means that slope = x/0 = nan (only vertical)
        %however because thsi is the slope of the original stream line i
        %should adjust the X values because the transect perpendicular to the stream will run
        %horizontal
   
            px_perp_pos = startPointPos(1) - (halfDistance);% - (halfDistance)*cos(slopeRadians);
            py_perp_pos = startPointPos(2) ; %*sin(slopeRadians);
            
            edge_px_pos = startPointPos(1) - (edgeOffset);
            edge_py_pos = startPointPos(2) ; 
        
        elseif slope == 0    
            px_perp_pos = startPointPos(1);
            py_perp_pos = startPointPos(2) - (halfDistance);% - (halfDistance); %*sin(slopeRadians);
            
            edge_px_pos = startPointPos(1);
            edge_py_pos = startPointPos(2) - (edgeOffset);
            
            
        else
            px_perp_pos = startPointPos(1) + (halfDistance)*cos(slopeRadians);
            py_perp_pos = startPointPos(2) + (halfDistance)*sin(slopeRadians);
            
            edge_px_pos = startPointPos(1) + (edgeOffset)*cos(slopeRadians);
            edge_py_pos = startPointPos(2) + (edgeOffset)*sin(slopeRadians);
        end
        
        %pn_perp_pos(i,:) = [StreamNumber transectNumber 1 distanceOut px_perp_pos(1) py_perp_pos(2)];
    else
            
        % this code is for sampling from the edge of the buffer back towards
        % the stream (to calculate edge effects
        %need to add if slope ==0 or is NaN
        
        if(slope > 0),
        px_perp_pos = startPointPos(1) - ((perp_seg*(i-1)*3.2808399)+halfDistance)*cos(slopeRadians);
        py_perp_pos = startPointPos(2) - ((perp_seg*(i-1)*3.2808399)+halfDistance)*sin(slopeRadians);
        
        elseif(slope == 0),
        px_perp_pos = startPointPos(1);
        py_perp_pos = startPointPos(2) - ((perp_seg*(i-1)*3.2808399)+halfDistance);

        elseif isnan(slope)
        px_perp_pos = startPointPos(1) - ((perp_seg*(i-1)*3.2808399)+halfDistance);
        py_perp_pos = startPointPos(2);

        else
        px_perp_pos = startPointPos(1) + ((perp_seg*(i-1)*3.2808399)+halfDistance)*cos(slopeRadians);
        py_perp_pos = startPointPos(2) + ((perp_seg*(i-1)*3.2808399)+halfDistance)*sin(slopeRadians);
            
        end       
            distanceOut=((i-1)*samplingDistance);    
    end
        %add total buffer width to this to facilitate...
        %(Startingpointpos(2)
         pn_perp_pos(i,:) = [StreamNumber transectNumber 1 startPointPos(3) distanceOut px_perp_pos py_perp_pos];
         edge_pos(1,:)= [StreamNumber transectNumber 1 startPointPos(3) 999 edge_px_pos edge_py_pos];
        end
    
    else
      distanceOut=0;
      px_perp_pos= startPointPos(1);
      py_perp_pos= startPointPos(2);
      pn_perp_pos(1,:)= [StreamNumber transectNumber 1 startPointPos(3) distanceOut px_perp_pos py_perp_pos];
      
      edge_px_pos= startPointPos(1);
      edge_py_pos= startPointPos(2);
      edge_pos(1,:)=[StreamNumber transectNumber 1 startPointPos(3) 999 edge_px_pos edge_py_pos];
    end

    % NEED TO ADD EDGE VALUES TO THIS SECTION --------------------->
    
    if sampleNumNeg >0
    for i = 1:sampleNumNeg       
         if i==1
           distanceOut2=0;
           
           if (slope>0)
           px_perp_neg = startPointNeg(1) + (halfDistance)*cos(slopeRadians);
           py_perp_neg = startPointNeg(2) + (halfDistance)*sin(slopeRadians);
           
           edge_px_neg = startPointNeg(1) + (edgeOffset)*cos(slopeRadians);
           edge_py_neg = startPointNeg(2) + (edgeOffset)*sin(slopeRadians);
           
           elseif slope==0
           px_perp_neg = startPointNeg(1);
           py_perp_neg = startPointNeg(2) + (halfDistance);
           
           edge_px_neg = startPointNeg(1);
           edge_py_neg = startPointNeg(2) + (edgeOffset);
               
           elseif isnan(slope)
           px_perp_neg = startPointNeg(1) + (halfDistance);
           py_perp_neg = startPointNeg(2);
           
           edge_px_neg = startPointNeg(1) + (edgeOffset);
           edge_py_neg = startPointNeg(2);
           
           else    
           px_perp_neg = startPointNeg(1) - (halfDistance)*cos(slopeRadians);
           py_perp_neg = startPointNeg(2) - (halfDistance)*sin(slopeRadians);
           
           edge_px_neg = startPointNeg(1) - (edgeOffset)*cos(slopeRadians);
           edge_py_neg = startPointNeg(2) - (edgeOffset)*sin(slopeRadians);
           end
           
          % pn_perp_neg(i,:) = [StreamNumber transectNumber 0 distanceOut2 startPointNeg(1) startPointNeg(2)];
         else
        % (slope >0) in the negative direction
        
        if(slope>0)
        px_perp_neg = startPointNeg(1) + ((perp_seg*(i-1)*3.2808399)+halfDistance)*cos(slopeRadians);
        py_perp_neg = startPointNeg(2) + ((perp_seg*(i-1)*3.2808399)+halfDistance)*sin(slopeRadians);

        elseif slope==0
            px_perp_neg = startPointNeg(1);
            py_perp_neg = startPointNeg(2) + ((perp_seg*(i-1)*3.2808399)+halfDistance);
            
        elseif isnan(slope)
            px_perp_neg = startPointNeg(1) + ((perp_seg*(i-1)*3.2808399)+halfDistance);
            py_perp_neg = startPointNeg(2);
            
        else
        px_perp_neg = startPointNeg(1) - ((perp_seg*(i-1)*3.2808399)+halfDistance)*cos(slopeRadians);
        py_perp_neg = startPointNeg(2) - ((perp_seg*(i-1)*3.2808399)+halfDistance)*sin(slopeRadians);

        end

            distanceOut2=((i-1)*samplingDistance);        
        end
        pn_perp_neg(i,:) = [StreamNumber transectNumber 0 startPointNeg(3) distanceOut2 px_perp_neg py_perp_neg];
        edge_neg(1,:) = [StreamNumber transectNumber 0 startPointNeg(3) 999 edge_px_neg edge_py_neg];
    end
    %distanceOut2=(i*samplingDistance);
    %pn_perp_neg(i,:) = [StreamNumber transectNumber 0 distanceOut2 px_perp_neg py_perp_neg];
    else
            distanceOut2=0;
            px_perp_neg=startPointNeg(1);
            py_perp_neg=startPointNeg(2);
            
            edge_px_neg=startPointNeg(1);
            edge_py_neg=startPointNeg(2);
            
            pn_perp_neg(1,:) = [StreamNumber transectNumber 0 startPointNeg(3) distanceOut2 px_perp_neg py_perp_neg];
            edge_neg(1,:) = [StreamNumber transectNumber 0 startPointNeg(3) 999 edge_px_neg edge_py_neg];

    end
    %output StreamNumber transectNumber NorthOrSouthofStream
    %SamplingDistanceFromEdge X Y
    
samplePoints=[pn_perp_pos; pn_perp_neg;edge_pos;edge_neg];
%pn_perp_neg; %removed for testing
% coordinate of point = seg_dist
%1=positive
%0=negative


% ********************BEGIN turn off plot (for testing only
%{

%

%plot out everything to see how this works

plot(pn_perp_pos(1:end,6),pn_perp_pos(1:end,7),'rx'), hold on
plot(pn_perp_neg(1:end,6),pn_perp_neg(1:end,7),'b*'), hold on
plot(startPointPos(1),startPointPos(2),'g*'), hold on
plot(startPointNeg(1),startPointNeg(2),'g*'), hold on
%plot()
axis equal
%}
