Does the magnitude of $2$ line need to be the same to use Dot product?

26 Views Asked by At

Was programming and was trying to find angle of $2$ point but got $0.0$ but I know that looks like a $45$ degree angle but when I even out the length of one of the lines then it works. I dont remember ever reading somewhere it saying that the magnitude had to be even to get a correct radian, is this true?

enter image description here

1

There are 1 best solutions below

1
On

$$\boldsymbol{a}\cdot\boldsymbol{b}=\lVert\boldsymbol{a}\rVert\cdot\lVert\boldsymbol{b}\rVert\cdot\cos(\theta)$$ Where $\theta$ is the angle between the vectors $\boldsymbol{a}, \boldsymbol{b}$. To me, your programming (language) is just bizarre - and I'm a programmer myself - so there may be some coding error, I can't tell, but in general here you want to do the following (in pseudocode):

float product = dot(a, b)
float normalised_product = product/(a.magnitude * b.magnitude)
float radians = arccos(normalised_product)
float degrees = radians * 180 / PI

You have to normalise the product first, since you must note that the dot product also multiplies the cosine by the magnitude of both vectors, so to retrieve the cosine you must divide out the magnitudes: $$\cos(\theta)=\frac{\boldsymbol{a}\cdot\boldsymbol{b}}{\lVert\boldsymbol{a}\rVert\cdot\lVert\boldsymbol{b}\rVert}$$