如何轻松地将线角转换为导航刻度(即范围为[0,360])和“北" = 0度)? [英] How do I easily convert a line angle to a navigational-bearing scale (i.e., with range of [0,360) and "North" = 0 deg)?

查看:345
本文介绍了如何轻松地将线角转换为导航刻度(即范围为[0,360])和“北" = 0度)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两者之间画一条线,分别是(x1,y1)(x2,y2).我知道我可以使用反正切和斜率找出那条线的角度(以度为单位):

I have two points, (x1,y1) and (x2,y2), that I'd like to draw a line between. I know I can figure out the angle (in degrees) of that line using arctangent and the slope:

atan((y2-y1)/(x2-x1))*180/pi

但是,我如何将这个角度转换为[0,360]比例?基本上,我希望角度为指南针刻度,其中北"为0度,东"为90度,南"为180度,西"为270度.

However, how would I convert this angle to a [0,360] scale? Basically, I want my angle to be on a compass scale in which "North" is 0 deg, "East" is 90 deg, "South" is 180 deg, and "West" is 270 deg.

谢谢!

推荐答案

只是概括@bgoldst的答案:

Just to generalize @bgoldst's answer:

(A1 - atan2(y2-y1,x2-x1) * 180/pi ) %%360

以下是该方程各个部分的说明:

Here is the explanation of the various parts of this equation:

  1. 您必须使用atan2()而不是atan().

atan2()是带有两个参数的反正切函数.使用两个参数的目的是收集有关输入符号的信息,以便返回所计算角度的适当象限.对于单参数反正切(atan)函数是不可能的.

atan2() is the arctangent function with two arguments. The purpose of using two arguments is to gather information on the signs of the inputs in order to return the appropriate quadrant of the computed angle. This is not possible for the single-argument arctangent (atan) function.

在这种情况下,使用模数运算符%%给出将角度除以360的余数.在360.

The modulus operator %% is used in this case to give the remainder of dividing the angle by 360. In this way, we force the angles to "wrap around" at 360.

使用atan2计算的角度乘以180/pi,以将弧度(atan2的默认输出)的答案转换为度.

The angle calculated using atan2 is multiplied by 180/pi in order to convert the answer from radians (atan2's default output) into degrees.

如果我们在该处停靠,则所得角度将基于标准三角形式,其中东部" = 0度.所有角度将相对于东方"角度. = 0.

If we stopped there, the resulting angle would be based on standard trigonometric form in which "East" = 0 degrees. All angles would be relative to "East" = 0.

通过从某个角度( A1 )中减去以度为单位的计算角度,我们可以将计算出的角度偏移 A1 度.对于导航轴承刻度(北方" = 0度),我们将设置A1 = 90.

By subtracting our calculated angle in degrees from some angle (A1), we can offset the calculated angle by A1 degrees. In the case of the navigational-bearing scale (with "North" = 0 degrees) we would set A1 = 90.

 (90 - atan2(y2-y1,x2-x1) * 180/pi ) %%360

这篇关于如何轻松地将线角转换为导航刻度(即范围为[0,360])和“北" = 0度)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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