angle1 = atan((I1c2(2) - I1c1(2)) / (I1c2(1) - I1c1(1)))
angle2 = atan((I2c2(2) - I2c1(2)) / (I2c2(1) - I2c1(1)))
difference = angle1 - angle2;
angle = ((difference/pi) * 180)
Any ideas what im doing wrong? Am I being a total spanner?
help atan2
I take a paper.
I draw two intersected lines.
What a surprise I find 4 angles a, -a, b and -b.
Your problem is not enough constrained !
Using vector gives you less solution.
Try the geometrical definition of dot product.
Jérôme
v1 = I1c2 - I1c1;
v2 = I2c2 - I2c1;
x1 = v1(1);
y1 = v1(2);
x2 = v2(1);
y2 = v2(2);
dotproduct = (x1*x2 + y1*y2);
v1mag = sqrt(x1*x1 + y1*y1);
v2mag = sqrt(x2*x2 + y2*y2);
costheta = dotproduct / (v1mag * v2mag);
angle = acos(costheta);
this gives me the correct angle every time but sometimes should be
negative but isnt.. tried a few if statements to work out which way
it should be going but no joy..
any thoughts? Sorry im not really a mathematician..
Thanx, Dan
Same using Matlab functions :
costh=dot(v1,v2)/(norm(v1)*norm(v2));
th=acos(costh);
This should give you the correct answer
I'am not a great mathematician but I think this should always give
you the good result.
Jérôme
-------
In article <eef9...@webx.raydaftYaTP>, Dan
<daNO...@theDONTSPAMME303factory.co.uk> wrote:
> Dont understand atan2...
> dot products it is..
> we have two vectors v1 and v2:
>
> v1 = I1c2 - I1c1;
> v2 = I2c2 - I2c1;
> x1 = v1(1);
> y1 = v1(2);
> x2 = v2(1);
> y2 = v2(2);
>
> dotproduct = (x1*x2 + y1*y2);
>
> v1mag = sqrt(x1*x1 + y1*y1);
> v2mag = sqrt(x2*x2 + y2*y2);
>
> costheta = dotproduct / (v1mag * v2mag);
>
> angle = acos(costheta);
>
> this gives me the correct angle every time but sometimes should be
> negative but isnt.. tried a few if statements to work out which way
> it should be going but no joy..
> any thoughts? Sorry im not really a mathematician..
> Thanx, Dan
------
Hello Dan,
It is far better to use the 'atan2' function for your purposes. Using
your notation above,
theta = atan2(x1*y2-y1*x2,x1*x2+y1*y2); % That is, 'angle'
This gives you a four-quadrant answer that lies between -pi and +pi with
the understanding that positive answers represent counterclock rotation
from line 1 to line 2, and negative ones a clockwise rotation (in a
right-hand coordinate system.) This also avoids the loss of accuracy you
would encounter when your lines are nearly parallel.
[This formula comes from your formula above for cos(theta) and the other
one giving sin(theta) from the cross product of v1 and v2.]
--
(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
Woo that does the trick thank you everyone