我们的想法是将其中一个点平移到原点,将另一个点偏移到与移动到原点的点偏移相同的距离,然后将第二个点(不是原点)转换为极坐标。它返回的是原点和点之间的距离,以及角度。
import math
# where object1 and object2 are tuples of x, y pairs
def AngleFromObject1ToObject2InDegrees(object1, object2):
translate_to_origin = (object1[0] - 0, object1[1] - 0)
new_points = (object2[0] - translate_to_origin[0], object2[1] - translate_to_origin[1])
return (math.sqrt(new_points[0] ** 2 + new_points[1] ** 2), math.degrees(math.atan(new_points[1] / new_points[0])))
print(AngleFromObject1ToObject2InDegrees((1,1),(5,5)))
print(AngleFromObject1ToObject2InDegrees((5,5),(1,1)))
作为“一行”:
import math
# where object1 and object2 are tuples of x, y pairs
def AngleFromObject1ToObject2InDegrees(object1, object2):
return (math.sqrt((object2[0] - object1[0]) ** 2 + (object2[1] - object1[1]) ** 2), math.degrees(math.atan((object2[1] - object1[1]) / (object2[0] - object1[0]))))
print(AngleFromObject1ToObject2InDegrees((1,1),(5,5)))
print(AngleFromObject1ToObject2InDegrees((5,5),(1,1)))