查找指南针2度之间的最接近差异-Javascript [英] Finding the closest difference between 2 degrees of a compass - Javascript

查看:111
本文介绍了查找指南针2度之间的最接近差异-Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是想找出指南针的两个点相隔多少度.例如,如果某人面对270,而其指南针为280,则这两个点之间的夹角为10度.相对于第一个标题,如果要在左边,在右边是正数,我还要一个负数.

I'm basically trying to find how many degrees apart two points of a compass are. For example if a person is facing 270 and their compass is 280 there are 10 degrees between those 2 points. I'd also like a negative number if it's to the left and positive to the right, relative to the 1st heading.

例如,当to标题为350和020时出现问题.这两个点相隔30度,但结果为-330.

The problem comes when the to headings are 350 and 020 for example. These two points are 30 degrees apart but would give a result of -330.

下面是我的代码示例:

function ConvertToRadians(_var)
{
    return _var * (Math.PI/180);
}
function ConvertToDegrees(_var)
{
    return _var * (180/Math.PI);
}
function GetHeadingDiff(_Heading1, _Heading2)
{   
    return ConvertToDegrees((ConvertToRadians(_Heading2) - ConvertToRadians(_Heading1)));
}

$(document).ready(function (){
    $('#process').click(function (e){
        e.preventDefault();
        var Hdg1 = parseFloat($('#heading1').val());
        var Hdg2 = parseFloat($('#heading2').val());
        $('#results').html(GetHeadingDiff(Hdg1,Hdg2));
    });
});

<input id="heading1" type="text" />
<input id="heading2" type="text" />
<button id="process" type="button">Submit</button>
<div id="results">
</div>

我确定我缺少一些简单的数学函数,只是似乎无法弄清楚.

I'm sure there is some simple math function I'm missing, I just can't seem to figure it out.

这是jsfiddle链接 http://jsfiddle.net/dTmPn/3/

Here is a jsfiddle link http://jsfiddle.net/dTmPn/3/

推荐答案

function GetHeadingDiff(_Heading1, _Heading2)
{   
    return (_Heading2-_Heading1+540) % 360 - 180;
}

示例:

GetHeadingDiff( 280, 270 )
-10
GetHeadingDiff( 270, 280 )
10
GetHeadingDiff( 350, 20 )
30
GetHeadingDiff( 20, 350 )
-30

这篇关于查找指南针2度之间的最接近差异-Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆