Java - 三角学地狱

发布于 2024-12-27 18:22:27 字数 223 浏览 1 评论 0原文

我的任务是能够用 Java 编写一个类来计算从北到某个点的方位角。唯一已知的物体有 2 个位置,两者的方位角和距离均为 0。例如,位置 1 - 30 度和 10m,位置 2 - 190 度和 50m。例如,如果您想从位置 1 行驶到位置 2 或从位置 2 行驶到位置 1,您将如何计算方位角?我可以使用余弦规则计算两个位置之间的距离,但不知道如何创建一个能够在不同场景下准确计算方位的类?

任何帮助或建议将不胜感激。

I have a task of being able to program a Class in Java to calculate the bearing from North to a point. The only objects that are known are 2 positions, both have a bearing from North and the distance from 0. So for example - position 1 - 30 degrees and 10m, position 2 - 190 degrees and 50m. How would you calculate the bearing if you wanted to travel from position 1 to position 2 for instance or from position 2 to 1? I can calculate the distance between the 2 positions using the cosine rule, but have no idea how to create a class that will accuratly calculate the bearing in different scenarios?

Any help or advise would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云雾 2025-01-03 18:22:27

来自http://en.wikipedia.org/wiki/Law_of_cosines#Applications

< img src="https://i.sstatic.net/E7DwF.png" alt="在此处输入图像描述">

...一旦您获得了所有三个边长,这将为您提供第三个角度你的三角形。

(半正矢公式用于球体上的导航......我认为我们只是担心平面上的向量。)

From http://en.wikipedia.org/wiki/Law_of_cosines#Applications:

enter image description here

...once you have all three side lengths, this will give you the third angle of your triangle.

(The Haversine formula is for navigation on a sphere... I think we're just worried about vectors on a plane.)

月依秋水 2025-01-03 18:22:27

我相信您正在寻找的是 Haversine 公式,谷歌搜索它会产生各种语言的实现。

I believe what you are looking for is the Haversine formula, googling it will yield implementations in various languages.

街角卖回忆 2025-01-03 18:22:27

这是从此处给出的 javascript 解决方案移植的 java 版本:
在 Javascript 中使用半正矢公式 作者:talkol

// this was a pojo class we used internally...
public class GisPostalCode {

    private String country;
    private String postalCode;
    private double latitude;
    private double longitude;

    // getters/setters, etc.
}


public static double distanceBetweenCoordinatesInMiles2(GisPostalCode c1, GisPostalCode c2) {

    double lat2 = c2.getLatitude();
    double lon2 = c2.getLongitude();
    double lat1 = c1.getLatitude();
    double lon1 = c1.getLongitude();

    double R = 6371; // km
    double x1 = lat2 - lat1;
    double dLat = x1 * Math.PI / 180;
    double x2 = lon2 - lon1;
    double dLon = x2 * Math.PI / 180;

    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) *
        Math.sin(dLon/2) * Math.sin(dLon/2);

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double d = R * c;

    // convert to miles
    return d / 1.60934;
}

This is a java version ported from a javascript solution given here:
Using the Haversine Formula in Javascript by talkol

// this was a pojo class we used internally...
public class GisPostalCode {

    private String country;
    private String postalCode;
    private double latitude;
    private double longitude;

    // getters/setters, etc.
}


public static double distanceBetweenCoordinatesInMiles2(GisPostalCode c1, GisPostalCode c2) {

    double lat2 = c2.getLatitude();
    double lon2 = c2.getLongitude();
    double lat1 = c1.getLatitude();
    double lon1 = c1.getLongitude();

    double R = 6371; // km
    double x1 = lat2 - lat1;
    double dLat = x1 * Math.PI / 180;
    double x2 = lon2 - lon1;
    double dLon = x2 * Math.PI / 180;

    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) *
        Math.sin(dLon/2) * Math.sin(dLon/2);

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double d = R * c;

    // convert to miles
    return d / 1.60934;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文