-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck If Given Four Points Form a Square
36 lines (32 loc) Β· 1.46 KB
/
Check If Given Four Points Form a 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
This Solution Is in Java
// User function Template for Javaclass Solution {
class Solution {
int distance(int a, int b, int p, int q){
return (p - a) * (p - a) + (q - b) * (q - b);
}
int fourPointSquare(int points[][]){
for (int i = 0; i < 4; i++){
for (int j = i + 1; j < 4; j++){
if (points[i][0] == points[j][0] && points[i][1] == points[j][1]){
return 0;
}
}
}
int d2 = distance(points[0][0], points[0][1], points[1][0], points[1][1]);
int d3 = distance(points[0][0], points[0][1], points[2][0], points[2][1]);
int d4 = distance(points[0][0], points[0][1], points[3][0], points[3][1]);
if(d2 == d3 && 2 * d2 == d4){
int d = distance(points[1][0], points[1][1], points[3][0],points[3][1]);
return ((d == distance(points[2][0], points[2][1], points[3][0],points[3][1]) && d == d2) ? 1: 0);
}
if(d3 == d4 && 2 * d3 == d2){
int d = distance(points[1][0], points[1][1], points[2][0],points[2][1]);
return ((d == distance(points[1][0], points[1][1], points[3][0],points[3][1]) && d == d3) ? 1: 0);
}
if(d2 == d4 && 2 * d2 == d3){
int d = distance(points[1][0], points[1][1], points[2][0],points[2][1]);
return ((d == distance(points[2][0], points[2][1], points[3][0],points[3][1]) && d == d2) ? 1: 0);
}
return 0;
}
}