Gia*_*ear 3 python trigonometry distance angle coordinates
是否已经在 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)
Run Code Online (Sandbox Code Playgroud)
其中x0= A 点的 x 坐标,y0= A 点的y 坐标,dist= A 和 B 之间的距离,= Btheta点相对于指南针测量的北 (0°) 的角度 (°)
您只需要一个将度数转换为弧度的函数。那么你的功能就变成了:
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)
Run Code Online (Sandbox Code Playgroud)
(如您所见,您在原始函数中混淆了正弦和余弦)
(还要注意线性角度变换,因为罗盘上的角度是顺时针方向,数学角度是逆时针方向。各个零点之间也有偏移)
您还可以使用复数来表示点,这比坐标元组要好一些(尽管专用Point类会更合适):
import cmath
def point_pos(p, d, theta):
return p + cmath.rect(d, pi/2-radians(theta))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6378 次 |
| 最近记录: |