三个地理位置之间的角度

Angle between three geolocations

我需要计算三个地理位置之间的角度,其中两个始终相同,只有一个在变化(例如当用户走路时)。

第一个点是用户的起始位置。第二点是用户的当前位置,所以一开始第一点等于第二点。第一点和固定点总是相同的。我有每个点的经纬度。

我尝试使用这个公式:

double angle1 = Math.atan2(startingLocation.getLongitude() - destinationLocation.getLongitude(), startingLocation.getLatitude() - destinationLocation.getLatitude());
double angle2 = Math.atan2(currentLocation.getLongitude() - destinationLocation.getLongitude(), currentLocation.getLatitude() - destinationLocation.getLatitude());

return Math.toDegrees(angle1 - angle2);

但是例如,当第一个点 == 第二个点时,除了度数为 0。但它给了我奇怪的结果,如 176 度。问题出在哪里?

使用这个

double theta = 180.0 / Math.PI * Math.atan2(x1 - x2, y1 - y2);

两条线之间的角度可以通过以下函数计算

public static double angleBetween2Lines(Line2D line1, Line2D line2)
{
    double angle1 = Math.atan2(line1.getY1() - line1.getY2(),
                           line1.getX1() - line1.getX2());
    double angle2 = Math.atan2(line2.getY1() - line2.getY2(),
                           line2.getX1() - line2.getX2());
    return angle1-angle2;
}

来源:Calculating the angle between two lines without having to calculate the slope? (Java)

如果你的起点是(x1,y1),终点是(x2,y2),固定点是(x3,y3)那么你应该使用

double angle1 = Math.atan2(y3-y1,x3-x1);
double angle2 = Math.atan2(y3-y2,x3-x2);
double result = angle1-angle2;