【问题标题】:Error in calculating the angle of a line计算直线角度的错误
【发布时间】:2011-06-11 11:30:59
【问题描述】:

我试图计算我的开始位置和结束位置之间的角度,但它保留了一个介于 2 和 -2 之间的数字。我从堆栈溢出(http://stackoverflow.com/questions/2676719/calculating-the-angle-between-the-line-defined-by-two-points)中获得了这段代码,并将其集成到我的 GridClass 中。有人知道这段代码有什么问题吗?

public static double getAngle(GeoPosition startPosition, GeoPosition endPosition)
{

double a_x = endPosition.getLatitude() - startPosition.getLatitude();
double a_y = endPosition.getLongitude() - startPosition.getLongitude();

return Math.atan2(a_y, a_x);
}

【问题讨论】:

  • 真的应该先学数学再学编程。
  • @drysdam:这也是我的第一个猜测,但是应该有“介于 -3 和 3 之间”的值......
  • 不,反正切只返回 -π/2 和 π/2 之间的值。
  • 哦,你是对的 - 所以它实际上在 -1.6 和 1.6 之间......

标签: 2d line angle


【解决方案1】:

但它保留了一个介于 2 和 -2 之间的数字

Java 中的三角法使用弧度。如果您需要度数 (0-360),请使用 Math.toDegrees:

将以弧度为单位的角度转换为以度为单位的大致等效角度。

【讨论】:

    猜你喜欢
    • 2011-02-10
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2017-07-23
    相关资源
    最近更新 更多