function[samplePoints] = calculatePointsAlongLine_Reverse_TransectCenter (startPointPos, startPointNeg, sampleNumPos, sampleNumNeg,slope, samplingDistPos,samplingDistNeg,StreamNumber,transectNumber,edgeEffect,sampleSquare)

%{
%this code calculate interior and edge points points starting from the end of the buffer
%it is based upon calculate points along line reverse however it only
%calculates the center point of each transect so it is direction dependent.
Nov 1 - Added fring calculations - ie boxes that are right at the edge of
the buffer / stream (eliminating the 3m cutoff i used for the edge
previously)

%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
clear pn_perp_neg %if i've previuosly run the entire code these need to be cleared as they're also used in the code
clear pn_perp_pos
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=-1.2;
%load bufferEdgeLocNeg 
%load bufferEdgeLocPos 
%load transCenterDistPos 
%load transCenterDistNeg
samplingDistPos=10;
samplingDistNeg=10;
edgeEffect=3;
sampleSquare=5;
%startPointPos=bufferEdgeLocPos;
%startPointNeg=bufferEdgeLocNeg;
startPointPos=[800 900 0];

startPointNeg=[800 400 0];
sampleNumPos=3;
sampleNumNeg=1;
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_segPos=samplingDistPos; %distance to center point (meters)
perp_segNeg=samplingDistNeg; %distance to center point in the neg direction

%move first box up to account for the size of the box, also account for the
%edgeEffect
edgeDistance=(edgeEffect)*3.2808399; 
halfSquare=(sampleSquare/2)*3.2808399;
fullTransectLengthPos=(perp_segPos*2)*3.2808399;
fullTransectLengthNeg=(perp_segNeg*2)*3.2808399;

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

%Offset for Center (interior): (perp_segPos*3.2808399)+edgeDistance)
%offset for edgeValue:  ((sampleSquare/2)*3.280839)
%offset for streamEdge : ((perp_segPos*2))-edgeDistance)

interiorPosOffset=(perp_segPos*3.2808399);
interiorNegOffset=(perp_segNeg*3.2808399);
edgeLandOffset=(halfSquare+edgeDistance); %this should be the same in both pos and neg directions

%edge stream offset = half of the transect width*2 (entire transect width)
%minus half of hte sample size 
edgePosStreamOffset=(fullTransectLengthPos-(edgeDistance+halfSquare));
edgeNegStreamOffset=(fullTransectLengthNeg-(edgeDistance+halfSquare));

edgeAnthroFringeOffset=(edgeDistance-halfSquare);

edgePosStreamFringeOffset=(fullTransectLengthPos-(edgeDistance-halfSquare));

edgeNegStreamFringeOffset=(fullTransectLengthNeg-(edgeDistance-halfSquare));


% remember that perp_segPos is 1/2 the transect length
% 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...

% THis will loop through 3 times to calculate Edge, Interior and streamEdge values  
%will add the two far edges to this as well...
for i = 1:5
    
    %1=interior, 2=bufferedge (landuse) 3= stream edge(stream) 4 anthro
    %fringe 5 streamFringe
    %define offsets for each iteration
    if i ==1
        offsetPos=interiorPosOffset;
        offsetNeg=interiorNegOffset;
    elseif i==2
        offsetPos=edgeLandOffset;
        offsetNeg=edgeLandOffset;
    elseif i==3
        offsetPos=edgePosStreamOffset;
        offsetNeg=edgeNegStreamOffset;
    elseif i==4
        offsetPos=edgeAnthroFringeOffset; 
        offsetNeg=edgeAnthroFringeOffset;
    else
        offsetPos=edgePosStreamFringeOffset;  
        offsetNeg=edgeNegStreamFringeOffset;
    end
    
        if (perp_segPos*2)>24 % if the buffer is wider than 24m
            
            if(slope > 0),
            px_perp_pos = startPointPos(1) - offsetPos*cos(slopeRadians);
            py_perp_pos = startPointPos(2) - offsetPos*sin(slopeRadians);

            elseif(slope == 0),
            px_perp_pos = startPointPos(1);
            py_perp_pos = startPointPos(2) - offsetPos;

            elseif isnan(slope)
            px_perp_pos = startPointPos(1) - offsetPos;
            py_perp_pos = startPointPos(2);

            else
            px_perp_pos = startPointPos(1) + offsetPos*cos(slopeRadians);
            py_perp_pos = startPointPos(2) + offsetPos*sin(slopeRadians);
            end 
            
           
        else % assign the xy values to be the starting point...
           px_perp_pos= startPointPos(1);
           py_perp_pos= startPointPos(2);
            
        end
        
        %write values to the matrix
        pn_perp_pos(i,:)=[StreamNumber transectNumber 1 startPointPos(3) i px_perp_pos py_perp_pos ];
    
        %in the negative direction...
        if (perp_segNeg*2)>24
            
            if(slope>0)
            px_perp_neg = startPointNeg(1) + offsetNeg*cos(slopeRadians);
            py_perp_neg = startPointNeg(2) + offsetNeg*sin(slopeRadians);

            elseif slope==0
                px_perp_neg = startPointNeg(1);
                py_perp_neg = startPointNeg(2) + offsetNeg;

            elseif isnan(slope)
                px_perp_neg = startPointNeg(1) + offsetNeg;
                py_perp_neg = startPointNeg(2);

            else
                px_perp_neg = startPointNeg(1) - offsetNeg*cos(slopeRadians);
                py_perp_neg = startPointNeg(2) - offsetNeg*sin(slopeRadians);
            end
            
             pn_perp_neg(i,:) = [StreamNumber transectNumber 0 startPointNeg(3) i px_perp_neg py_perp_neg];
        else
           px_perp_neg=startPointNeg(1) ;
           py_perp_neg=startPointNeg(2) ;
        end
           %output StreamNumber transectNumber NorthOrSouthofStream  XXX
           %location (interior, edge etc) and x and y values
            pn_perp_neg(i,:) = [StreamNumber transectNumber 0 startPointNeg(3) i px_perp_neg py_perp_neg];

    
    
    %SamplingDistanceFromEdge X Y

end

samplePoints=[pn_perp_pos; pn_perp_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,6),pn_perp_pos(1,7),'b*'), hold on
plot(pn_perp_pos(2,6),pn_perp_pos(2,7),'y*'), hold on
plot(pn_perp_pos(3,6),pn_perp_pos(3,7),'r*'), hold on
plot(pn_perp_pos(4,6),pn_perp_pos(4,7),'k*'), hold on
plot(pn_perp_pos(5,6),pn_perp_pos(5,7),'m*'), hold on

%
plot(pn_perp_neg(1,6),pn_perp_neg(1,7),'b*'), hold on
plot(pn_perp_neg(2,6),pn_perp_neg(2,7),'y*'), hold on
plot(pn_perp_neg(3,6),pn_perp_neg(3,7),'r*'), hold on
plot(pn_perp_neg(4,6),pn_perp_neg(4,7),'k*'), hold on
plot(pn_perp_neg(5,6),pn_perp_neg(5,7),'m*'), hold on
%
plot(startPointPos(1),startPointPos(2),'go'), hold on
plot(startPointNeg(1),startPointNeg(2),'go'), hold on
%plot()

axis equal
%}
