简体   繁体   中英

Calculating the Euclidean distance between 2 points on a circle in D-dimensional space

It's fairly straightforward to calculate a direct Euclidean distance between 2 points:

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)

However, how can I calculate the distance between 2 points on circle without going through the circle? That it, the distance on the circle's perimeter, in D-dimensional space.

Clearly, in 2D a circle is just a circle:

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]

While in 3D it will be a sphere, 4D hypersphere, etc.

Let center ben the center of your circle.

Then you can compute the angle between the two center-to-point vectors with the dot product formula, which relates the dot product with the cosine of the angle and the norm of the vectors (look for the geometric definition of the dot product )

normalize = lambda vect: vect/vect.norm()
v1 = normalize(point_1 - center)
v2 = normalize(point_2 - center)
angle = torch.arccos(v1.dot(v2))

Then, the length of the arc is the radius of the circle times the angle, ie

distance = angle*r

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM