-
Notifications
You must be signed in to change notification settings - Fork 0
/
593. Valid Square
38 lines (34 loc) · 1.16 KB
/
593. Valid Square
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class Solution {
private static final int X = 0;
private static final int Y = 1;
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
ArrayList<Double> segments = new ArrayList(6);
segments.add(calcDistance(p1,p2));
segments.add(calcDistance(p1,p3));
segments.add(calcDistance(p1,p4));
segments.add(calcDistance(p2,p3));
segments.add(calcDistance(p2,p4));
segments.add(calcDistance(p3,p4));
Collections.sort(segments);
double diagonal = segments.get(5);
double side = segments.get(3);
int diagCount = 1;
int sideCount = 0;
for(int i = 0; i < segments.size()-1; i++){
double segment = segments.get(i);
if(segment == diagonal){
diagCount++;
}else if(segment == side){
sideCount++;
}
}
return diagCount == 2 && sideCount == 4;
}
private double calcDistance(int[] p1, int[] p2){
int x1 = p1[X];
int y1 = p1[Y];
int x2 = p2[X];
int y2 = p2[Y];
return Math.pow(x1-x2,2) + Math.pow(y1-y2,2);
}
}