Given coordinates of four points in a plane, find if the four points form a square or not.
To check for square, we need to check for following.
a) All fours sides formed by points are same.
b) The angle between any two sides is 90 degree. (This condition is required as Quadrilateral also has same sides.

How to check if given four points form a square

The idea is to pick any point and calculate its distance from rest of the points. Let the picked picked point be ‘p’. To form a square, distance of two points must be same from ‘p’, let this distance be d. The distance from one point must be different from that d and must be equal to √2 times d. Let this point with different distance be ‘q’.
The above condition is not good enough as the the point with different distance can be on the other side. We also need to check that q is at same distance from 2 other points and this distance is same as d.

[ad type=”banner”]

Below is C++ implementation of above idea.

// A C++ program to check if four given points form a square or not.
[pastacode lang=”cpp” manual=”%23include%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Structure%20of%20a%20point%20in%202D%20space%0Astruct%20Point%0A%7B%0A%20%20%20%20int%20x%2C%20y%3B%0A%7D%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20square%20of%20distance%0A%2F%2F%20from%20point%20’p’%20to%20poitn%20’q’%0Aint%20distSq(Point%20p%2C%20Point%20q)%0A%7B%0A%20%20%20%20return%20(p.x%20-%20q.x)*(p.x%20-%20q.x)%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20(p.y%20-%20q.y)*(p.y%20-%20q.y)%3B%0A%7D%0A%20%0A%2F%2F%20This%20function%20returns%20true%20if%20(p1%2C%20p2%2C%20p3%2C%20p4)%20form%20a%0A%2F%2F%20square%2C%20otherwise%20false%0Abool%20isSquare(Point%20p1%2C%20Point%20p2%2C%20Point%20p3%2C%20Point%20p4)%0A%7B%0A%20%20%20%20int%20d2%20%3D%20distSq(p1%2C%20p2)%3B%20%20%2F%2F%20from%20p1%20to%20p2%0A%20%20%20%20int%20d3%20%3D%20distSq(p1%2C%20p3)%3B%20%20%2F%2F%20from%20p1%20to%20p3%0A%20%20%20%20int%20d4%20%3D%20distSq(p1%2C%20p4)%3B%20%20%2F%2F%20from%20p1%20to%20p4%0A%20%0A%20%20%20%20%2F%2F%20If%20lengths%20if%20(p1%2C%20p2)%20and%20(p1%2C%20p3)%20are%20same%2C%20then%0A%20%20%20%20%2F%2F%20following%20conditions%20must%20met%20to%20form%20a%20square.%0A%20%20%20%20%2F%2F%201)%20Square%20of%20length%20of%20(p1%2C%20p4)%20is%20same%20as%20twice%0A%20%20%20%20%2F%2F%20%20%20%20the%20square%20of%20(p1%2C%20p2)%0A%20%20%20%20%2F%2F%202)%20p4%20is%20at%20same%20distance%20from%20p2%20and%20p3%0A%20%20%20%20if%20(d2%20%3D%3D%20d3%20%26%26%202*d2%20%3D%3D%20d4)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20d%20%3D%20distSq(p2%2C%20p4)%3B%0A%20%20%20%20%20%20%20%20return%20(d%20%3D%3D%20distSq(p3%2C%20p4)%20%26%26%20d%20%3D%3D%20d2)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20The%20below%20two%20cases%20are%20similar%20to%20above%20case%0A%20%20%20%20if%20(d3%20%3D%3D%20d4%20%26%26%202*d3%20%3D%3D%20d2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20d%20%3D%20distSq(p2%2C%20p3)%3B%0A%20%20%20%20%20%20%20%20return%20(d%20%3D%3D%20distSq(p2%2C%20p4)%20%26%26%20d%20%3D%3D%20d3)%3B%0A%20%20%20%20%7D%0A%20%20%20%20if%20(d2%20%3D%3D%20d4%20%26%26%202*d2%20%3D%3D%20d3)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20d%20%3D%20distSq(p2%2C%20p3)%3B%0A%20%20%20%20%20%20%20%20return%20(d%20%3D%3D%20distSq(p3%2C%20p4)%20%26%26%20d%20%3D%3D%20d2)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20Point%20p1%20%3D%20%7B20%2C%2010%7D%2C%20p2%20%3D%20%7B10%2C%2020%7D%2C%0A%20%20%20%20%20%20%20%20%20%20p3%20%3D%20%7B20%2C%2020%7D%2C%20p4%20%3D%20%7B10%2C%2010%7D%3B%0A%20%20%20%20isSquare(p1%2C%20p2%2C%20p3%2C%20p4)%3F%20cout%20%3C%3C%20%22Yes%22%3A%20cout%20%3C%3C%20%22No%22%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Yes
[ad type=”banner”]