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:
 Click to share on Facebook (Opens in new window)
 Click to share on Twitter (Opens in new window)
 Click to share on Google+ (Opens in new window)
 Click to share on LinkedIn (Opens in new window)
 Click to share on Pinterest (Opens in new window)
 Click to share on WhatsApp (Opens in new window)
 Click to share on Telegram (Opens in new window)
 More
Related
Arindam Bose
Scientific History Blog Writer â€¢ Art enthusiast and Illustrator â€¢ Amateur Photographer â€¢ Biker and Hiker â€¢ Beer Enthusiast â€¢ Electrical Engineer â€¢ Chicago

"The incomplete perception of completeness" . . . â€¢ blackworknowkblackandwhitehsketchetinstagram.com/p/BZ2mZsZAcJ6/bTx2t4
2 weeks ago, @adambose

Do you have regrets? I am not asking for any specific ones, I just want to know if you haveâ€instagram.com/p/BZHw7knAaAF/O7
1 month ago, @adambose

Try ridewithvia_CHI! Rides start from just $3/ride. Use my code arindam7e5 for $10 credit. Download the app here: bit.ly/2bofk5p
2 months ago, @adambose

The Prologue and the Epilogue of an image arindambose.com/?p=999 pic.twitter.com/aUJluZ26OS
2 months ago, @adambose

The residue of Kurukshetra arindambose.com/?p=994
2 months ago, @adambose

2 months ago, @adambose

3 months ago, @adambose

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

4 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
4 months ago, @adambose
Leave a Reply