Menu Close

Given the coordinates, return true if the four points construct a square.

Given the coordinates of four points in 2D space p1, p2, p3 and p4, return true if the four points construct a square. The coordinate of a point pi is represented as [xi, yi]. The input is not given in any order.
A valid square has four equal sides with positive length and four equal angles (90-degree angles).

Example 1:
Input: p1 = [2,6], p2 = [5,1], p3 = [0,-2], p4 = [-3,3]
Output: true

Example 2:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Output: false
import math

A=list(map(int,input('Point p1: ').split(',')))
B=list(map(int,input('Point p2: ').split(',')))
C=list(map(int,input('Point p3: ').split(',')))
D=list(map(int,input('Point p4: ').split(',')))

LIST=[A,B,C,D]
M=sorted(LIST , key=lambda X: [X[1], X[0]])

p1,p2,p3,p4 = M[0],M[1],M[2],M[3]


def length(x,y):
	return (((y[0]-x[0])**2) + ((y[1]-x[1])**2))

flag=0

if length(p1,p2)==length(p2,p4):
	if length(p2,p4) == length(p4,p3):
		if length(p4,p3) == length(p3,p1):
			if length(p2,p3) == length(p1,p4):
				flag=1
			else:
				flag=0
		else:
			flag=0
	else:
		flag=0
else:
	flag=0

print(True) if flag==1 else print(False)

YOU CAN INPUT THE COORDINATES(POINTS) IN ANY ORDER.
PRACTICAL SAMPLE IMAGE BELOW

Input_1:
Point p1: 2,6
Point p2: 5,1
Point p3: 0,-2
Point p4: -3,3

Output:
True



Input_2:
Point p1: 0,0
Point p2: 1,1
Point p3: 1,0
Point p4: 0,12

Output:
False


Input_3:
Point p1: 0,0
Point p2: 1,1
Point p3: 1,0
Point p4: 0,1

Output:
True


Input_4:
Point p1: 1,0
Point p2: -1,0
Point p3: 0,1
Point p4: 0,-1

Output:
True


Input_5:
Point p1: 10,20
Point p2: 20,20
Point p3: 10,10
Point p4: 20,10

Output:
True


Sample Image of Output and the Execution of the Code.

Executed the code in terminal(Ubuntu) python3

More Q