Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

angle between two lines

604 views
Skip to first unread message

Dan

unread,
Jan 26, 2005, 9:43:12 AM1/26/05
to
Hi a fairly mathsy question:
Im trying to calculate the angle between two lines (4 coordinates),
it seems to work most of the time but in some circumstances is out by
180 degrees or is -ve when should be +ve..
% coordinates for first line are I1c1 and I1c2, and for second line
are I2c1 and I2c2

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?

Ben Barrowes

unread,
Jan 26, 2005, 10:30:37 AM1/26/05
to
You are using the two-quadrant atan.

help atan2

Jérôme

unread,
Jan 26, 2005, 10:30:50 AM1/26/05
to
Hi,

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

Dan

unread,
Jan 26, 2005, 11:20:17 AM1/26/05
to
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

Jérôme

unread,
Jan 26, 2005, 12:35:27 PM1/26/05
to
Hi,

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

Roger Stafford

unread,
Jan 26, 2005, 4:21:05 PM1/26/05
to

-------
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

Dan

unread,
Jan 27, 2005, 4:54:59 AM1/27/05
to

Woo that does the trick thank you everyone

0 new messages