How to check if dot product calculation is true

106 Views Asked by At

I've written a python code that calculates the dot product of two x,y,z points converted from lan/lon points (out of three lat/lon points)

The formula I'm using.

The angle I'm trying to get.

My questions are this: I got this calculation from another source, It doesn't consider my third point at all, why is this, it doesn't need it?

how can I verify that my answer is true?

My answer Is 0.0017907536175649065 It doesn't match the angle it should on the map.

I mean, are the zeroes can be ignored? , It's just the answer I got doesn't make any sense to me.

My code if someone's instersted.

def get_angle():
    bax = 3986139.124316147
    bay = 3924600.2399883396
    baz = 1126
    acx = 3995519.3498883517
    acy = 3919784.087359532
    acz = 1800 - baz
    a = [bax, bay, baz]
    b = [acx, acy, acz]
    a_mag = np.linalg.norm(a)
    b_mag = np.linalg.norm(b)

    theta = np.arccos(np.dot(a, b) / (a_mag * b_mag))
    print(theta)
1

There are 1 best solutions below

2
On

Your formula defines two new vectors, $\mathbf a$ and $\mathbf b$ in terms of three old vectors, $\mathbf B$, $\mathbf A$ and $\mathbf c$, which is absolutely perplexing naming. I think it's much clearer to just scrap all that and think about it as follows:

We have three points $A$, $B$, and $C$ (with position vectors $\mathbf a$, $\mathbf b$, $\mathbf c$, respectively). We wish to find the angle $\angle \mathit{ABC}$. We will do so by finding the angle between the two vectors $\overrightarrow{\mathit{BA}}$ and $\overrightarrow{\mathit{BC}}$. (Hopefully it is clear to you why this it the same thing).

But we can calculate $\overrightarrow{\mathit{BA}}$ and $\overrightarrow{\mathit{BC}}$ as $\mathbf a - \mathbf b$ and $\mathbf c - \mathbf b$ respectively. Then we can use the dot-product angle fact (in fact this is how some people define angle!):

The angle $\theta$ between two vectors $\mathbf u$, $\mathbf v$ (in $n$ dimensions) is given by \begin{equation*} \cos \theta = \frac{\mathbf u \cdot \mathbf v} {\lVert \mathbf u \rVert \lVert \mathbf v \rVert} \end{equation*}

This might culminate in a little program like so:

import numpy as np

def angle_between_points(a, b, c):
    """
    Given position vectors a, b, c representing points ABC, and
    return the angle ABC (in radians).

    Position vectors should be Numpy arrays.
    """
    vec_b_to_a = a - b
    vec_b_to_c = c - b
    b_to_a_mag = np.linalg.norm(vec_b_to_a)
    b_to_c_mag = np.linalg.norm(vec_b_to_c)
    return np.arccos(np.dot(vec_b_to_a, vec_b_to_c)
                     / (b_to_a_mag * b_to_c_mag))

print(angle_between_points(np.array([1, 2]),
                           np.array([1, 1]),
                           np.array([2, 1])))

print(angle_between_points(np.array([1, 2]),
                           np.array([1, 1]),
                           np.array([100, 1])))

print(angle_between_points(np.array([1, 2]),
                           np.array([1, 1]),
                           np.array([100, 100])))