View Full Version : Summary: Calculation of 2D Angles ('CAST' problem)

Dr. Chris Kirtley
12-19-2002, 02:08 AM
Dear all,

Thanks for the many replies about the 'CAST' problem. Most people seem
to be using the atan2 function. I am using Javascript at the moment, for
a course on biomechanics next semester:

I was suprised to find that Javascript does indeed support the ATAN2
function, math.atan2() so I will try it.

Note, however, that this is not the complete solution. As At Hof
mentioned, there are problems when the denominator is zero, and even
more problems when (as in not-gait activities) the segments move into
the third or even fourth quadrant.

I'm reassured that I haven't missed any very elegant solution all these
years, anyway. It seems rather intellectually unsatisfying to be having
to do all these tests (they get really very tedious when all four
quadrants are considered).

Dr. Chris Kirtley MD PhD
Associate Professor
Dept. of Biomedical Engineering
Catholic University of America

Anthony Blazevich

I've got around this problem in the past by using 'if - then'
statements. For
example code for 'if hip angle >180, then 180 + abs(hip angle)' where
abs is
the absolute value of the negative angle will give a correct value. Not
if that helps in your programming format.




I don't know if my solution is 'elegant' or not, but have a look at the
attached BASIC code. The 'elegance' lies in the test of segment
found in the gosub routine 'SegmentPosition' (I think).


Human Movement Laboratory
Clinical Research Centre
School of Health Professions
University of Brighton

"Einar S. Idsų"

As you probably know, your problem stems from the fact that the
tan-function is periodic with a period of pi and thereby maps all real
numbers to all real numbers with periodicity pi. By definition atan,
the inverse of the tan-function, maps all real numbers to (-pi/2,pi/2).
If the argument of atan is negative, then it is mapped to (-pi/2,0)
and if it is positive then it is mapped to (0,pi/2).

Without loss of generalization, let the origin of the shank be in
coordinates 0,0 and the knee at x,y. The angle of the knee will then be
expressed as atan(y/x). y is always positive, whereas x can be both
positive and negative. For positive x, all is well in your case, but for
negative x, the fraction y/x becomes negative. atan does not distinguish
between the two cases of negative y/positive x and positive y/negative x
since all it sees is a single negative number. The atan-function then
"chooses" to map the negative number to the area (-pi/2,0). Note: atan
of course also doesn't distinguish between x and y both positive or both
negative, but that is not a problem in your case since y is always

The conclusion is that in order to be able to use the atan-function
properly, you have to know the sign of both y and x (i.e. the quadrant).
In this case it is quite simple since the only argument that needs to be
checked is x: If x is negative, then the value pi should be added to the
calculated angle.

However, due to this mapping problem, an alternative function has
arisen. It is commonly known as the atan2-function and it takes two
arguments instead of one: atan(y,x). This function will map all real
numbers to the area (-pi,pi). Its implementation is just as described
above: Calculate the arctangent of the ratio y/x and check the signs of
x and y to determine the quadrant. This function could just as easily
have been implemented to map all real numbers to (0,2pi).

Hope this helps.

Kind regards,
Einar S. Idsų

PhD student
Dept. of Engineering Cybernetics
Norwegian University of Science and Technology

Ulrich Simon

Dear Dr. Kirtley,

Are you able to use the ATAN2 Function? The ATAN2 function is
implemented in most of the standard program languages (C, MatLab,
FORTRAN, &). This function is called with two arguments instead of only
one argument.
Use: alpha = ATAN2(Y, X) in the range ( Pi < alpha < Pi)
Instead of: alpha = ATAN(Z); Z = Y/X in the range ( Pi/2 < alpha < Pi/2)
However, internally the ATAN2 function does nothing else than your self
written tool: determining the particular quadrant from the signs of the
two arguments. There is definitely no other way to calculate it.

Best regards
Ulrich Simon

Dr.-Ing. Ulrich Simon
Institute of Orthopaedic Research and Biomechanics
University of Ulm

To unsubscribe send SIGNOFF BIOMCH-L to LISTSERV@nic.surfnet.nl
For information and archives: http://isb.ri.ccf.org/biomch-l