Converting Cartesian to Polar coordinates system
The function below uses atan2() to convert a 2d vector expressed
in cartesian coordinates (x,y) to the polar coordinates (rho,theta).
#include <math.h>
void
cartesian_to_polar(const double x, const double y, /* cartesian */
double *rho, double *theta /* polar */
)
{
*rho = hypot (x,y); /* better than sqrt(x*x+y*y) */
*theta = atan2 (y,x);
}
[[ Notes to the reviewers:
- similar examples might be derived using atan2f(), atan2l().
- In this revised version, I have used hypot() as suggested by Fred T.
Sure it could also serve to illustrate hypot(), but I believe
it is better to provide another example for hypot().
- In my opinion, it would be valuable to add some explanations right
after the example _if_ the editorial constraints allow it. Namely:
" There are alternative ways to compute the angle theta, using
e.g. asin(), acos() or atan(). However, atan2() presents here two
advantages:
- the angle's quadrant is automatically determined.
- the singular cases (0,y) are taken into account.
Finally, using hypot() is better than sqrt() for special cases,
refer to <hypot() reference> "
end of notes to the reviewers ]]
--
COMPUTERBILD 15/03: Premium-e-mail-Dienste im Test
--------------------------------------------------
1. GMX TopMail - Platz 1 und Testsieger!
2. GMX ProMail - Platz 2 und Preis-Qualitätssieger!
3. Arcor - 4. web.de - 5. T-Online - 6. freenet.de - 7. daybyday - 8. e-Post
|