gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:49:53 25 4
gpt4 key购买 nike

是否已经在 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°) 的角度 (°)

最佳答案

你只需要一个函数 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))

关于Python:使用到点 A (x0, y0) 的距离和角度找到给定点 B 的 x,y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23280636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com