Reputation: 2441
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.
Upvotes: 3
Views: 577
Reputation: 3573
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, i.e
distance = angle*r
Upvotes: 3