# How can I calculate the length of the longest side of a rectangle and the slope of an imaginary rectangle parallel to the longest side of the first?

16 views (last 30 days)
Jules Ray on 17 Oct 2011
I need a suggestion for this easy but confusing dilemma.
I have a rectangle formed by 4 points (x2,y1) (x1,y2) (x3,y4) (x4,y3). The maximum and minimum are respectively (x4,y4) and (x1,y1).
I need to create a script to obtain the length of the longest side of the rectangle and the slope of an imaginary rectangle parallel to the longest side of the rectangle.
The problem is not so easy taking into account that the rectangle could be in many different positions. I have an idea about the slope, using Pythagoras and the maximum and minimum values, but...the length...I'm stuck on that.
##### 2 CommentsShowHide 1 older comment
Jules Ray on 17 Oct 2011
i have posted my script below... please lets take a look

the cyclist on 17 Oct 2011
I don't understand the part about the slope, but there is an easy way to find the longest side. The distance between each pair of points is
>> dij = sqrt((xi-xj)^2+(yi-yj)^2);
Calculate this for all pairs. You should get three unique length values, corresponding to:
• short side
• long side
• diagonal
The middling value will be the long side of the rectangle.
(You might need to be careful about floating point inexactitudes.)
Jules Ray on 17 Oct 2011
thanks about your suggestions.... but what's about the length do you have any idea? i want to apply the method proposed by the cyclist, using the major, middle and minor length.... sounds good....

Image Analyst on 19 Oct 2011
Sorry - I gave you a more extensive answer than the Cyclist but for some reason you (or someone) deleted your first posting of this. I gave you the slope, and told you how to find the various lengths. I'm not inclined to repeat my prior waste of time.
Jules Ray on 19 Oct 2011
i dont know what's happen but thanx anyway Image Analyst.....
this is my final result, that i want to share with everybody, works percfekt:
X=p(:,1);
Y=p(:,2);
%Define coordinates for each vertice
x1=p(1,1);
x2=p(2,1);
x3=p(3,1);
x4=p(4,1);
y1=p(1,2);
y2=p(2,2);
y3=p(3,2);
y4=p(4,2);
%minimum and maximum for the entire dataset
minx=min(X);
miny=min(Y);
maxx=max(X);
maxy=max(Y);
% SLOPE
%defines slope of an imaginary central line
m=((maxy-miny)/(maxx-minx)); %check, posible BUG
an=90-atan(m)*180/pi; %change the slope to degrees
theta=an*pi/180; %theta is the required input "STEP SUCESFULLY COMPLETED"
nim=numel(p);
% LENGHT
%Alternative 1, calculate all diagonals, the middle one should be the
%lenght
A=sqrt((x2-x1)^2+(y2-y1)^2);
B=sqrt((x3-x2)^2+(y3-y2)^2);
C=sqrt((x3-x4)^2+(y3-y4)^2);
D=sqrt((x4-x2)^2+(y4-y2)^2);
E=sqrt((x3-x1)^2+(y3-y1)^2);
F=sqrt((x4-x1)^2+(y4-y1)^2);
G=sqrt((x3-x1)^2+(y3-y1)^2);
V=[A B C D E F G];
W=sort(V);
S=W'; %max to min ordering of obtained results and transposing
H=round(S); %eliminate decimal places
minV=min(H); % find min value in H
H(H==minV)=0; % set all min values to zero
maxV=max(H); % find max value in H
H(H==maxV)=0; % set all max values to zero
%intermediate=H(H~=0); %matrix with non-zero values, max and min values eliminated
ind=find(H~=0, 1, 'first'); %finds the address of the first non-zero value
ddg=H(ind); %lenght of the rectangle "Requested answer"
clear X Y A B C D E F G V W x1 x2 x3 x4 y1 y2 y3 y4 ind minx miny maxx maxy maxV minV p m an S H nim