I'm trying to calculate the angle between three 3D points.
The results are not what I expect.
Can anyone please tell me
where the code may be incorrect and how to fix it?
# points are A, B, C each with 3 coordinates (x, y, z)
# stored as array elements [0],[1],[2]
# value desired is cos(theta) between AB and BC that
# should range 0 to 1, where 1 is a straight line A to C
# Calculate by cos(theta) = AB*BC (dot product)
@AB = ($B[0]-$A[0], $B[1]-$A[1], $B[2]-$A[2]);
@BC = ($C[0]-$B[0], $C[1]-$B[1], $C[2]-$B[2]);
$lengthAB = sqrt((($AB[0])**2)+(($AB[1])**2)+(($AB[2])**2);
$lengthBC = sqrt((($BC[0])**2)+(($BC[1])**2)+(($BC[2])**2);
@unitvectorAB=($AB[0]/$lengthAB, $AB[1]/$lengthAB, $AB[2]/$lengthAB);
@unitvectorBC=($BC[0]/$lengthBC, $BC[1]/$lengthBC, $BC[2]/$lengthBC);
$costheta = ($unitvectorAB[0]*$unitvectorBC[0])+($unitvectorAB[1]*$uni
+tvectorBC[1])+($unitvectorAB[2]*$unitvectorBC[2]);
Thank you!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.