-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindSquare.java
More file actions
119 lines (101 loc) · 2.45 KB
/
FindSquare.java
File metadata and controls
119 lines (101 loc) · 2.45 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class Coordinates {
float x;
float y;
public float squareDistance (Coordinates p) {
return (float) (Math.pow((this.x - p.x), 2) + Math.pow((this.y - p.y), 2));
}
public void setValues (String coordinates) {
String[] vals = coordinates.split(",");
x = Float.parseFloat(vals[0].substring(1));
y = Float.parseFloat(vals[1].substring(0, vals[1].length() - 1));
}
}
public class FindSquare {
Coordinates[] p;
public boolean isSquare() {
float[] dist = new float[6];
float diagDist;
float side;
int diagDistCount = 0;
int sideCount = 0;
dist[0] = p[0].squareDistance(p[1]);
dist[1] = p[0].squareDistance(p[2]);
if (dist[0] == dist[1]) {
sideCount+=2;
side = dist[0];
diagDist = 2 * side;
} else if (dist[0] == 2 * dist[1]) {
diagDistCount++;
diagDist = dist[0];
side = dist[1];
sideCount++;
} else if (dist[1] == 2 * dist[0]) {
diagDistCount++;
diagDist = dist[1];
side = dist[0];
sideCount++;
} else {
return false;
}
dist[2] = p[0].squareDistance(p[3]);
if (dist[2] == side) {
sideCount++;
} else if (dist[2] == diagDist) {
diagDistCount++;
} else {
return false;
}
dist[3] = p[1].squareDistance(p[2]);
if (dist[3] == side) {
sideCount++;
} else if (dist[3] == diagDist) {
diagDistCount++;
} else {
return false;
}
dist[4] = p[1].squareDistance(p[3]);
if (dist[4] == side) {
sideCount++;
} else if (dist[4] == diagDist) {
diagDistCount++;
} else {
return false;
}
dist[5] = p[2].squareDistance(p[3]);
if (dist[5] == side) {
sideCount++;
} else if (dist[5] == diagDist) {
diagDistCount++;
} else {
return false;
}
if (diagDistCount == 2 && sideCount == 4) {
return true;
}
return false;
}
public FindSquare (String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int i;
while ((line = br.readLine()) != null) {
p = new Coordinates[4];
String[] params = line.split(",\\s");
for (i = 0; i < 4; i++) {
p[i] = new Coordinates();
p[i].setValues(params[i]);
}
System.out.println(isSquare());
}
}
public static void main (String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Unsupported number of parameters. Exiting.");
System.exit(1);
}
FindSquare fs = new FindSquare(args[0]);
}
}