Python 97 (without complex points)
def t(p):return len(set(p))-1==len(set([pow(pow(a-c,2)+pow(b-d,2),.5)for a,b in p for c,d in p]))
This will take lists of point tuples in [(x,y),(x,y),(x,y),(x,y)] in any order, and can handle duplicates, or the wrong number of points. It does NOT require complex points like the other python answers.
You can test it like this:
S1 = [(0,0),(1,0),(1,1),(0,1)] # standard square
S2 = [(0,0),(2,1),(3,-1),(1,-2)] # non-axis-aligned square
S3 = [(0,0),(1,1),(0,1),(1,0)] # different order
S4 = [(0,0),(2,2),(0,2),(2,0)] #
S5 = [(0,0),(2,2),(0,2),(2,0),(0,0)] #Redundant points
B1 = [(0,0),(0,2),(3,2),(3,0)] # rectangle
B2 = [(0,0),(3,4),(8,4),(5,0)] # rhombus
B3 = [(0,0),(0,0),(1,1),(0,0)] # only 2 distinct points
B4 = [(0,0),(0,0),(1,0),(0,1)] # only 3 distinct points
B5 = [(1,1),(2,2),(3,3),(4,4)] # Points on the same line
B6 = [(0,0),(2,2),(0,2)] # Not enough points
def tests(f):
assert(f(S1) == True)
assert(f(S2) == True)
assert(f(S3) == True)
assert(f(S4) == True)
assert(f(S5) == True)
assert(f(B1) == False)
assert(f(B2) == False)
assert(f(B3) == False)
assert(f(B4) == False)
assert(f(B5) == False)
assert(f(B6) == False)
def t(p):return len(set(p))-1==len(set([pow(pow(a-c,2)+pow(b-d,2),.5)for a,b in p for c,d in p]))
tests(t)
This will take a little explaining, but the overall idea is that there are only three distances between the points in a square (Side, Diagonal, Zero(point compared to itself)):
def t(p):return len(set(p))-1==len(set([pow(pow(a-c,2)+pow(b-d,2),.5)for a,b in p for c,d in p]))
- for a list p of tuples (x,y)
- Remove duplicates using set(p) and then test the length
- Get every combination of points (a,b in p for c,d in p)
- Get list of the distance from every point to every other point
- Use set to check there are only three unique distances
-- Zero (point compared to itself)
-- Side length
-- Diagonal length
To save code characters I am:
- using a 1 char function name
- using a 1 line function definition
- Instead of checking the number of unique points is 4, I check that it is -1 the different point lengths (saves ==3==)
- use list and tuple unpacking to get a,b in p for c,d in p, instead of using a[0],a[1]
- uses pow(x,.5) instead of including math to get sqrt(x)
- not putting spaces after the )
- not putting a leading zero on the float
I fear someone can find a test case that breaks this. So please do and Ill correct. For instance the fact I just check for three distances, instead of doing an abs() and checking for side length and hypotenuse, seems like an error.
First time I've tried code golf. Be kind if I've broken any house rules.