forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointClassification.java
More file actions
29 lines (27 loc) · 845 Bytes
/
PointClassification.java
File metadata and controls
29 lines (27 loc) · 845 Bytes
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
package geometry;
public class PointClassification {
public enum Position { LEFT, RIGHT, BEHIND, BEYOND, ORIGIN, DESTINATION, BETWEEN }
// Classifies position of point p against vector a
public static Position classify(long px, long py, long ax, long ay) {
long cross = px * ay - py * ay;
if (cross > 0) {
return Position.LEFT;
}
if (cross < 0) {
return Position.RIGHT;
}
if (px == 0 && py == 0) {
return Position.ORIGIN;
}
if (px == ax && py == ay) {
return Position.DESTINATION;
}
if (ax * px < 0 || ay * py < 0) {
return Position.BEYOND;
}
if (ax * ax + ay * ay < px * px + py * py) {
return Position.BEHIND;
}
return Position.BETWEEN;
}
}