Assume I have 4 points (they are 2-dimension), which are different from each other, and I want to know whether they form a square. How to do it? (let the process be as simple as possible.)
Assuming that your square might be rotated against whatever coordinates system you have in place, you can't rely on there being any repetition of X and Y values in your four points.
What you can do is calculate the distances between each of the four points. If you find the following to be true, you have a square:
Incidentally, the distance z will have to be SQRT((x^2)/2), but you don't need to confirm this. If conditions 1 and 2 are true then you have a square. NOTE: Some people are concerned about the inefficiency of square root. I didn't say that you should do this calculation, I just said that if you did you would get a predictable result!
The bare minimum of work that you would need to do would be to pick a point, say A and calculate the distance to each of the other three points. If you can find that A is x from one point and z from two other points, then you just need to check those two other points against each other. If they are also x from each other then you have a square. i.e.:
Since AB = AD, check BD:
Just to be sure, you need to check the other sides: BC and CD.
Since AC = BD and AC = BC = CD, therefore this a square.
Along the way, if you find more than two distinct edge distances then the figure cannot be a square, so you can stop looking.
EDIT: Working Example Implementation
Since there is confusion about what the algorithm is, I have created a working example on jsfiddle (see here). In my explanation of the algorithm, I use arbitrary points A, B, C, and D. Those arbitrary points happen to be in a certain order for the sake of walking through the example. The algorithm works even if the points are in a different order, however, the example doesn't necessarily work if those points are in a different order.
Thanks to: meshuai, Blrfl, MSalters and Bart van Ingen Schenau for useful comments to improve this answer.
I think you can do this with simple addition and subtraction and finding min/max. Terms (matches other people's diagram):
If 4 points share only 2 x values and 2 y values you have a level square.
Otherwise, you have a square if your points satisfy the following:
Explanation: The line segments A-C and B-D should meet at their midpoints. Thus (A.x + C.x) / 2 is the midpoint of A-C and (B.x + D.x) / 2 is the midpoint of B-D. Multiply each side of this equation by 2 to get my first equation. The second equation is the same thing for Y-values. Diamond-shapes (rhomboids) will satisfy these properties, so you need to check that you have equal sides - that the width is the same as the height. That's the third equation.
I think the easiest solution is the following:
The advantage of this solution is that you don't have to use square roots at all.
Similar to the answer by starblue
Pick any three of the four points.
Look for a right angled vertex among them: By checking if the dot product of any two of the three vectors is zero. If not found, not a square.
Check whether the vertices adjacent to this angle are right angled too. If not, not a square.
Check whether diagonals are perpendicular: If the dot product of the vectors between the first and fourth vertex and the other two vertices (diagonals) is zero, then its a square.
this is not an answer according to the standards set, but I hope this helps:
[Copied from the link below so you don't have to open the link] Python 76 characters
Function S takes a list of complex numbers as its input (A). If we know both the centre and one corner of a square, we can reconstruct the square by rotating the corner 90,180 and 270 degrees around the centre point (c). On the complex plane rotation by 90 degrees about the origin is done by multiplying the point by i. If our original shape and the reconstructed square have the same points then it must have been a square.
This was taken from : Determine if 4 points form a square
If you like the answer, I say, take some moments out to thank the person, or up-vote his answer on that page.
There are some good answers here, but the question asked for the simplest approach. I gave this some quick thought and this is how I would do it.
You can tell if four points represent a square (even if rotated), but finding the average of the four points.
Once you have the average, the distance between each point and the average would have to be the same for all four points.
My mistake. That would only tell you if the form points were on a circle. If you also check the distance between points, then it must be a square.
This assumes that points A,B,C,D do not cross (as in have a valid winding order).
Pick three of the four points.
Figure out if it's a right isosceles triangle by checking if one of the three vectors between points is equal to another one rotated by 90 degrees.
If so, compute the fourth point by vector addition and compare it to the given fourth point.
Note that this doesn't require expensive square roots, not even multiplication.
I'm sorry but some answers don't apply.
For the case you measure 3 edges (let's say AB, AC and AD) to find that two have the same size (let's say AC and AD) and one is bigger (let's say AB). Then you would measure CD to see if it's the same size of AB, and you find that it is. Instead of a square, you could have the picture below, and that makes it a wrong solution.
Then you try some other solution: measure all the distances at least once: AB, AC, AD, BC, BD, CD. Then you find that 4 of then are equal, and the other 2 are also equal among themselves. But you could just have a picture like below:
So, those answers aren't correct, despite the high upvotes they received.
One possible solution: if the two equal measures don't connect the same point. So: if AB and CD are the same lenght, all the other combinations (AC, AD, BC, BD) are also equal, you have a square. If you have the same point making the biggest length (AB and AC is the biggest, and all the others are equal), you have one of the pictures above.
Let the four points have coordinate vectors a, b, c, d.
Then lets call their differences w=(a-d), x=(b-a), y=(c-b), z=(d-c).
Then w is orthogonal to a if you can create w from a by a 90-degree-rotation. Mathematically the 90-degree-rotation-matrix in 2-space is ( ( 0, -1 ), ( 1, 0 ) ). Thus, the condition whether w is a 90-degree-rotated a results in
( w_1 == -x_2 and w_2 == x_1 )
If this holds, then you need to check that w == -y and x == -z, or
( ( w_1 == -y_1 and w_2 == -y_2 ) and ( x_1 == -z_1 and x_2 == -z_2 ) )
If those three relations hold, a, b, c, d make an oriented square.