Get distance between point A and C when their distances with a common point B is known

838 Views Asked by At

Background:

My plan is to get the overall euclidean distance matrix for all the vectors in N number of dataset. Each dataset is basically an array of n-dimensional points. For e.g: A dataset can be like [[1,2,3,...n],[1,2,3,..n],...[1,2,3,n]]. However, the datasets are not gonna be shared to a single entity for which I cannot compile them and thus will not know all the points to calculate pairwise euclidean distance for some similarity calculation and clustering analysis and modeling. However, I will be only notified what are the common vectors in the datasets and distance of the vectors in any dataset with respect to those common vectors without knowing or passing the point's coordinates. If the dataset could be shared in the same model, I wouldn't have to face this distributed calculation problem.

Problem:

Given,

  1. The euclidean distance between vector A and vector B
  2. The euclidean distance between vector B and vector C

How to calculate the euclidean-distance between point A and C?

Thanks in advance.

Important to mention: So far I have been trying to use @AmanKushwaha mentioned formula to calculate the euclidean-distance between point A and C via Python script. Thankfully, it gives me the correct distance for 2-dimensional vectors. But as I stated above, instead of 2-dimensional vectors, each dataset can consist an array of N dimensional vectors where the value of N can be upto 20,000 (Please see the latest comments).

2

There are 2 best solutions below

11
On BEST ANSWER

If $\angle ABC$ is known, say $\angle ABC=\theta$ then you can get the exact distance between the points $A$ and $C$ for given distances $AB$ and $AC$ using the following rule:

$AC=\sqrt{AB^2+BC^2-2AB.BC \cos{\theta}} \tag{i} \label{i}$ which is nothing but cosine rule.

Yes, you can find $AC$ for different angles between $AB$ and $BC$ by putting $\theta=0°,45°,90°,360°$ etc. in the above rule.

$\mathbf{Edit1:}$

After reading your comments, I understand that you can not use the coordinates of $A$ and $C$ to calculate the distance $AC$ but you can use a different attribute from both the datasets together even if that attribute involves the use of coordinates of $A$ and $C$ in respective datasets, like you said you can use $\angle ABD$ and $\angle CBD$ if $D$ is another common point to calculate $\angle ABC$

Well then, If that's the case then there's no need to look for any other common points. You can simply calculate slope of line $AB$ and $BC$ in the dataset containing $A$ and $C$ respectively and then use the slopes to calculate the angle between them. Here's how

Let $A\equiv (a_1,a_2) , B \equiv (b_1,b_2) ,C \equiv (c_1,c_2)$

$m_{AB}=\frac{b_2-a_2}{b_1-a_1}$

$m_{BC}=\frac{b_2-c_2}{b_1-c_1}$

Now, using both datasets together $\tan{\angle ABC}=\tan{\theta}=\frac{m_{BC}-m_{AB}}{1+m_{BC} m_{AB}}\text{ ( }0°\leq \theta \leq 180°) $

$$\implies\theta=\begin{cases} \tan^{-1}{\frac{m_{BC}-m_{AB}}{1+m_{BC} m_{AB}}}\text{ (if } \frac{m_{BC}-m_{AB}}{1+m_{BC} m_{AB}} \gt 0)\\ \pi + tan^{-1}{\frac{m_{BC}-m_{AB}}{1+m_{BC} m_{AB}}}\text{ (if } \frac{m_{BC}-m_{AB}}{1+m_{BC} m_{AB}} \lt 0)\\ 90° \text{ (if } \frac{m_{BC}-m_{AB}}{1+m_{BC} m_{AB}} \text{ is not defined or } m_{BC} m_{AB}=-1)\end{cases}$$

The case for $m_{AB}=m_{BC}$ is left since here $\theta$ can be either $0°$ or $180°$ but actual value can't be known using $\tan{\theta}$ which is zero for both the angles and clearly $\cos{\theta}$ differs significantly at $0°$ and $180°$ making the difference in distance $AC$. This is simply the case when lines $AB$ and $BC$ are parallel and hence, coincident since the lines share a common point $B$ and the problem we are facing is that we can't know if $B$ lies between $A$ and $C$ (i.e., $\theta =180°$) or $B$ is one of the extremes (i.e., $\theta =0°$).

Evaluate $\cos{\theta}$ using the $\theta$ found above and use it in $(\ref{i})$ to get the required distance $AC$

$\mathbf{Edit2:}$

Now that you mentioned that these are vectors we are dealing with, it becomes quite simple now if we can use $\vec A$ and $\vec B$ from one dataset to calculate $\vec{BA}$. Similarly we use, $\vec B$ and $\vec C$ from another dataset to calculate $\vec{BC}$

Then, $$\vec{AC}=\vec{BC}-\vec{BA}$$

The distance $AC$ is thus equal to $|\vec {AC}|$

$\mathbf{Note:}$

(i) The difference from previous methods is that now, not only we have distances $AB$ and $BC$ (which are magnitudes of their respective vectors) but we also have their direction, so the problem of finding the angle between $AB$ and $AC$, specifically $\angle ABC$ is now resolved.

(ii) What we've actually done there appears like cheating because we actually used the coordinates of $A$ and $C$ together (indirectly) from different datasets to get the vector $\vec{AC}$ and hence the distance $AC$. Here's how we did it:

Let $\vec{A}=\begin{bmatrix} a_{1} \\ a_{2} \\ \vdots \\ a_{n} \end{bmatrix} , \vec{B}=\begin{bmatrix} b_{1} \\ b_{2} \\ \vdots \\ b_{n} \end{bmatrix} , \vec{C}=\begin{bmatrix} c_{1} \\ c_{2} \\ \vdots \\ c_{n} \end{bmatrix}$ be three $n-$ dimensional column vectors such that $\vec A, \vec B$ are in one dataset and $\vec B, \vec C$ are in another.

Now, calculating $\vec{BA}$ from the first dataset, we get $$\vec{BA}= \vec A - \vec B = \begin{bmatrix} a_{1}-b_1 \\ a_{2}-b_2 \\ \vdots \\ a_{n}-b_n \end{bmatrix} $$.

Similarly, calculating $\vec{BC}$ from the second dataset, we get $$\vec{BC}= \vec C - \vec B = \begin{bmatrix} c_{1}-b_1 \\ c_{2}-b_2 \\ \vdots \\ c_{n}-b_n \end{bmatrix} $$.

Hence, $$\vec{AC}=\vec{BC}-\vec{BA}= \begin{bmatrix} a_{1}-c_1 \\ a_{2}-c_2 \\ \vdots \\ a_{n}-c_n \end{bmatrix}$$

$\therefore AC=|\vec{AC}|=\sqrt{(a_{1}-c_1)^2+(a_{2}-c_2)^2+ \ldots (a_{n}-c_n)^2}$

Look, the coordinates have been used.

(iii) Now the question is, can we use these vectors like $\vec{BA}$ and $\vec{BC}$ from the respective datasets which look like using the coordinates directly just calling it by other name. It is true that these vectors hold information about not only the lengths $AB$ and $BC$ but also the direction of these "directed" line segments, $\textbf{which is essentially what we need}$, but you can't get the coordinates of $A$ and $B$ from the $\vec{BA}$ (which is free to move anywhere in space). So, "the coordinates of $A$ and $B$" and "the vector $\vec {BA}$" are not exactly the same in terms of representing a data. The former gives the actual location of points in space and gives every kind of information possible about these points whereas the latter only gives the distance and direction of one point w.r.t other which is only a portion of information of the former.

(iv) You can also notice that, using the method above, we are only getting the vector $\vec{AC}$ and so the distance $AC$ and not the coordinates of $A$ and $C$, neither have we used these coordinates directly from the datasets to calculate $AC$. So, is there any other way to calculate the distance $AC$ using only the distances $AB$ and $BC$ not the vectors? $\textbf{No!}$ Believe it or not, you have to use the coordinates in some way or other to get the distance $AC$. As I have already said, the distance $AC$ varies with the position of these points in datasets (more specifically, the direction of one point w.r.t. other) and as such the values of distances $AB$ and $BC$ are not enough to get the distance $AC$.

(v) Assuming that you are not getting the distances like $AB$ and $AC$ from some external source and you are actually calculating these distances yourself using the coordinates you get from respective datasets, then why not use the coordinates to calculate the vectors instead holding the information (including the value of distance, the magnitude) we need to calculate $AC$. Moreover, since you had no problem in $\textbf{using the coordinates}$ to calculate the slopes for different pairs of points in a dataset and then using these slopes together to get the $\angle ABC$ in the previous method, I believe you shouldn't have any problem with using the vectors from different datasets together only because the use of coordinates looks more explicit here.

$\textbf{Edit 3:}$

From one source, get the vector $\vec{BA}$ calculated. From another source, get the vector $\vec{BC}$ calculated.

Once, you get these two vectors, calculate $\vec{AC}$ using the following formula $$\vec{AC}=\vec{BC}-\vec{BA}$$ Then you just have to calculate the magnitude of this vector. I hope you have some inbuilt functions to calculate the magnitude of a given vector, if not, you can make one. Here:

The vector $\vec{AC}$ will be like this: $$\begin{bmatrix} d_1 \\ d_2\\ \vdots \\ d_{n} \end{bmatrix}$$

Square the elements of these vectors, then add them and take positive square root. That's it. The distance $AC$ is nothing but the magnitude of $\vec{AC}$ which is $\sqrt{(d_1)^2+(d_2)^2+ \ldots (d_n)^2}$

$\textbf{I repeat, you are not using the coordinates of $A$ and $C$}$ Two different sources are using them to get the vectors, and you use the vectors to get the distance.

$$\textbf{The End}$$

0
On

This is just another way to find the first the equation in Aman's answer

Translate the plane so that $A$ coincides with the origin, and then rotate the plane such that $B$ lies on the positive end of the $OX$ axis. This preserves the distances between the points.

The points $C$, thus, are all the points $(x,y)$ that lie in a circle of radius $BC$ centered at $(AB, 0)$. We can describe $C$ using the parametric equations of a circle,

$$ C = (x,y) = (AB + BC \cos(\theta), BC\sin(\theta)), $$

where $\theta$ is the counterclockwise angle between the $OX$ axis and the $BC$ segment, as per the image

Calculating the distance between $C$ and $A$ gives us

$$ \begin{array}{} AC & = & \sqrt{(x-0)^2 + (y-0)^2}\\ & = & \sqrt{(AB + BC \cos(\theta))^2 + (BC\sin(\theta))^2}\\ & = & \sqrt{(AB)^2 + (BC)^2\cos^2(\theta) + 2(AB)(BC)\cos(\theta) + (BC)^2\sin^2(\theta)}\\ & = & \sqrt{(AB)^2 + (BC)^2 + 2(AB)(BC)\cos(\theta)}\\ \end{array} $$

Note that $\angle ABC$ and the $\theta$ I'm using here are supplementary. This means $\cos(\angle ABC) = -\cos(\theta)$, which wields

$$ \sqrt{(AB)^2 + (BC)^2 - 2(AB)(BC)\cos(\angle ABC)}, $$

the equation in Aman's answer.