Given the coordinates of four points in 2D space, return whether the four points could construct a square.

The coordinate (x,y) of a point is represented by an integer array with two integers.

Example:

Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True



 Note: 

All the input integers are in the range [-10000, 10000].
A valid square has four equal sides with positive length and four equal angles (90-degree angles).
Input points have no order.

Difficulty: MEDIUM_HARD
Tags: Geometry

Mathematics

Starter code:
class Solution:
    def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
        

Solutions:
class Solution:

	def validSquare(self, p1, p2, p3, p4):

		def length(x, y):
			return (x[0] - y[0]) * (x[0] - y[0]) + (x[1] - y[1]) * (x[1] - y[1])
		res = []
		a1 = length(p1, p2)
		a2 = length(p1, p3)
		a3 = length(p1, p4)
		a4 = length(p2, p3)
		a5 = length(p2, p4)
		a6 = length(p3, p4)
		res = [a1, a2, a3, a4, a5, a6]
		res = sorted(res)
		for i in range(3):
			if res[i] == res[i + 1]:
				continue
			else:
				return False
		if res[4] != res[5]:
			return False
		if res[0] != 0:
			return True
		else:
			return False


class Solution:

	def validSquare(self, p1, p2, p3, p4):
		p1 = [p1[0] * 4, p1[1] * 4]
		p2 = [p2[0] * 4, p2[1] * 4]
		p3 = [p3[0] * 4, p3[1] * 4]
		p4 = [p4[0] * 4, p4[1] * 4]
		center_x = (p1[0] + p2[0] + p3[0] + p4[0]) / 4
		center_y = (p1[1] + p2[1] + p3[1] + p4[1]) / 4
		print((center_x, ' ', center_y))
		if p1 == [center_x, center_y]:
			return False
		center_to_p1 = [center_x - p1[0], center_y - p1[1]]
		e_p2 = [int(center_x - (center_y - p1[1])), int(center_y + (center_x - p1[0]))]
		e_p3 = [int(center_x + center_x - p1[0]), int(center_y + center_y - p1[1])]
		e_p4 = [int(center_x + (center_y - p1[1])), int(center_y - (center_x - p1[0]))]
		if (e_p2 == p2 or e_p2 == p3 or e_p2 == p4) and (e_p3 == p2 or e_p3 == p3 or e_p3 == p4) and (e_p4 == p2 or e_p4 == p3 or e_p4 == p4):
			return True
		else:
			return False


class Solution(object):

	def validSquare(self, p1, p2, p3, p4):
		points = [p1, p2, p3, p4]
		dists = collections.Counter()
		for i in range(len(points)):
			for j in range(i + 1, len(points)):
				dists[self.getDistance(points[i], points[j])] += 1
		return len(list(dists.values())) == 2 and 4 in list(dists.values()) and (2 in list(dists.values()))

	def getDistance(self, p1, p2):
		return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2


class Solution:

	def validSquare(self, p1, p2, p3, p4):

		def distance(m1, m2):
			x1 = m1[0]
			y1 = m1[1]
			x2 = m2[0]
			y2 = m2[1]
			return ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** 0.5
		m = [d1, d2, d3, d4, d5, d6] = (distance(p1, p2), distance(p2, p3), distance(p3, p4), distance(p4, p1), distance(p2, p4), distance(p3, p1))
		m = sorted(list(m))
		print(m)
		c = 0
		x = m[0]
		if m[0] == 0.0 and m[1] == 0.0 and (m[2] == 0.0) and (m[3] == 0.0):
			return False
		for i in m:
			if c == 4:
				return True
			if i == m[0]:
				c += 1
			else:
				m[0] = i
				c = 0
		return False


class Solution:

	def dist(self, p1, p2):
		return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)

	def validSquare(self, p1, p2, p3, p4):
		ps = [p1, p2, p3, p4]
		ds = []
		for i in range(3):
			for j in range(i + 1, 4):
				ds.append(self.dist(ps[i], ps[j]))
		ds.sort()
		return ds[0] > 0 and ds[0] == ds[1] == ds[2] == ds[3] and (ds[4] == ds[5])


class Solution:

	def validSquare(self, p1, p2, p3, p4):
		p = []
		for i in range(1, 5):
			p.append(eval('p' + str(i)))
		e = {}
		for i in range(4):
			for j in range(4):
				for k in range(j + 1, 4):
					if i == j or i == k or j == k:
						continue
					dij = (p[i][0] - p[j][0]) ** 2 + (p[i][1] - p[j][1]) ** 2
					dik = (p[i][0] - p[k][0]) ** 2 + (p[i][1] - p[k][1]) ** 2
					djk = (p[j][0] - p[k][0]) ** 2 + (p[j][1] - p[k][1]) ** 2
					if dij == dik and dij + dik == djk:
						if dij in e:
							e[dij] += 1
						else:
							e[dij] = 1
		for k in e:
			print(e[k])
		if len(e) != 1:
			return False
		for k in e:
			if e[k] == 4:
				return True
			else:
				return False


class Solution:

	def validSquare(self, p1, p2, p3, p4):
		ps = sorted([p1, p2, p3, p4])
		return ps[0][0] + ps[3][0] == ps[1][0] + ps[2][0] and ps[0][1] + ps[3][1] == ps[1][1] + ps[2][1] and (ps[2][0] - ps[1][0] == abs(ps[3][1] - ps[0][1])) and (not ps[0] == ps[1])


class Solution:

	def validSquare(self, A, B, C, D):

		def sub(p2, p1):
			return [p2[0] - p1[0], p2[1] - p1[1]]
		if sub(A, B) == [0, 0]:
			return False
		if sub(B, A) == sub(D, C) and sub(C, A) == sub(D, B):
			print(1)
			diff1 = sub(B, A)
			diff2 = sub(A, C)
			print((diff1, diff2))
			if diff1 == [-diff2[1], diff2[0]] or diff1 == [diff2[1], -diff2[0]]:
				return True
		elif sub(D, A) == sub(C, B) and sub(B, A) == sub(C, D):
			print(2)
			diff1 = sub(D, A)
			diff2 = sub(A, B)
			print((diff1, diff2))
			if diff1 == [-diff2[1], diff2[0]] or diff1 == [diff2[1], -diff2[0]]:
				return True
		elif sub(C, A) == sub(B, D) and sub(D, A) == sub(B, C):
			print(3)
			diff1 = sub(C, A)
			diff2 = sub(A, D)
			print((diff1, diff2))
			if diff1 == [-diff2[1], diff2[0]] or diff1 == [diff2[1], -diff2[0]]:
				return True
		else:
			print(0)
		return False


class Solution:

	def validSquare(self, p1, p2, p3, p4):
		l = [p1, p2, p3, p4]
		s = set()
		for i in range(len(l)):
			for j in range(i + 1, len(l)):
				d = self.dist(l[i], l[j])
				if d == 0:
					return False
				s.add(d)
		return len(s) == 2

	def dist(self, a, b):
		return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2