-
Notifications
You must be signed in to change notification settings - Fork 16
/
ValidBoomerang.java
40 lines (34 loc) · 1.17 KB
/
ValidBoomerang.java
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
import java.util.Objects;
public class ValidBoomerang {
public boolean isBoomerang(int[][] points) {
Position p1 = Position.from(points[0]);
Position p2 = Position.from(points[1]);
Position p3 = Position.from(points[2]);
if (p1.equals(p2) || p2.equals(p3) || p3.equals(p1)) return false;
if (p1.x == p2.x) return p3.x != p2.x;
if (p2.x == p3.x) return true;
return (p2.y - p1.y) * (p3.x - p2.x) != (p3.y - p2.y) * (p2.x - p1.x);
}
private final static class Position {
private final int x;
private final int y;
private static Position from(int[] array){
return new Position(array[0], array[1]);
}
Position(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
return x == position.x && y == position.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
}