总体思路仍然是枚举所有情况,面向对象,可读性高,为了防止代码过长,没有进行属性封装。
用一个对象数组存储四个角点,通过实现comparator接口实现排序,通过排序保证了数组 0和3的对象是对角角点,于是对角线就是0,3 和 1,2
对角线中点重合,对角线长度相等且大于0,对角线互相垂直>>>>>>正方形

旋转,ox,oy为旋转中心:

 int temp = x;
 x = -(y - oy) + ox;
 y = temp - ox + oy;

全部代码如下,值得注意的是对象数组传参时,排序会影响该数组,所以需要深copy该数组。

import java.util.*;

class Node {
    public int x, y, ox, oy;
    public int count;

    public Node(int x, int y, int ox, int oy){
        this.x = x;
        this.y = y;
        this.ox = ox;
        this.oy = oy;
        this.count = 0;
    }

    public void rotate(){
        int temp = x;
        x = -(y - oy) + ox;
        y = temp - ox + oy;
        count++;
        if(count >= 4){
            count = 0;
        }
    }
}

public class Main {
    public static int isSquare(Node[] temp){
        Node[] nodes = new Node[4];
        for(int i = 0; i < 4; i++){
            nodes[i] = new Node(temp[i].x, temp[i].y, 0, 0);
        }
        Arrays.sort(nodes, new Comparator<Node>() {
            @Override public int compare(Node o1, Node o2){
                if(o1.x != o2.x){
                    return Integer.compare(o1.x, o2.x);
                }else{
                    return Integer.compare(o1.y, o2.y);
                }
            }
        });
        if(nodes[0].x + nodes[3].x == nodes[1].x + nodes[2].x && nodes[0].y + nodes[3].y == nodes[1].y + nodes[2].y){ //对角线中点重合
            if((nodes[0].x - nodes[3].x) * (nodes[1].x - nodes[2].x) + (nodes[0].y - nodes[3].y) * (nodes[1].y - nodes[2].y) == 0){//对角线垂直
                if(Math.pow(nodes[0].x - nodes[3].x, 2) + Math.pow(nodes[0].y - nodes[3].y, 2) == Math.pow(nodes[0].x - nodes[3].x, 2) + Math.pow(nodes[0].y - nodes[3].y, 2)){
                    if(Math.pow(nodes[0].x - nodes[3].x, 2) + Math.pow(nodes[0].y - nodes[3].y, 2) > 0 && Math.pow(nodes[0].x - nodes[3].x, 2) + Math.pow(nodes[0].y - nodes[3].y, 2) > 0){ //对角线长度>0
                        return temp[0].count + temp[1].count + temp[2].count + temp[3].count;
                    }
                }
            }
        }

        return -1;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0; i < n; i++){
            Node[] nodes = new Node[4];
            for(int j = 0; j < 4; j++){
                int a = sc.nextInt();
                int b = sc.nextInt();
                int x = sc.nextInt();
                int y = sc.nextInt();
                nodes[j] = new Node(a, b, x, y);
            }

            int cnt = -1;
            for(int k1 = 0; k1 < 4; k1++){
                nodes[0].rotate();
                for(int k2 = 0; k2 < 4; k2++){
                    nodes[1].rotate();
                    for(int k3 = 0; k3 < 4; k3++){
                        nodes[2].rotate();
                        for(int k4 = 0; k4 < 4; k4++){
                            nodes[3].rotate();
                            int temp = isSquare(nodes);

                            if(temp != -1){
                                if(cnt == -1){
                                    cnt = temp;
                                }else{
                                    cnt = Math.min(temp, cnt);
                                }
                            }
                        }
                    }
                }
            }
            System.out.println(cnt);
        }
    }
}