-3

Four Corner Vertices of a deformed rectangle are given i.e. p1(x1,y1), p2(x2,y2), p3(x3,y3) & p4(x4,y4).

How to find position of all co-ordinates i.e. which one is upper left, upper right, lower right and lower left?

I am working with MATLAB.

Clarification: The rectangle is not necessarily axis aligned. It may be slightly tilted.

erbal
  • 421
  • 5
  • 18
  • The upper left coordinate is the one with the biggest y value and the smallest x value, for example. This question doesn't have anything to do with Matlab in special. – Wauzl Oct 13 '15 at 07:44
  • I mentioned MATLAB thinking there may be special function available to do it. – erbal Oct 13 '15 at 07:56
  • is your rectangle axis aligned? ie edges parallel to x or y axis – gregswiss Oct 13 '15 at 08:16
  • @gregswiss: edges not surely parallel to any axes. parallel or slightly tilted also possible. – erbal Oct 13 '15 at 08:19
  • Are the given points the corner points, or are they anywhere on the rectangle? Are the angles always 90°? – hbaderts Oct 13 '15 at 08:30
  • 1
    Here's a NumPy implementation - http://stackoverflow.com/q/30088697/3293881 – Divakar Oct 13 '15 at 08:37
  • @hbaderts: The corner point are given. Angle is 90 degree or very close to it. – erbal Oct 13 '15 at 08:48
  • @hbaderts: Angles are always 90 degree between sides of a rectangle. But the rectangle (on which we are working) may be somewhat deformed, so we should not use right angle property for solution of this problem. – erbal Oct 13 '15 at 09:34
  • @user11659 I asked because if not axis aligned it's not necessary clear what mean upper left. – gregswiss Oct 13 '15 at 11:00

2 Answers2

1

You can use find to get the indices and use the &operator to compare if both boundaries are fulfilled. Valid for axis aligned rectangles.

xP = [1 2 2 1];
yP = [3 1 3 1];

[~,right] = max(xP);
[~,left]  = min(xP);
[~,up]    = max(xP);
[~,low]   = min(yP);


upperleft  = intersect(up,left);
lowerright = intersect(low,right);
JaBe
  • 664
  • 1
  • 8
  • 27
  • I was assuming an axis aligned rectangle. Otherwise, upperleft is not clearly defined. – JaBe Oct 13 '15 at 08:32
  • 1
    if the rectangle is not axis aligned then indeed the corner labels should be renamed `up/down/left/right` but finding which is what is even more straightforward: `[~ up] = max(Yp)`, `[~ down] = min(Yp)` ... etc – Hoki Oct 13 '15 at 08:42
  • @Hoki: This does not work if any two values are same either in xP or yP. – erbal Oct 13 '15 at 09:02
  • @user11659, well, if 2 values are the same then your rectangle is _axis aligned_. So do a check for this condition at the beginning, then based on that use JaBe's solution or my extension. – Hoki Oct 13 '15 at 09:47
  • @Hoki: Rectangle is not necessarily axis aligned. parallel or slightly tilted also possible. As we are working on a general solution, either we should check in beginning from vertices about this. – erbal Oct 13 '15 at 10:07
0

This is solution I worked out.

fx=[x1 x2 x3 x4];  %represent x-cord
fy=[y1 y2 y3 y4];   %represent y-cord 
[xmn, ixmn]=min(fx);
fx(ixmn)=NaN;
[xmn2,ixmn2]=min(fx);
fx(ixmn)=xmn;   % to restore original data

if(fy(ixmn)>fy(ixmn2))
    ul=ixmn2; %upper-left
    ll=ixmn;  %lower left 
else
    ul=ixmn; 
    ll=ixmn2;
end
idd=setdiff(1:4,[ul ll]); 
if(fy(idd(1))>fy(idd(2)))
    ur=idd(2); %upper right
    lr=idd(1); %lower right
else
    ur=idd(1);
    lr=idd(2);
end

ul,ur ->position of upper left & right corner in fx,fy

ll,lr ->position of lower left & right corner in fx,fy

erbal
  • 421
  • 5
  • 18