计算 D 维圆上 2 点之间的欧氏距离 space
Calculating the Euclidean distance between 2 points on a circle in D-dimensional space
计算两点之间的直接欧氏距离相当简单:
import torch
p1 = torch.tensor([1.0, 3.5])
p2 = torch.tensor([5.0, 9.2])
dis = torch.sum(torch.square(p1-p2))
dis
>>> tensor(48.4900)
但是,如何在不穿过圆的情况下计算圆上两点之间的距离?也就是说,圆周长的距离,D维space.
显然,在 2D 中,圆只是一个圆:
import numpy as np
import matplotlib.pyplot as plt
def circle_points(r, n):
circles = []
for r, n in zip(r, n):
t = np.linspace(0, 2*np.pi, n, endpoint=False)
x = r * np.cos(t)
y = r * np.sin(t)
circles.append(np.c_[x, y])
return circles
r = [2]
n = [20]
circles = circle_points(r, n)
fig, ax = plt.subplots()
for circle in circles:
ax.scatter(circle[:, 0], circle[:, 1])
ax.set_aspect('equal')
plt.show()
point_1 = circles[0][0]
point_2 = circles[0][11]
print('point_1: ', point_1, 'point_2: ', point_2)
>>> point_1: [2. 0.] point_2: [-1.90211303 -0.61803399]
而在 3D 中它将是球体、4D 超球体等
让 center
成为你的圆心。
然后你可以用点积公式计算两个中心到点向量之间的角度,它将点积与角度的余弦和向量的范数联系起来(寻找几何定义dot product)
normalize = lambda vect: vect/vect.norm()
v1 = normalize(point_1 - center)
v2 = normalize(point_2 - center)
angle = torch.arccos(v1.dot(v2))
那么,圆弧的长度就是圆的半径乘以角度,即
distance = angle*r
计算两点之间的直接欧氏距离相当简单:
import torch
p1 = torch.tensor([1.0, 3.5])
p2 = torch.tensor([5.0, 9.2])
dis = torch.sum(torch.square(p1-p2))
dis
>>> tensor(48.4900)
但是,如何在不穿过圆的情况下计算圆上两点之间的距离?也就是说,圆周长的距离,D维space.
显然,在 2D 中,圆只是一个圆:
import numpy as np
import matplotlib.pyplot as plt
def circle_points(r, n):
circles = []
for r, n in zip(r, n):
t = np.linspace(0, 2*np.pi, n, endpoint=False)
x = r * np.cos(t)
y = r * np.sin(t)
circles.append(np.c_[x, y])
return circles
r = [2]
n = [20]
circles = circle_points(r, n)
fig, ax = plt.subplots()
for circle in circles:
ax.scatter(circle[:, 0], circle[:, 1])
ax.set_aspect('equal')
plt.show()
point_1 = circles[0][0]
point_2 = circles[0][11]
print('point_1: ', point_1, 'point_2: ', point_2)
>>> point_1: [2. 0.] point_2: [-1.90211303 -0.61803399]
而在 3D 中它将是球体、4D 超球体等
让 center
成为你的圆心。
然后你可以用点积公式计算两个中心到点向量之间的角度,它将点积与角度的余弦和向量的范数联系起来(寻找几何定义dot product)
normalize = lambda vect: vect/vect.norm()
v1 = normalize(point_1 - center)
v2 = normalize(point_2 - center)
angle = torch.arccos(v1.dot(v2))
那么,圆弧的长度就是圆的半径乘以角度,即
distance = angle*r