Python:使用距离点A(x0,y0)的距离和角度找到给定点B的x,y坐标

时间:2014-04-24 21:58:32

标签: python coordinates distance trigonometry angle

是否已经在Python中实现了一个函数,使用以度(°)表示的给定角度从点A(x0,Y0)找到点B的X,Y?

from math import cos, sin

def point_position(x0, y0, dist, theta):
    return dist*sin(theta), dist*cos(theta)

其中x0 =点A的x坐标,y0 =点A的y坐标,dist = A和B之间的距离,theta =角度(°)通过罗盘测量的B点面向北(0°)

1 个答案:

答案 0 :(得分:4)

您只需要converts degrees to radians的功能。那你的功能就变成了:

from math import sin, cos, radians, pi
def point_pos(x0, y0, d, theta):
    theta_rad = pi/2 - radians(theta)
    return x0 + d*cos(theta_rad), y0 + d*sin(theta_rad)

(你可以看到你在原来的功能中混合了正弦和余弦)

(还要注意线性角度变换,因为罗盘上的角度是顺时针方向,数学角度是逆时针方向。相应的零点之间也存在偏移)

您还可以使用complex numbers来表示点,这比坐标元组更好(尽管专用的Point类更合适):<​​/ p>

import cmath
def point_pos(p, d, theta):
    return p + cmath.rect(d, pi/2-radians(theta))