First of all I must say that many of us already know this trick probably. But I encountered this problem once during a big project, I was a bit confused finding the solution. It seemed to be simple problem, but somehow I couldn’t find the answer that how I can make a general quadrangle out of four random points in XY Cartesian coordinate system. I mean to say in which order I should take the points to join them together to make a quadrangle. I searched in the internet hoping that someone else might have solved the problem earlier, but I found nothing about it. Then I thought no one might fall into such problem as everyone might already know the solution. However, I found myself a big nuts, after thinking about the problem a bit rationally. I found a simpler algorithm so I am gladly sharing the algorithm with you.
Second of all, what I am going to discuss in next few minutes, will be all in Imaging coordinate system which is a bit different from general XY Cartesian coordinate system. So there will be no negative coordinates, all points are positive and the origin is (1,1). And the origin is the most TopLeft point of the image.
Suppose, we take four random points in image coordinate system: (11,16), (10,7), (4,11) and (19,9) in Fig.1.
So the Point matrix is:
11 16
10 7
4 11
19 9
Step 1: First sort the matrix using the first column only i.e with respect to X coordinate in ascending order. So now the Matrix would be like this:
4 11
10 7
11 16
19 9
Step 2: Now sort the first two rows using the Y coordinate in ascending order and sort the next two rows in descending order. So now the Matrix would be like this:
10 7
4 11
11 16
19 9
Step 3: Your quadrangle points are sorted now, Name the points back to back: I, II, III, IV as in Fig.2.
Connect I with II:
Connect II with III:
Connect III with IV:
Connect IV with I:
Very simple, isn’t it? Simple but useful trick. Take more examples to make the idea clear. Now I am giving the MATLAB code for this algorithm. You can contact me if you need further assistance.
MATLAB CODE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Program Name : Quadrangle Sort
% Author : Arindam Bose
% Version : 1.0
% Description : This program sorts 4 points in quadrangle order
% Copyright : Â© Arindam Bose, All rights reserved.
% License : Open Source, Freeware
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [rectMatrix] = rectanglesort(points)
rectMatrix = sortrows(points);
if rectMatrix(1,2) > rectMatrix(2,2)
temp = rectMatrix(1,:);
rectMatrix(1,:) = rectMatrix(2,:);
rectMatrix(2,:) = temp;
end
if rectMatrix(3,2) < rectMatrix(4,2)
temp = rectMatrix(3,:);
rectMatrix(3,:) = rectMatrix(4,:);
rectMatrix(4,:) = temp;
end
end
Hope this helps a lot. Thanks… More Tricks are coming soon…
Share this:
Related
Arindam Bose
Scientific History Blog Writer â€¢ Art enthusiast and Illustrator â€¢ Amateur Photographer â€¢ Biker and Hiker â€¢ Beer Enthusiast â€¢ Electrical Engineer â€¢ Chicago

2 weeks ago, @adambose

I just opened my etsy shop, go check it out. etsy.me/2sRUfvo via Etsy
2 months ago, @adambose

2 months ago, @adambose

Remember the DearDad project by hitRECord and instagram for father's day I was talking about? Look, the 1015... fb.me/35anBEnuR
2 months ago, @adambose

I have been illustrating for a project called "Dear Dad" in collaboration with HitRecord and Instagram. To... fb.me/8UvuciDNO
2 months ago, @adambose

4 months ago, @adambose

4 months ago, @adambose

Did You Know: How Egyptians Rocked the World of Science Long beforeÂ Greeksarindambose.com/?p=938vpic.twitter.com/jhT5ofqal88
4 months ago, @adambose

Dyamn, see Neelab, let them know now, what happened behind the scene, on that day. ðŸfb.me/1OHQ0CdGnhqZ
4 months ago, @adambose

Do you like my work? If you do, please support my work by buying stuffs at society6.com/iadambose society6
7 months ago, @adambose
Leave a Reply