Given coordinates of four points in a plane. Find if the four points form a square or not.
Example 1:
Input:
points=(0,0),(0,1),(1,0),(1,1)
Output:
1
Explanation:
These points form a square.
Example 2:
Input:
points=(0,0),(1,1),(1,0),(0,2)
Output:
0
Explanation:
These four points do not form a square.
Your Task:
You don't need to read input or print anything.Your Task is to complete the function fourPointSquare() which takes a 2D array of dimensions 4x2 which contains the cordinates of the four points and returns 1 if they form a square.Otherwise it returns 0.
 
Expected Time Complexity:O(1)
Expected Space Complexity:O(1)
 
Constraints:
0<=X-cordinate,Y-cordinate<=10^{5}

Difficulty: EASY
Tags: Mathematics
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3



class Solution:

    def fourPointSquare(self, points):    

        #code here

Solutions:
class Solution:

	def fourPointSquare(self, points):

		def distance(x, y):
			return (x[0] - y[0]) * (x[0] - y[0]) + (x[1] - y[1]) * (x[1] - y[1])
		record = dict()
		for i in range(4):
			for j in range(i + 1, 4):
				dist = distance(points[i], points[j])
				record[dist] = record.get(dist) + 1 if dist in record else 1
		if len(record) != 2:
			return 0
		for val in record.values():
			if not (val == 2 or val == 4):
				return 0
		return 1


class Solution:

	def fourPointSquare(self, points):
		l = []
		for i in range(4):
			for j in range(4):
				if i < j:
					x = points[i][0] - points[j][0]
					y = points[i][1] - points[j][1]
					l.append(x * x + y * y)
		l.sort()
		if l[0] == l[1] and l[0] == l[2] and (l[0] == l[3]) and (l[0] * 2 == l[4]) and (l[0] * 2 == l[5]) and (l[0] > 0):
			return 1
		else:
			return 0


class Solution:

	def fourPointSquare(self, points):
		times1 = {}
		times2 = {}
		for p in points:
			if p[0] not in times1:
				times1[p[0]] = 1
			else:
				times1[p[0]] += 1
		for p in points:
			if p[1] not in times2:
				times2[p[1]] = 1
			else:
				times2[p[1]] += 1
		if len(times1) != 2 or len(times2) != 2:
			return 0
		for k in times1:
			if times1[k] != 2:
				return 0
		for k in times2:
			if times2[k] != 2:
				return 0
		return 1


import math

class Solution:

	def fourPointSquare(self, points):
		x1 = points[0][0]
		y1 = points[0][1]
		l = []
		for i in range(1, 4):
			l.append(math.sqrt((x1 - points[i][0]) ** 2 + (y1 - points[i][1]) ** 2))
		l.sort()
		if l[0] != 0 and math.sqrt(l[0] ** 2 + l[1] ** 2) == l[2]:
			return 1
		else:
			return 0


class Solution:

	def caldis(self, d1, d2):
		dis = (d2[0] - d1[0]) * (d2[0] - d1[0]) + (d2[1] - d1[1]) * (d2[1] - d1[1])
		return dis

	def fourPointSquare(self, points):
		p1 = points[0]
		p2 = points[1]
		p3 = points[2]
		p4 = points[3]
		d2 = self.caldis(p1, p2)
		d3 = self.caldis(p1, p3)
		d4 = self.caldis(p1, p4)
		if d2 == 0 or d3 == 0 or d4 == 0:
			return 0
		if d2 == d3 and 2 * d2 == d4 and (2 * self.caldis(p2, p4) == self.caldis(p2, p3)):
			return 1
		if d3 == d4 and 2 * d3 == d2 and (2 * self.caldis(p3, p2) == self.caldis(p3, p4)):
			return 1
		if d2 == d4 and 2 * d2 == d3 and (2 * self.caldis(p2, p3) == self.caldis(p2, p4)):
			return 1
		return 0


class Solution:

	def fourPointSquare(self, points):
		(a, b, c, d) = (*points,)
		p = abs(points[0][0] - points[0][1])
		q = abs(points[1][0] - points[1][1])
		r = abs(points[2][0] - points[2][1])
		s = abs(points[3][0] - points[3][1])
		if p == 0 and p == q and (q == r) and (r == s) and (s == p):
			return 0
		if p + r == q + s:
			return 1
		return 0


class Solution:

	def fourPointSquare(self, points):
		(x1, y1) = (points[0][0], points[0][1])
		(x2, y2) = (points[1][0], points[1][1])
		(x3, y3) = (points[2][0], points[2][1])
		(x4, y4) = (points[3][0], points[3][1])
		d1 = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
		d2 = ((x3 - x1) ** 2 + (y3 - y1) ** 2) ** 0.5
		d3 = ((x4 - x1) ** 2 + (y4 - y1) ** 2) ** 0.5
		a = [d1, d2, d3]
		if a.count(min(a)) == 2 and a.count(max(a)) == 1:
			return 1
		else:
			return 0


class Solution:

	def f(self, x1, y1, x2, y2):
		d = x2 - x1
		e = y2 - y1
		return d * d + e * e

	def fourPointSquare(self, p):
		s = {}
		a = self.f(p[0][0], p[0][1], p[1][0], p[1][1])
		s[a] = 1
		a = self.f(p[1][0], p[1][1], p[2][0], p[2][1])
		s[a] = 1
		a = self.f(p[2][0], p[2][1], p[3][0], p[3][1])
		s[a] = 1
		a = self.f(p[3][0], p[3][1], p[0][0], p[0][1])
		s[a] = 1
		a = self.f(p[3][0], p[3][1], p[1][0], p[1][1])
		s[a] = 1
		a = self.f(p[0][0], p[0][1], p[2][0], p[2][1])
		s[a] = 1
		if len(s) != 2:
			return 0
		l = [0, 0]
		k = 0
		for i in s:
			l[k] = i
			k += 1
		if l[0] == l[1] * 2 or l[0] * 2 == l[1]:
			return 1
		return 0


class Solution:

	def fourPointSquare(self, points):
		d1 = abs(points[0][0] - points[0][1])
		d2 = abs(points[1][0] - points[1][1])
		d3 = abs(points[2][0] - points[2][1])
		d4 = abs(points[3][0] - points[3][1])
		if d1 == 0 and d1 == d2 and (d2 == d3) and (d3 == d4) and (d4 == d1):
			return 0
		if d1 + d3 == d2 + d4:
			return 1
		return 0


class Solution:

	def fourPointSquare(self, points):
		n = []
		for (i, j) in points:
			if i not in n:
				n.append(i)
			if j not in n:
				n.append(j)
		m = set(n)
		if len(m) > 2 or len(m) <= 1:
			return 0
		return 1
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		points = []
		for i in range(4):
			points.append(list(map(int, input().strip().split(' '))))
		ob = Solution()
		ans = ob.fourPointSquare(points)
		print(ans)


import math

class Solution:

	def fourPointSquare(self, points):
		a = abs(points[0][0] - points[0][1])
		b = abs(points[1][0] - points[1][1])
		c = abs(points[2][0] - points[2][1])
		d = abs(points[3][0] - points[3][1])
		if a == 0 and a == b and (b == c) and (c == d) and (d == a):
			return 0
		if a + c == b + d:
			return 1
		return 0
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		points = []
		for i in range(4):
			points.append(list(map(int, input().strip().split(' '))))
		ob = Solution()
		ans = ob.fourPointSquare(points)
		print(ans)


class Solution:

	def fourPointSquare(self, points):
		A = points[0]
		B = points[1]
		C = points[2]
		D = points[3]
		if A == B == C == D:
			return 0
		AB = math.sqrt((B[1] - A[1]) ** 2 + (B[0] - A[0]) ** 2)
		BC = math.sqrt((C[1] - B[1]) ** 2 + (C[0] - B[0]) ** 2)
		CD = math.sqrt((D[1] - C[1]) ** 2 + (D[0] - C[0]) ** 2)
		DA = math.sqrt((A[1] - D[1]) ** 2 + (A[0] - D[0]) ** 2)
		if AB == CD:
			return 1
		else:
			return 0


from math import sqrt

class Solution:

	def fourPointSquare(self, points):
		(x1, x2, x3, x4) = [a[0] for a in points]
		(y1, y2, y3, y4) = [b[1] for b in points]
		length_arr = []
		length_arr.append(self.getDistance(x1, y1, x2, y2))
		length_arr.append(self.getDistance(x1, y1, x3, y3))
		length_arr.append(self.getDistance(x1, y1, x4, y4))
		length_arr.append(self.getDistance(x2, y2, x3, y3))
		length_arr.append(self.getDistance(x2, y2, x4, y4))
		length_arr.append(self.getDistance(x3, y3, x4, y4))
		l1 = -1
		l2 = -1
		for length in length_arr:
			if length == 0:
				return 0
			if (l1 == -1) & (l2 == -1):
				l1 = length
			elif (l1 != length) & (l2 == -1):
				l2 = length
			elif (l1 != length) & (l2 != length):
				return 0
		return 1

	def getDistance(self, x1, y1, x2, y2):
		return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))


class Solution:

	def fourPointSquare(self, points):
		dist = []
		for i in range(3):
			f = points[i]
			for j in range(i + 1, 4):
				s = points[j]
				dist.append((s[0] - f[0]) ** 2 + (s[1] - f[1]) ** 2)
		m = {}
		for i in dist:
			if i in m:
				m[i] += 1
			else:
				m[i] = 1
		for i in m:
			if m[i] == 4:
				return 1
		return 0


class Solution:

	def fourPointSquare(self, points):
		dX = {}
		dY = {}
		for p in points:
			if p[0] in dX:
				dX[p[0]] += 1
			else:
				dX[p[0]] = 1
			if p[1] in dY:
				dY[p[1]] += 1
			else:
				dY[p[1]] = 1
		li = list(dX.values()) + list(dY.values())
		if len(set(li)) == 1 and li[0] == 2:
			return 1
		else:
			return 0


class Solution:

	def fourPointSquare(self, points):

		def dist(p1, p2):
			return (p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2
		d = [dist(points[0], points[1]), dist(points[0], points[2]), dist(points[0], points[3]), dist(points[1], points[2]), dist(points[1], points[3]), dist(points[2], points[3])]
		d.sort()
		if d[0] == 0:
			return 0
		if d[0] == d[1] and d[1] == d[2] and (d[2] == d[3]) and (d[4] == d[5]) and (d[5] == 2 * d[0]):
			return 1
		return 0


class Solution:

	def fourPointSquare(self, points):
		dict1 = {}
		for vertex in points:
			if vertex[0] not in dict1:
				dict1[vertex[0]] = [vertex[1]]
			else:
				dict1[vertex[0]].append(vertex[1])
		if len(dict1) != 2:
			return 0
		for vals in dict1.values():
			if not all((True if i in dict1 else False for i in vals)):
				return 0
		return 1


class Solution:

	def fourPointSquare(self, points):
		x1 = points[1][1] - points[0][1]
		x2 = points[1][0] - points[0][0]
		x3 = points[3][1] - points[2][1]
		x4 = points[3][0] - points[2][0]
		l1 = x1 * x1 + x2 * x2
		l2 = x3 * x3 + x4 * x4
		if l1 == l2 and l1 != 0:
			return 1
		else:
			return 0


class Solution:

	def caldist(self, x1, y1, x2, y2):
		ans = (y2 - y1) ** 2 + (x2 - x1) ** 2
		return ans

	def fourPointSquare(self, points):
		d1 = Solution().caldist(points[0][0], points[0][1], points[1][0], points[1][1])
		d2 = Solution().caldist(points[0][0], points[0][1], points[2][0], points[2][1])
		d3 = Solution().caldist(points[0][0], points[0][1], points[3][0], points[3][1])
		if d1 == 0 or d2 == 0 or d3 == 0:
			return 0
		elif d1 == d2 and d3 == 2 * d1:
			return 1
		elif d2 == d3 and d1 == 2 * d2:
			return 1
		elif d3 == d1 and d2 == 2 * d1:
			return 1
		return 0