Kill Process



I also consider using sort to solve the problem, not by sorting the four points position but sorting the length of two points. Finally, [a, a, a, a, 2a, 2a] is the correct pattern.
public class Solution { public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) { int[] lens = new int[6]; lens[0] = (p1[0]p2[0])*(p1[0]p2[0])+(p1[1]p2[1])*(p1[1]p2[1]); lens[1] = (p1[0]p3[0])*(p1[0]p3[0])+(p1[1]p3[1])*(p1[1]p3[1]); lens[2] = (p1[0]p4[0])*(p1[0]p4[0])+(p1[1]p4[1])*(p1[1]p4[1]); lens[3] = (p2[0]p3[0])*(p2[0]p3[0])+(p2[1]p3[1])*(p2[1]p3[1]); lens[4] = (p2[0]p4[0])*(p2[0]p4[0])+(p2[1]p4[1])*(p2[1]p4[1]); lens[5] = (p3[0]p4[0])*(p3[0]p4[0])+(p3[1]p4[1])*(p3[1]p4[1]); Arrays.sort(lens); for(int i=0; i<3; i++) { if (lens[i]==lens[i+1]) continue; else return false; } if (lens[4]!=lens[5]) return false; if (lens[0]!=0) return true; else return false; } }

You don't consider the situation that a diamond but its angles are not 90degree. (For example,it angles are 60degree,120degree,60degree,120degree,like put two equilateral triangles together). I think your solution should add "the length of four sides != the length of diagonals" in order to avoid this case. And forgive my poor english...

What I did is check the vectors from the center to four points
while the length of any one of these vectors must be same and not zero
and angle among these four vectors must be 90degree or 180degree,length_Sq > 0 // not a dot
length_Sq  v[i].x * v[i].x  v[i].y * v[i].y == 0 // same lengthv[i].x * v[j].x + v[i].y * v[j].y == 0 //90degree:

(v[i].x + v[j].x == 0) && (v[i].y + v[j].y == 0) // 180degree