思路
判断正方形: 用一个数组用于保存四个点之间的距离,求出两两点之间的距离,若数组不存在所求的距离数值,则添加进数组,若数组超过三个数值,则返回FALSE,否则返回true(其实只要两个就行了,不用距离平方比为1/2,自己举例证明…)
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
if (Arrays.equals(p1, p2) || Arrays.equals(p1, p3) || Arrays.equals(p1, p4) || Arrays.equals(p2, p3) ||
Arrays.equals(p2, p4) || Arrays.equals(p3, p4)) {
return false;
}
int x1 = p1[0];
int x2 = p2[0];
int x3 = p3[0];
int x4 = p4[0];
int y1 = p1[1];
int y2 = p2[1];
int y3 = p3[1];
int y4 = p4[1];
HashSet<Double> set = new HashSet<>();
set.add(Math.sqrt(Math.abs(x1 - x2) * Math.abs(x1 - x2) + Math.abs(y1 - y2) * Math.abs(y1 - y2)));
set.add(Math.sqrt(Math.abs(x1 - x3) * Math.abs(x1 - x3) + Math.abs(y1 - y3) * Math.abs(y1 - y3)));
set.add(Math.sqrt(Math.abs(x1 - x4) * Math.abs(x1 - x4) + Math.abs(y1 - y4) * Math.abs(y1 - y4)));
set.add(Math.sqrt(Math.abs(x2 - x3) * Math.abs(x2 - x3) + Math.abs(y2 - y3) * Math.abs(y2 - y3)));
set.add(Math.sqrt(Math.abs(x2 - x4) * Math.abs(x2 - x4) + Math.abs(y2 - y4) * Math.abs(y2 - y4)));
set.add(Math.sqrt(Math.abs(x3 - x4) * Math.abs(x3 - x4) + Math.abs(y3 - y4) * Math.abs(y3 - y4)));
return set.size() < 3;
}