Determine if 4 points form a square Write a function that takes 4 points on the plane as input and returns true iff the 4 points form a square.  The points will have integral coordinates with absolute values &lt; 1000.

You may use any reasonable representation of the 4 points as input.  The points are not provided in any particular order.

Shortest code wins.

Example squares:

(0,0),(0,1),(1,1),(1,0)    # standard square
(0,0),(2,1),(3,-1),(1,-2)  # non-axis-aligned square
(0,0),(1,1),(0,1),(1,0)    # different order


Example non-squares:

(0,0),(0,2),(3,2),(3,0)  # rectangle
(0,0),(3,4),(8,4),(5,0)  # rhombus
(0,0),(0,0),(1,1),(0,0)  # only 2 distinct points
(0,0),(0,0),(1,0),(0,1)  # only 3 distinct points


You may return either true or false for the degenerate square (0,0),(0,0),(0,0),(0,0)
J, 28 17 25 27

J doesn't really have functions, but here's a monadic verb that takes a vector of points from the complex plane:

4 8 4-:#/.~&amp;(/:~&amp;:|&amp;,&amp;(-/~))


Method is a mix of Michael Spencer (work solely on inter-vertex lengths; but he's currently failing my rhombus2) and Eelvex's (check the sets' sizes) works.  Reading right to left:


-/~ compute all point differences
, flatten
| extract magnitude
/:~ sort up
#/.~ nub and count
4 8 4 -: must have exactly 4 equidistant (at 0), 8 a bit bigger (length 1, sides), 4 bigger yet (length sqrt 2, diagonals)


Demonstration:

   NB. give the verb a name for easier use
   f =: 4 8 4-:#/.~&amp;(/:~&amp;:|&amp;,&amp;(-/~))

   NB. standard square
   f 0 0j1 1j1 1
1

   NB. non-axis-aligned square
   f 0 2j1 3j_1 1j_2
1

   NB. different order
   f 0 1j1 0j1 1
1

   NB. rectangle
   f 0 0j2 3j2 3
0

   NB. rhombus 1
   f 0 3j4 8j4 5
0

   NB. rhombus 2
   f 0 1ad_60 1ad0 1ad60
0


For memory's sake, my previous method (required ordered vertices, but could detect regular polygons of any order):

*./&amp;(={.)&amp;(%1&amp;|.)&amp;(-1&amp;|.)


See history for explanation and demo.  The current method could probably be expanded to other polygons, that 4 8 4 does look a lot like a binomial distribution.