[英]Python: Does a module exist which already find an angle and the distance between two points?
首先,我很抱歉發布這個簡單的問題。 可能有一個模塊來計算兩點之間的角度和距離。
特定
你可以用下式計算角度, theta
和A和B之間的距離:
import math
def angle_wrt_x(A,B):
"""Return the angle between B-A and the positive x-axis.
Values go from 0 to pi in the upper half-plane, and from
0 to -pi in the lower half-plane.
"""
ax, ay = A
bx, by = B
return math.atan2(by-ay, bx-ax)
def dist(A,B):
ax, ay = A
bx, by = B
return math.hypot(bx-ax, by-ay)
A = (560023.44957588764, 6362057.3904932579)
B = (560036.44957588764, 6362071.8904932579)
theta = angle_wrt_x(A, B)
d = dist(A, B)
print(theta)
print(d)
產量
0.839889619638 # radians
19.4743420942
(編輯:由於您正在處理平面中的點,因此比點積公式更容易使用atan2
)。
當然, math
模塊有atan2
。 math.atan2(y, x)
是角度(0, 0)
到(x, y)
。
math.hypot(x, y)
也是距離形式(0, 0)
到(x, y)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.