# How to count angle between vector and horizontally oriented vector?

I need to calculate in my Java application an angle between my line and horizontal line that has the same beginning. I have a line described by its equation:

$$f(x) = ax + b.$$

I would like to know angles alpha and beta in degrees. From this equation I can compute few points that lie on both lines and use it for computation. An angle between horizontal line and another two lines is max. 90 degreees.

Edit: now I am in my problem in this stage:

I have three points and I need to calculate an angle between them:

start = {x, y}
end1 = {x, y}
end2 = {x, y}


I create two vectors that these three points define a move them into beginnig of coordinate system:

vector1 = [{end1.x - start.x}, {end1.y - start.y}]
vector2 = [{end2.x - start.x}, {end2.y - start.y}]


Now I count the angle between these two vectors (in radians):

radians = ({vector1.x * vector2.x} + {vector1.y * vector2.y}) / (vector1Length * vector2Length);

then I convert (in Java) radians into degrees.

Question:

When I apply above mentioned technique with these three points:

start = {0, 0}
end1 = {1, 0}
end2 = {1, 1}


it calculates 1.41 radians what is 81 degress, but in my opinion it should be 45 degrees.

What am I doing wrong?

-
For some basic information about writing math at this site see e.g. here, here, here and here. – user93957 Nov 29 '13 at 13:43

Pay attention to brackets and arccosine functions, you missed them in your code.

radians = arccos(( {vector1.x * vector2.x} + {vector1.y * vector2.y} ) / vector1Length * vector2Length );

-
In my code I have brackets, it's not the problem. – user1315357 Mar 27 '13 at 8:37
I am sure you miss the arccosine, check that too. And make sure you are calculating length with the right formula: len(v)=sqrt(v.x*v.x + v.y*v.y). – gev Mar 27 '13 at 8:39
Yes, calculating of length is correct. My value for counting arccos is 1.414213562373095 (numerator = 2.0, denominator = 1.4142135623730951) and this is not able Java function Math.acos convert to arc cosine. I don't know what I am still doing wrong. – user1315357 Mar 27 '13 at 8:46
How did you get numerator 2, it should be 1? Double check your code in that part. – gev Mar 27 '13 at 8:51
That plot is not quite right, look at the scales of x and y axises. If you want to see the actual angle you must have the same scale on both of them. Imagine $AB=4mm$ and $BC=136mm$, do you see how big is angle $CAB$? – gev Mar 27 '13 at 11:43

For vectors $\vec{x} = (x_1, x_2)$ and $\vec{y} = (y_1, y_2)$, the cosine of angle between them is given by your formula.

$$\cos \theta = \frac{\vec{x} \cdot \vec{y}}{\|\vec{x}\| \, \|\vec{y}\|}.$$

You need to use the arccosine (also called inverse cosine) function to recover $\theta$.

$$\theta = \arccos \left( \frac{\vec{x} \cdot \vec{y}}{\|\vec{x}\| \, \|\vec{y}\|} \right).$$

-
Thanks, I know it from Wikipedia, I need to implement it. – user1315357 Mar 27 '13 at 8:48