I had a similar problem. I got it. All the calculations are in degrees.
I needed to calculate id a gps location is inside a rectangle.
Or, I needed to see if an angle x
is between angle check+r
and angle check-r
.
check-r<x<check+r
.
If you need a<x<b
, find the angle check
in the middle of a
and b
and then the distance (r
) of check
from a
or b
.
The method normalize, changes the angles from -infinity...infinity to -180...180.
The method check, takes the arguments
x
: the angle that we need to see if it is between the angles check-r and check+r.
check
: the angle to check with.
r
: the radius around angle check.
private static double normalize(double x) {
x = x % 360;
if (x>=180) {
return x-360;
}
if (x<-180) {
return x+360;
}
return x;
}
public static boolean check(double x, double check, double r) {
x = x - check;
x = normalize(x);
return x<r && x>-r;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…