Jump to content

find the angle between two lines


dthomson

Recommended Posts

Hi,

I was wondering if anyone had developed a VI for calculating the angle between to arbitrary lines which both pass through the origin. E.g., given phi1, theta1, phi2, theta2, find the angle between the two vectors. I've found some descriptions on the web. It doesn't appear entirely trivial, but not all that difficult either. But if someone has already done it, it would be nice to have that as a starting point.

Thanks,

Dave T.

Link to comment

QUOTE(dthomson @ Oct 11 2007, 11:53 AM)

Hi,

I was wondering if anyone had developed a VI for calculating the angle between to arbitrary lines which both pass through the origin. E.g., given phi1, theta1, phi2, theta2, find the angle between the two vectors. I've found some descriptions on the web. It doesn't appear entirely trivial, but not all that difficult either. But if someone has already done it, it would be nice to have that as a starting point.

Thanks,

Dave T.

Well, it isn't addition...

Convert polar to rect for the end points:

sin(phi1) = y1/phi1 then

y1 = sin(phi1)/phi1

cos(phi1) = x1/phi1 then

x1 = cos(phi1)/phi1

m1 = (y2 - y1)/(x2 - x1)

you said (x2,y2) = (0,0)

so then m1 = y1/x1

get m2 for the (phi2, theta2) line

the angle rho between two lines is tan(rho)= (m2 - m1)/(1+m2*m1)

put it in a formula node.

Did I really do that for you? It's not like me at all. Maybe I did it wrong ;-)

Mike

Link to comment

Mike,

Thanks for the suggestion. I'm having a bit of a time interpreting it, though, for two reasons. Perhaps my description wasn't detailed enough. I intended to imply that phi and theta were the azimuth and elevation in three dimensions. So my first problem is that your description seems to apply to two dimensions. My fault, since I wasn't explicit enough. Secondly, though, I don't understand, even in two dimensions, how sin(phi1) = y1/phi1. I think sin(phi)=y1/R. Since we're just talking angles and directions, you could assume unit vectors, R=1, and calculate y1 and y2 based on phi1 and phi2, but I think that would just be sin(phi1) and sin(phi2), without the additional factor of phi1 and phi2.

I think one could approach the 3D problem by rotating the coordinate system so that the two lines are in a plane, then using your approach. There are other approaches as well. But I was just wondering if anyone had already packaged them up.

Regards,

Dave T.

Link to comment

I was taking phi to be length and assumed you were doing polar reather than spherical. You didn't mention the vector length so I thought is was an easier problem.

Lot's of bookkeepping, not trivial, I see now.

So you have to ignore what I wrote.Sin(phi) is wrong, I meant to say sin(theta).

Sorry. Now I get it, maybe I have what you want somewhere. I don't want to derive it on the fly since I am demonstrably bad at that, eh?

M

QUOTE(dthomson @ Oct 11 2007, 02:03 PM)

Mike, Thanks for the suggestion. I'm having a bit of a time interpreting it, though, for two reasons. Perhaps my description wasn't detailed enough. I intended to imply that phi and theta were the azimuth and elevation in three dimensions. So my first problem is that your description seems to apply to two dimensions. My fault, since I wasn't explicit enough. Secondly, though, I don't understand, even in two dimensions, how sin(phi1) = y1/phi1. I think sin(phi)=y1/R. Since we're just talking angles and directions, you could assume unit vectors, R=1, and calculate y1 and y2 based on phi1 and phi2, but I think that would just be sin(phi1) and sin(phi2), without the additional factor of phi1 and phi2. I think one could approach the 3D problem by rotating the coordinate system so that the two lines are in a plane, then using your approach. There are other approaches as well. But I was just wondering if anyone had already packaged them up.Regards, Dave T.

OK. I have the CRC in front of me now.

Quoting now [my stuff in brackets]:

For the line P1(x1, y1, z1), P2(x2, y2, z2)

[where A, B, and © are the angles from the x, y, and z axes respectively, you don't have ©]

[d is the vector length]

[x1, x2, y1, y2, z1, z3 are projections onto the the X, Y and Z planes of the line end points]

[d = SQRT((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)]

l = Cos(A) = (x2-x1)/d, m = cos(B) = (y2 - y1)/d, n = cos© = (z2-z1)/d

The Releationship Between Direction Cosines :

cos^2(A) +cos^2(B) + cos^2© = 1

[looks like you need to calculate angle © from this to get n]

[the editor is making the copyright symbol out of parenCparen and I don't know how to escape it :-( ]

or [simplified]

l^2 + m^2 + n^2 = 1

The Angle PHI Between 2 Lines with the Direction Cosines l1, m1, n1, and l2, m2, and n2:

PHI = l1*l2 + m1*m2 + n1*n2

That seems to be all you need. Better than I remebered. The Law of Cosine is pretty cool.

Mike

Link to comment

QUOTE(rpursley @ Oct 11 2007, 03:32 PM)

I believe x dot y = mag x * mag y * cos(alpha)

In your case you only mention phi and theta so I assume they are unit vectors.

x dot y = cos(ALPHA) = [cos(THETAx-THETAy)*sin(PHIx)*sin(PHIy)] + [cos(PHIx)*cos(PHIy)]

I think my math is right.

We were all missing it. He has the magnitude and two angles phi and theta. It is 3D in spherical coords.

It boils down to the angle between two lines in 3 space, which is l1*l2 + m1*m2 + n1*n2

where l = cos(theta1), l2 = cos(theta2), m1 = cos(phi1), m2 = cos(phi2)

the angles to the z axis has to be calculated to get n1 = cos(gamma2), n2 = cos(gamma2)

using the law of cosines is easiest for this

cos^2(theta) + cos^2(phi) + cos^2(gamma) = 1

so gamma = (acos(acos(1 - cos^2(theta) - cos^2(phi)))

and so on.

Might be a dot product for this too, but I have never used such.

Mike

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.