ABCD
BAN USERpublic static final int SMALLER = 0x10;
public static final int BIGGER = 0x20;
public static boolean crossesItself(List<Double> distances){
if(distances.size() < 4){
return false;
}
Double prevY = distances.get(0);
Double prevX = distances.get(1);
//need these two so that we know when we have both set
int xOrientation = 0;
int yOrientation = 0;
for(int i = 2; i < distances.size(); i++){
int direction = i % 2;
if(direction == 0){
Double y = distances.get(i);
int newYOrientation = y.compareTo(prevY) > 0 ? BIGGER : SMALLER;
if(yOrientation == 0){
yOrientation = newYOrientation;
xOrientation = newYOrientation;
} else if(yOrientation == BIGGER && newYOrientation == SMALLER){
yOrientation = newYOrientation;
xOrientation = newYOrientation;
System.out.println("Y coordinate changed from BIGGER -> SMALLER");
} else if(newYOrientation != yOrientation){
return true;
}
prevY = y;
} else {
Double x = distances.get(i);
int newXOrientation = x.compareTo(prevX) > 0 ? BIGGER: SMALLER;
if(xOrientation == 0){
xOrientation = newXOrientation;
yOrientation = newXOrientation;
} else if(xOrientation == BIGGER && newXOrientation == SMALLER){
xOrientation = newXOrientation;
yOrientation = newXOrientation;
System.out.println("X coordinate changed from BIGGER -> SMALLER");
} else if(newXOrientation != xOrientation){
return true;
}
prevX = x;
}
}
return false;
}
This is false. Consider the sequence: [2.0, 2.0, 3.0, 1.0, 4.0]
- ABCD September 17, 2015