Sudhakar Rayavaram
Problem solver (And maker),
Inquisitive (Root to most of my problems),
Software craftsman (Don't ask for estimates)

Works at TarkaLabs

Tech guy behind SportIndia.in

Is it a rectangle?
02 Nov 2008

Couple of days back when I was travelling back to home from office, I overheard two fellow employees discussing over a programming problem… “If four points in a 2D plane (x,y) is given, how to find whether it forms a rectangle or not?”. Question sounds simple right? Well, how would you program this solution? what will be the algorithm? Just give it a shot.

Check out my implementation below… It did not look this small and simple when I started solving it. Tried calculating angles between edges, tried equating edge lengths, tried rotating the skewed rectangles (special case), tried getting B.Sc. Maths degree first… But, all of these are not really needed for the solution… It is enough if you calculate the centroid and make sure all the 4 points are equi-distant from the centroid. Well, square also satisfies this logic and square is also a rectangle :)

I guess it is always hard to come up with a simple solution… You have to put lot of effort to think simple!