Python 在坐标地图上获得位置的转角?
我试着让一个机器人转向另一个机器人,基于它们在全球坐标地图上各自的坐标 这是我写的代码,但似乎根本不起作用:Python 在坐标地图上获得位置的转角?,python,math,geometry,robot,Python,Math,Geometry,Robot,我试着让一个机器人转向另一个机器人,基于它们在全球坐标地图上各自的坐标 这是我写的代码,但似乎根本不起作用: def calcAngleToCoords(self, curAngle, curPosition, targPosition): retVal = False if type(curPosition) is list and type(targPosition) is list: x_1, y_1 = curPosition x_2,
def calcAngleToCoords(self, curAngle, curPosition, targPosition):
retVal = False
if type(curPosition) is list and type(targPosition) is list:
x_1, y_1 = curPosition
x_2, y_2 = targPosition
# Sets origin coordinate to zero
x_2 = x_2 - x_1
y_2 = y_2 - y_1
radius = math.sqrt(y_2 ** 2 + x_2 ** 2) # Pythagorean Thereom, a^2 + b^2 = c^2 | Radius = c, y_2 = a, x_2 = b
angle = curAngle * (math.pi / 180)
x_1 = radius * math.cos(angle)
y_1 = radius * math.sin(angle)
turnArc = math.atan( (y_1 - y_2) / (x_2 - x_1) ) * (180 / math.pi)
retVal = turnArc
# TODO: Check to see if the angle is always clockwise.
else:
raise TypeError("Invalid parameter types. Requires two lists.")
return(retVal)
有谁能告诉我一个更好的方法或者我做错了什么?这是一个项目,我的工作和最后期限即将到来,所以任何帮助将不胜感激 无需计算半径
x_2 = x_2 - x_1
y_2 = y_2 - y_1
angle = curAngle * (math.pi / 180)
dx = math.cos(angle)
dy = math.sin(angle)
turnArc = math.atan2(x_2 * dy - y_2 * dx, x_2 * dx + y_2 * dy ) * (180 / math.pi)
注意使用atan2函数,返回-pi和pi之间的角度。它可以正确确定旋转方向并找到最短的转弯。若只需顺时针旋转,若旋转弧为负,则加180,无需计算半径
x_2 = x_2 - x_1
y_2 = y_2 - y_1
angle = curAngle * (math.pi / 180)
dx = math.cos(angle)
dy = math.sin(angle)
turnArc = math.atan2(x_2 * dy - y_2 * dx, x_2 * dx + y_2 * dy ) * (180 / math.pi)
注意使用atan2函数,返回-pi和pi之间的角度。它可以正确确定旋转方向并找到最短的转弯。如果你只需要顺时针旋转,如果turnArc为负,加上180,你怎么知道它不工作?你怎么知道它不工作?你怎么知道它不工作?谢谢,但我有点困惑。你说不需要计算半径,但你在代码中包含了半径?谢谢,但我有点困惑。你说不需要计算半径,但你在代码中包含了半径?谢谢,但我有点困惑。你说不需要计算半径,但你在代码中包含了半径?