Square (Codeforces 1921A)
Tags: Greedy, math

A square of positive (strictly greater than $$$0$$$) area is located on the coordinate plane, with sides parallel to the coordinate axes. You are given the coordinates of its corners, in random order. Your task is to find the area of the square.

Input: Each test consists of several testcases. The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of testcases. The following is a description of the testcases.Each testcase contains four lines, each line contains two integers $$$x_i, y_i$$$ ($$$-1000\le x_i, y_i\le 1000$$$), coordinates of the corners of the square. It is guaranteed that there is a square with sides parallel to the coordinate axes, with positive (strictly greater than $$$0$$$) area, with corners in given points.

Output: For each test case, print a single integer, the area of the square.


Sample Inputs:
3
1 2
4 5
1 5
4 2
-1 1
1 -1
1 1
-1 -1
45 11
45 39
17 11
17 39

Sample Outputs:
9
4
784

Solution (Python):
def main():
    for i in range(int(input())):
        x_axis = []
        y_axis = []
        for j in range(4):
            (a, b) = map(int, input().split())
            if a not in x_axis:
                x_axis.append(a)
            if b not in y_axis:
                y_axis.append(b)
        print(abs(x_axis[0] - x_axis[1]) * abs(y_axis[0] - y_axis[1]))
main()

Solution (Python):
T = int(input())
for _ in range(T):
    (x1, y1) = map(int, input().split())
    (x2, y2) = map(int, input().split())
    (x3, y3) = map(int, input().split())
    (x4, y4) = map(int, input().split())
    l = max(abs(x1 - x2), abs(y1 - y2), abs(x2 - x3), abs(y2 - y3), abs(x3 - x4), abs(y3 - y4), abs(x4 - x1), abs(y4 - y1))
    a = l ** 2
    print(a)

Solution (Python):
for _ in range(int(input())):
    points = []
    for i in range(4):
        p1 = list(map(int, input().split()))
        points.append(p1)
    (h, v) = (True, True)
    (x, y) = points[0]
    for i in range(1, 4):
        (a, b) = points[i]
        if a != x and h:
            x = abs(x - a)
            h = False
        if y != b and v:
            y = abs(y - b)
            v = False
    print(x * y)