作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否已经在 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/
我是一名优秀的程序员,十分优秀!