function[XY] = calculateRectangleVertices(m, sample_point, H, W)

%this code finds the 4 points, orthogonal to the transect which then define
%a sample area 
% m is the slope of the line
% sample_point is the point of interest
% H is the length of the box - the short side of the transect 
% W is the  LONG side of the transect
% eg: a 4x4 box, H=4
% outputs XY which is a matrix of the four corners.

%{
% ************************begin code for testing
%clc
%clear
load m_perp
load lidarSampleLoc_temp
%m=1.45;
%sample_point2=[20 40];
%sample_point=[30 40];
sample_point=[lidarSampleLoc_temp(1,6) lidarSampleLoc_temp(1,7)];
m=m_perp(1,1);
H=sampleSquare;
W=sampleLength;

%{
%slope=((sample_point(2)-sample_point2(2))/ (sample_point(1)-sample_point2(1)) );
if sample_point2(1) == sample_point(1)
   m=NaN;
else 
    m=slope;
end
%}
%m=0;
%H=5; %short side
%W=10;

%}
% the slope of the line perpendicular to the stream
%m = -.9;

%convert Length of box to feet
H=H*3.2808399; %short side
W=W*3.2808399; %long side



% individual coordinates in x and y of sample point
x0 = sample_point(1,1);
y0 = sample_point(1,2);


 % slope converted to degrees
    theta1 = (((atan(m(1)))*180/pi) +90);
    % this is the sample point on line perpendicular from stream
    %sample_point = [0 0];

    % hyp. of triangle where H is length box side
  %  h = 0.7071*H;
    h = (sqrt(((W/2)^2)+((H/2)^2))/2);
    %.7071 is half of 1.4 which using the pythag therum for a triangle
    % with sides of length 1 and 1, you get a hypotenuse of 1.4 / 2 = .7
    % you divide by 2 because...it's half the length because the starting
    % point is in the CENTER of the square...

%make sure the slope is not 0 or NaN (if it's 0 that means the y values are
%equal and the line is horizontal. if it's NaN that means the x values are
%equal and the line is perfectly vertical

% 1= the line is moving either east or north, 0 is west or south

if m(1) == 0
    % equations for calculating vertex points of sample box
    % point 1 (+,+); point 2 (+,-); point 3 (-,+); point 4 (-,-)
    x1 = x0 + (H/2);
    y1 = y0 + (W/2);

    x2 = x0 + (H/2);
    y2 = y0 - (W/2);

    x3 = x0 - (H/2);
    y3 = y0 + (W/2);

    x4 = x0 - (H/2);
    y4 = y0 - (W/2);
   
elseif  isnan(m(1))
   % equations for calculating vertex points of sample box
    % point 1 (+,+); point 2 (+,-); point 3 (-,+); point 4 (-,-)
    x1 = x0 + (W/2);
    y1 = y0 + (H/2);

    x2 = x0 + (W/2);
    y2 = y0 - (H/2);

    x3 = x0 - (W/2);
    y3 = y0 + (H/2);

    x4 = x0 - (W/2);
    y4 = y0 - (H/2); 
    
    
    
else 
   
%{
    % equations for calculating vertex points of sample box
    % point 1 (+,+); point 2 (+,-); point 3 (-,+); point 4 (-,-)
    x1 = x0 + h*cos((45+theta)*pi/180); 
    y1 = y0 + h*sin((45+theta)*pi/180);
   % draw out diamon picture... 
    %theta -  theta = ((atan(m(1)))*180/pi); converts a slope into degrees
    %h=multiplier -- 
    %pi/180 - conversion of the angle factor (to change from degrees to radians)
%sin = opp / hypotunuse - then hyp * sin = opp
    x2 = x0 + h*sin((45+theta)*pi/180);
    y2 = y0 - h*cos((45+theta)*pi/180);

    x3 = x0 - h*sin((45+theta)*pi/180);
    y3 = y0 + h*cos((45+theta)*pi/180);

    x4 = x0 - h*cos((45+theta)*pi/180);
    y4 = y0 - h*sin((45+theta)*pi/180);
    
    
    elseif m(1)> 0
    
    %}
    
%theta = slope converted into degrees then converted into radians??
theta = theta1/180*pi;

x01=x0-(W/2); %-W which is the long side
y01=y0-(H/2); %-H which is the short side
x02=x0+(W/2);
y02=y0-(H/2);
x03=x0+(W/2);
y03=y0+(H/2);
x04=x0-(W/2);
y04=y0+(H/2);

    x1 = (x0 +(x01-x0)*cos(theta)-(y01-y0)*sin(theta));
    y1=  (y0 +(x01-x0)*sin(theta)+(y01-y0)*cos(theta));
    
    x2 = (x0 +(x02-x0)*cos(theta)-(y02-y0)*sin(theta));
    y2=  (y0 +(x02-x0)*sin(theta)+(y02-y0)*cos(theta));
    
    x3 = (x0 +(x03-x0)*cos(theta)-(y03-y0)*sin(theta));
    y3=  (y0 +(x03-x0)*sin(theta)+(y03-y0)*cos(theta));
    
    x4 = (x0 +(x04-x0)*cos(theta)-(y04-y0)*sin(theta));
    y4=  (y0 +(x04-x0)*sin(theta)+(y04-y0)*cos(theta));

end

%{
x = 10
y = 20
w = 3
h = 2
x1=x-w
y1=y-h
x2=x+w
y2=y-h
x3=x+w
y3=y+h
x4=x-w
y4=y+h
polygon(c(x1,x2,x3,x4),c(y1,y2,y3,y4),border="blue")

##Rotate it at (10,20) by 45 degree
theta = 45/180*pi
x.rotated = c(10 + (x1-10)*cos(theta)-(y1-20)*sin(theta),
                10 + (x2-10)*cos(theta)-(y2-20)*sin(theta),
                10 + (x3-10)*cos(theta)-(y3-20)*sin(theta),
                10 + (x4-10)*cos(theta)-(y4-20)*sin(theta))
y.rotated = c(20 + (x1-10)*sin(theta)+(y1-20)*cos(theta),
                20 + (x2-10)*sin(theta)+(y2-20)*cos(theta),
                20 + (x3-10)*sin(theta)+(y3-20)*cos(theta),
                20 + (x4-10)*sin(theta)+(y4-20)*cos(theta))
              %}

XY = [x1 y1 x2 y2 x3 y3 x4 y4];


%{

dist1m = (sqrt((x1-x2)^2+(y1-y2)^2)*0.304801);
dist2m = (sqrt((x1-x3)^2+(y1-y3)^2)*0.304801);
dist3m = (sqrt((x3-x4)^2+(y3-y4)^2)*0.304801);


x=[x1,x2,x3,x4,x1];
y=[y1,y2,y3,y4,y1];
figure (3)
plot (x1,y1, 'm*','MarkerSize',30), hold on
plot (x3,y3, 'm*','MarkerSize',30), hold on

%sp(1),sp(2),
plot (sample_point(1),sample_point(2),'ro'), hold on
%plot (sample_point2(1),sample_point2(2),'ko'), hold on
plot(x,y,'--rs')
%plot([x1,y1],[x3,y3],'--gs'), hold on
%plot([x2,y2],[x3,y3],'rs'), hold on
%plot([x3,y3],[x4,y4],'--ks'), hold on
axis equal
% axis([-10 10 -10 10])
%}