【问题标题】:Python: find a x,y coordinate for a given point B using the distance from the point A (x0, y0) and the anglePython:使用与点 A 的距离 (x0, y0) 和角度找到给定点 B 的 x,y 坐标
【发布时间】:2014-04-25 06:49:31
【问题描述】:

是否已经在 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°) 用指南针测量

【问题讨论】:

  • 您需要将 x0 和 y0 添加到返回值中。

标签: python coordinates distance trigonometry angle


【解决方案1】:

你只需要一个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 类会更合适):

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

【讨论】:

  • 也许使用math.radians而不是为它重写一个函数?
  • 感谢@niklas,你是对的。只需在point_pos公式中更改dist中的d,示例就完美了!
  • @M4rtini 好点。我也忘记了角度的简单翻译,因为 OP 的角度很奇怪。
  • 感谢@M4rtini 和 niklas 假设您的中心坐标为 x0 =0 和 y0 = 0,距离 AB = 5 且 B 的角度为 90°。坐标必须是 x0= 5, y0= 0。使用 point_pos(0, 0, 5, 90) 我得到 xb = -3.5355339059327373 和 yb = 3.5355339059327378
  • @Gianni 我得到 (5,0)。也许您使用的是我的代码的旧版本,我使用 pi/4 而不是 pi/2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2023-03-10
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
相关资源
最近更新 更多